Vim: moving files?
Currently if I'm editing a file and I decide that I want to rename it, I use:
:!mv % new.py :new new.py :bd
To rename the file, open the new file, then delete the old buffer.
Is there a simpler way to rename a file without leaving the old buffers hanging around?
(I rea开发者_JAVA百科lize that I could also use :sav
, but then I would need to :bp
a few times to find the previous buffer, :bd
it, then :bn
back to the new buffer)
I think you may be looking for this plugin.
It is a little plugin that does :saveas and removes the old file.
I think adding a plugin is much more complicated than what you're looking for. You could use a function to bind this functionality to a new command.
function! s:movefunction(arg)
let oldnum = bufnr("%")
let newname = a:arg
execute "!mv %" . " " . newname
exe "new " . newname
exe "bd" . oldnum
endfunction
command! -nargs=* Newmv call s:movefunction('<args>')
After adding this to your .vimrc, you can then call this by doing
:Newmv new_file_name.py
You can also rename a file within the vim explorer move to the file you want to rename and press:
R
So that's:
- Explore
:E
- Move down to file
j
or search file pattern/
- Rename
R
- Open the new file name
<CR>
(enter).
Whether this is shorter to type or not will depend on the number of files in your directory.
This answer on vi.stackexchange suggests using :Move
from the tpope/vim-eunuch plugin.
:Move
: Rename a buffer and the file on disk simultaneously.
精彩评论