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

例如:

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

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


当前回答

:h rename()是迄今为止最简单、最干净的方法。 就叫它吧 :调用重命名("oldname", "newnane")

其他回答

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

:Gmove newname

这将:

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

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

:Gwrite

Tim Pope设计了一个更大的插件vim-eunuch,它包含了重命名函数和其他一些功能(删除、查找、保存所有、chmod、sudo编辑等等)。

使用vim-eunuch重命名文件。

:请确保移动

相对于rename.vim:

:重命名!)请

节省了一些按键:)

我建议:从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

在Gary Bernhardt的.vimrc中有一个函数处理这个问题。

function! RenameFile()
let old_name = expand('%')
let new_name = input('New file name: ', expand('%'), 'file')
if new_name != '' && new_name != old_name
    exec ':saveas ' . new_name
    exec ':silent !rm ' . old_name
    redraw!
endif
endfunction
map <leader>n :call RenameFile()<cr>

你也可以使用netw

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

:E

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

R

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