Is there a way to move a file within Vim? E.g. I opened a file foo/bar.txt in Vim. I know 2 ways to move this file:
First solution:
:bd bar.txt
mv foo/bar.txt foo/bar2.txt
:e foo/bar2.txt
Second solution:
But these two solutions are embarrassing. I know, there is a plugin for renaming files vim-enuch, but isn't there a Vim way for performing such basic functionality?
You can enter :pwd to display the current working directory. This is where your file will be saved if simply enter :w filename . You can change the working directory with :cd path/to/new/directory .
Type "gg" in command mode. This brings the cursor to the first line.
Then change directory, select file(s) (see netrw-mf), and press "mc". The copy is done from the current window (where one does the mf) to the target. If one does not have a target directory set with netrw-mt, then netrw will query you for a directory to copy to.
In normal mode or in insert mode, press Alt-j to move the current line down, or press Alt-k to move the current line up. After visually selecting a block of lines (for example, by pressing V then moving the cursor down), press Alt-j to move the whole block down, or press Alt-k to move the block up.
You could also use netrw (the default file explorer) rename functionality.
:E
/
(e.g. /bar.txt
)R
. You will then be prompted for a new filepath. When done entering the filepath hit <CR>
<CR>
While this solution may not be as quick as using vim-eunch, it does allow you to see the project's structure as you rename the file. This will also allow you to move multiple files at once.
For further reading run :help netrw-move
There is no atomic way to move a file like that, but this should be close:
function! MoveFile(newspec) let old = expand('%') " could be improved: if (old == a:newspec) return 0 endif exe 'sav' fnameescape(a:newspec) call delete(old) endfunction command! -nargs=1 -complete=file -bar MoveFile call MoveFile('<args>')
Now you could say:
:MoveFile file2.txt
To rename to file2.txt
:MoveFile %.0
to move file2.txt to file2.txt.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With