Renaming the current file in Vim

前端 未结 21 685
轻奢々
轻奢々 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:37

    There’s a function in Gary Bernhardt’s .vimrc that handles this.

    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 n :call RenameFile()
    

提交回复
热议问题