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

例如:

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

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


当前回答

Vim确实有一个重命名函数,但不幸的是它不保留历史。

在不丢失历史的情况下重命名文件的最简单的操作系统不可知的方法是:

:saveas new_file_name
:call delete(expand('#:p'))

Expand ('#:p')返回旧文件的完整路径。

如果你还想从缓冲区列表中删除旧文件,请使用:bd #。

或者创建一个插件

如果希望使用快速命令重命名文件,请在~/下添加一个新文件。Vim /plugin包含以下内容:

function! s:rename_file(new_file_path)
  execute 'saveas ' . a:new_file_path
  call delete(expand('#:p'))
  bd #
endfunction

command! -nargs=1 -complete=file Rename call <SID>rename_file(<f-args>)

Rename命令可以帮助您快速重命名文件。

其他回答

另一种方法是只使用netrw,它是vim的原生部分。

:e path/to/whatever/folder/

然后还有删除、重命名等选项。

下面是打开netrw到你正在编辑的文件文件夹的键图:

map <leader>e :e <C-R>=expand("%:p:h") . '/'<CR><CR>

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

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

:exe "!mv % newfilename" | e newfilename

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

Vim确实有一个重命名函数,但不幸的是它不保留历史。

在不丢失历史的情况下重命名文件的最简单的操作系统不可知的方法是:

:saveas new_file_name
:call delete(expand('#:p'))

Expand ('#:p')返回旧文件的完整路径。

如果你还想从缓冲区列表中删除旧文件,请使用:bd #。

或者创建一个插件

如果希望使用快速命令重命名文件,请在~/下添加一个新文件。Vim /plugin包含以下内容:

function! s:rename_file(new_file_path)
  execute 'saveas ' . a:new_file_path
  call delete(expand('#:p'))
  bd #
endfunction

command! -nargs=1 -complete=file Rename call <SID>rename_file(<f-args>)

Rename命令可以帮助您快速重命名文件。