Reputation: 154484
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 realize 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)
Upvotes: 1
Views: 1476
Reputation: 11399
You can also rename a file within the vim explorer move to the file you want to rename and press:
R
So that's:
:E
j
or search file pattern /
R
<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.
Upvotes: 1
Reputation: 1387
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
Upvotes: 2