I wanted to be able to move/rename a currently open file using netrw. The typical concerns are:
- Closing the old buffer
- Actually moving to preserve file attributes (so no :saveas)
I'm aware of some of the plugins and custom functions for this purpose but I wanted to try it using netrw in a single keybind.
Here's what I have so far:
nnoremap <silent> <leader>Sm :argadd<CR>:enew<bar>bd #<CR>:1argdo Explore<CR>
This successfully adds the current file to arglist, opens temporary new buffer, deletes the previous old file buffer, and opens netrw to the dir of the file in arglist.
From there, what I have to do manually is mA
to mark the file from the arglist, then R
to move/rename.
How can I append the last two key-presses to the original mapping? The following does not work for example:
nnoremap <silent> <leader>Sm :argadd<CR>:enew<bar>bd #<CR>:1argdo Explore<CR>mAR
Once netrw is entered, the key-presses seem to stop.
Solution
nmap <leader>Sm :let fn=expand('%:t')<bar>enew<bar>bw #<bar>Explore %:p:h<bar>call search(expand(fn))<bar>redraw!<CR>mfR
nmap
allowed the key-presses to continueredraw!
allowed me to actually seenetrw
- added
enew<bar>bw #<bar>
to delete the old buffer
call rename(...)
under the hood (see here. Why not just use that directly? It's so much simpler and way more robust!nmap
fixes the issue of the keys not being sent, regardless of whetherredraw!
is used, which only effects whether I see the directory contents in the netrw window or not. @filbranden masochistic?....this is vim, after all! And you're right about reusing the plumbing, but I wanted to avoid recreating what I know netrw already did on the surface; use it's porcelain, and even finish the move/rename IN netrw itself for visual confirmation.