我应该如何重命名我的当前文件在Vim?

例如:

我正在编辑person.html_erb_spec.rb 我希望它重命名为person.haml_spec.rb 我想继续编辑person.haml_spec.rb

我该如何优雅地做这件事呢?


当前回答

如果你使用git并且已经有tpope的插件逃犯。然后简单地Vim:

:Gmove newname

这将:

重命名磁盘上的文件。 在git repo中重命名文件。 将文件重新加载到当前缓冲区中。 保留撤销历史记录。

如果你的文件还没有添加到git repo,那么先添加它:

:Gwrite

其他回答

我建议:从tpope的eunuch(太监)来命名这个。

它还包括一系列其他方便的命令。

Rename命令目前定义如下(检查repo是否有更新!):

command! -bar -nargs=1 -bang -complete=file Rename :
  \ let s:file = expand('%:p') |
  \ setlocal modified |
  \ keepalt saveas<bang> <args> |
  \ if s:file !=# expand('%:p') |
  \   call delete(s:file) |
  \ endif |
  \ unlet s:file

有一个小插件可以让你这么做。

如果你使用git并且已经有tpope的插件逃犯。然后简单地Vim:

:Gmove newname

这将:

重命名磁盘上的文件。 在git repo中重命名文件。 将文件重新加载到当前缓冲区中。 保留撤销历史记录。

如果你的文件还没有添加到git repo,那么先添加它:

:Gwrite

这个小脚本并不完美(你必须按下额外的回车键),但它完成了工作。

function Rename()
  let new_file_name = input('New filename: ')
  let full_path_current_file = expand("%:p")
  let new_full_path = expand("%:p:h")."/".new_file_name
  bd    
  execute "!mv ".full_path_current_file." ".new_full_path
  execute "e ".new_full_path
endfunction                                                                                                                                                                                                                                 

command! Rename :call Rename()
nmap RN :Rename<CR>

我用NERDTree插件来做:

:NERDTreeFind

然后按m

要重命名,您可以在当前节点上选择(m)并更改文件名。也有选项,如删除,复制,移动等…