English 中文(简体)
a. 在新目录中一行建立档案?
原标题:Create file inside new directory in vim in one step?
  • 时间:2012-05-01 07:17:06
  •  标签:
  • vim

在正文中,我想在<代码>[工作目录]/spec/models/内建立一个称为“条码”的新档案,但名录还没有。

建立目录和开始编辑档案的最快途径是什么? 任何一分子?

最佳回答

指挥:

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。

哺乳动物不使用的条件(按顺序排列):

  1. Command is run without arguments
  2. Command is run with `generate filename` or `=generate_filename()` backticks filename generators or with +command/++opt switches.
  3. Command contains more then one argument or has unescaped special characters.
  4. Argument is a directory.
  5. Argument is an existing file.
  6. Argument is a file in an existing directory.

在最近3起案件中,没有做任何事,第二和第三起案件不可能处理,更复杂。

The above is ready for adding a cnoreabbrev:

cnoreabbrev <expr> e ((getcmdtype() is#  :  && getcmdline() is#  e )? E : e )

-complete=file spoils things: it add not only completion, but also arguments processing (thus checking for ` expansion and special characters presence does not make sense) and forbids having multiple “filenames” (thus no ++opt).

<代码>-bar使您无法使用'=“String”,因为现在就开始发表评论。 没有<条码>-条码>。 这不是一个<条码>:e,因为你不能填写<条码>。 E. 档案

另一种版本:

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>
问题回答
:!mkdir -p spec/models
:w spec/models/blog_spec.rb

如果你经常遇到这种情况,可能值得为此增加指挥。

command -nargs=1 E execute( silent! !mkdir -p "$(dirname "<args>")" ) <Bar> e <args>

如果在您的档案中添加这一条,你可以简单地照此使用:

:E spec/models/blog_spec.rb

Edit 这一工作将仅涉及Libert/Mac,而不是Windows。

我一般认为,只有之后,才有家长名录,试图挽救档案。

该代码将促使你以<代码>创建名录: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




相关问题
Autoupdate VIM Plugins?

Is it possible to update vim plugins automatically?

how to unindent in vim without leaving edit mode?

I m writing a lot of python code recently, and i used the tab-to-space mode in vim. I was just wondering how would i unindent in vim without leaving edit mode for example after i finished if...: block....

Scrolling inside Vim in Mac s Terminal

I ve been googling around trying to figure out if it s possible to use my mouse wheel to scroll while inside Vim in Mac s Terminal, with no luck. It seems as if only X11 or iTerm support this. Before ...

Vim - Deleting XML Comments

How do I delete comments in XML? If the opening and the closing comment tags are on the same line, I use :g/^<!--.*-->$/d to delete the comment. How to delete the comments that are spread ...

Limiting a match in vim to certain filetypes?

I have the following in my .vimrc to highlight lines longer than 80 chars: highlight OverLength ctermbg=red ctermfg=white guibg=#592929 match OverLength /\%81v.*/ This works quite well. However, the ...

Profiling Vim startup time

I’ve got a lot of plugins enabled when using Vim – I have collected plugins over the years. I’m a bit fed up with how long Vim takes to start now, so I’d like to profile its startup and see which of ...

热门标签