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

例如:

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

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


当前回答

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

其他回答

如果文件已经保存:

:!mv {file location} {new file location}
:e {new file location}

例子:

:!mv src/test/scala/myFile.scala src/test/scala/myNewFile.scala
:e src/test/scala/myNewFile.scala

权限要求:

:!sudo mv src/test/scala/myFile.scala src/test/scala/myNewFile.scala

保存为:

:!mv {file location} {save_as file location}
:w
:e {save_as file location} 

Windows未验证

:!move {file location} {new file location}
:e {new file location}

这样怎么样(经过杰克的建议改进):

:exe "!mv % newfilename" | e newfilename

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

我用NERDTree插件来做:

:NERDTreeFind

然后按m

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

在编辑时写入文件-:w newname -以创建一个副本。 开始编辑新副本-:e#。 (可选)删除旧副本-:!rm oldname。

在Windows上,可选的第三步略有变化:

(可选)删除旧的Windows副本-:!德尔oldname。