我应该如何重命名我的当前文件在Vim?
例如:
我正在编辑person.html_erb_spec.rb 我希望它重命名为person.haml_spec.rb 我想继续编辑person.haml_spec.rb
我该如何优雅地做这件事呢?
我应该如何重命名我的当前文件在Vim?
例如:
我正在编辑person.html_erb_spec.rb 我希望它重命名为person.haml_spec.rb 我想继续编辑person.haml_spec.rb
我该如何优雅地做这件事呢?
当前回答
如果文件已经保存:
:!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}
其他回答
该命令被称为:saveas,但不幸的是,它不会删除您的旧文件,您必须手动执行。参见:help save获取更多信息。
编辑:
大多数vim安装都有一个集成的文件资源管理器,您可以使用它进行此类操作。尝试:在命令模式下探索(我会将其映射到一个功能键,这非常方便)。例如,可以用R重命名文件,也可以用D删除文件。但是在资源管理器中按<F1>会给你一个更好的概述。
如果文件已经保存:
:!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}
这个小脚本并不完美(你必须按下额外的回车键),但它完成了工作。
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>
你也可以使用netw
explore命令在打开文件的目录中打开netrw
:E
将光标移到需要重命名的文件上:
R
输入新名字,回车,按y。
:!mv % %:h/new_name
寄存器%包含当前文件的名称。'%:h'显示包含当前文件的目录'head',例如:%:hreturns /abc/def,当你的文件完整路径是abc/def/my.txt