blog_spec.rbvim でinsideという名前の新しいファイルを作成したいのです[working directory]/spec/models/が、ディレクトリがまだ存在しませんか?
ディレクトリを作成してファイルの編集を開始する最も速い方法は何ですか? ワンライナーはありますか?
blog_spec.rbvim でinsideという名前の新しいファイルを作成したいのです[working directory]/spec/models/が、ディレクトリがまだ存在しませんか?
ディレクトリを作成してファイルの編集を開始する最も速い方法は何ですか? ワンライナーはありますか?
:!mkdir -p spec/models
:w spec/models/blog_spec.rb
これに頻繁に遭遇する場合は、コマンドを追加する価値があるかもしれません。
command -nargs=1 E execute('silent! !mkdir -p "$(dirname "<args>")"') <Bar> e <args>
その行を.vimrcファイルに追加すると、次のように簡単に使用できます。
:E spec/models/blog_spec.rb
編集これはLinux/Macでのみ機能し、Windowsでは機能しません。
次のコマンドを試してください。
function s:MKDir(...)
if !a:0
\|| stridx('`+', a:1[0])!=-1
\|| a:1=~#'\v\\@<![ *?[%#]'
\|| isdirectory(a:1)
\|| filereadable(a:1)
\|| isdirectory(fnamemodify(a:1, ':p:h'))
return
endif
return mkdir(fnamemodify(a:1, ':p:h'), 'p')
endfunction
command -bang -bar -nargs=? -complete=file E :call s:MKDir(<f-args>) | e<bang> <args>
このコマンドは、ビルトインの代わりになることを意図しています:e。
mkdir が実行されない条件 (順):
`generate filename` さ`=generate_filename()`れ+commandます++opt。最後の 3 つのケースでは何もすべきではありません。
上記は次を追加する準備ができていcnoreabbrevます:
cnoreabbrev <expr> e ((getcmdtype() is# ':' && getcmdline() is# 'e')?'E':'e')
-complete=file物事を台無しにします: 補完だけでなく、引数の処理も追加します (したがって、`展開と特殊文字の存在をチェックしても意味がありません)。複数の「ファイル名」を持つことを禁止します (したがって、++opt はありません)。
-barコメントを開始する`="String"`ため、使用できなくなります。"あなたができないので、それなし-barでは:eエミュレーションではありませんE file | another command。
別のバージョン:
function s:MKDir(...)
if !a:0
\|| isdirectory(a:1)
\|| filereadable(a:1)
\|| isdirectory(fnamemodify(a:1, ':p:h'))
return
endif
return mkdir(fnamemodify(a:1, ':p:h'), 'p')
endfunction
command -bang -bar -nargs=? -complete=file E :call s:MKDir(<f-args>) | e<bang> <args>
通常、ファイルを保存しようとした後でのみ、親ディレクトリがまだ存在していないことがわかります。
このコードは、 でディレクトリを作成する:wか、単にで作成するように促します:w!:
augroup vimrc-auto-mkdir
autocmd!
autocmd BufWritePre * call s:auto_mkdir(expand('<afile>:p:h'), v:cmdbang)
function! s:auto_mkdir(dir, force)
if !isdirectory(a:dir)
\ && (a:force
\ || input("'" . a:dir . "' does not exist. Create? [y/N]") =~? '^y\%[es]$')
call mkdir(iconv(a:dir, &encoding, &termencoding), 'p')
endif
endfunction
augroup END