Renaming the current file in Vim

前端 未结 21 688
轻奢々
轻奢々 2021-01-29 17:15

How should I rename my current file in Vim?

For example:

  • I am editing person.html_erb_spec.rb
  • I would like it renamed to person
相关标签:
21条回答
  • 2021-01-29 17:56

    Another way is to just use netrw, which is a native part of vim.

    :e path/to/whatever/folder/
    

    Then there are options to delete, rename, etc.

    Here's a keymap to open netrw to the folder of the file you are editing:

    map <leader>e :e <C-R>=expand("%:p:h") . '/'<CR><CR>
    
    0 讨论(0)
  • 2021-01-29 17:57

    You can also use :f followed by :w

    0 讨论(0)
  • 2021-01-29 17:59

    The command is called :saveas, but unfortunately it will not delete your old file, you'll have to do that manually. see :help saveas for more info.

    EDIT:

    Most vim installations have an integrated file explorer, which you can use for such operations. Try :Explore in command mode (I would actually map that to a function key, it's very handy). You can rename files with R or delete them with D, for example. But pressing <F1> in the explorer will give you a better overview.

    0 讨论(0)
  • 2021-01-29 18:00
    :!mv % %:h/new_name
    

    Register % contains the name of the current file.'%:h'shows the directory 'head' containing the current file, e.g.: %:hreturns /abc/def when your file full path is abc/def/my.txt

    0 讨论(0)
  • 2021-01-29 18:00

    How about this (improved by Jake's suggestion):

    :exe "!mv % newfilename" | e newfilename
    
    0 讨论(0)
  • 2021-01-29 18:01
    sav person.haml_spec.rb | call delete(expand('#'))
    
    0 讨论(0)
提交回复
热议问题