0

I wanted to be able to move/rename a currently open file using netrw. The typical concerns are:

  1. Closing the old buffer
  2. 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 continue
  • redraw! allowed me to actually see netrw
  • added enew<bar>bw #<bar> to delete the old buffer
  • 2
    You’ll need nmap in this case so netrw’s keybindings take effect – D. Ben Knoble Apr 20 at 13:30
  • 1
    "I wanted to try it using netrw in a single keybind." That's quite masochistic... Why would you want to do that? Netrw's rename operation just uses call rename(...) under the hood (see here. Why not just use that directly? It's so much simpler and way more robust! – filbranden Apr 20 at 15:12
  • @D.BenKnoble Yes, it appears that nmap fixes the issue of the keys not being sent, regardless of whether redraw! 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. – brianclements Apr 20 at 21:11
1

I didn't follow the same approach as you did to get to the file, but I ran into the same problem. For me, a simple redraw! made it work.

Here's my final command:

nmap <LEADER>Sm :let fn=expand('%:t') <BAR> Explore %:p:h <BAR> call search(fn) <BAR> redraw!<CR>mfR
| improve this answer | |
  • I ended up using your method after all anyway, less buggy than mine. Using args is a mess. – brianclements Apr 20 at 21:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.