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

例如:

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

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


当前回答

在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>

其他回答

sav person.haml_spec.rb | call delete(expand('#'))

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

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

:Gmove newname

这将:

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

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

:Gwrite

我用NERDTree插件来做:

:NERDTreeFind

然后按m

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

:sav newfile | !rm #

注意,它不会从缓冲区列表中删除旧文件。如果这对你来说很重要,你可以使用以下方法:

:sav newfile | bd# |