Renaming the current file in Vim

前端 未结 21 1035
轻奢々
轻奢々 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条回答
  •  萌比男神i
    2021-01-29 17:42

    This little script isn't perfect (the extra carriage-return you have to press) but it get's the job done.

    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
    

提交回复
热议问题