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

例如:

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

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


当前回答

你也可以使用netw

explore命令在打开文件的目录中打开netrw

:E

将光标移到需要重命名的文件上:

R

输入新名字,回车,按y。

其他回答

我建议:从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
sav person.haml_spec.rb | call delete(expand('#'))

要重命名现有文件而不使用插件,您应该使用命令

:Explore

这个命令允许您浏览文件。目录,删除或重命名它们。然后你应该在资源管理器中导航到必要的文件,而不是键入R命令,这将允许你重命名文件名

我用NERDTree插件来做:

:NERDTreeFind

然后按m

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

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

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>