How to (safely) swap the contents of two files opened in (neo)vim buffers

Raise your hand if you’ve been here before: you have file1 open in a vim or neovim buffer and you want to “fork” its contents over to file2, but you need to reference file1 while you do so. So you do the obvious: you open a split buffer with :sp or :vsp, run a quick :saveas file2 then hack away at the file to make the changes you want followed by :w (or whatever shortcut you have mapped to the same) and call it a day… only to realize that you were in the wrong split and that you’ve accidentally switched file1 and file2 around?

Continue reading

Increment only numbers matching regex in Vim

Long-time vim or neovim users are probably already aware that visually selecting a block of text then pressing CTRL + A in vim will result in any numbers in the selected block of text being incremented by 1. This works even if the block contains non-numeric text: each group of digits gets treated as a number and is incremented.1

For example, here’s a video that shows what happens when you select some text in vim and then use CTRL + A to increment the values:

Continue reading


  1. To be pedantic, only the first group-of-digits/number on each line gets incremented; like many vim commands this only works on the first match per line of text unless some sort /g global modifier is used.