Skip to main content

Posts

Showing posts from April, 2018

Vim - Replace word under cursor

The way you edit text in vim is pretty much by running commands, you have a few shortcuts here and there, but most of the time is just running commands. While I really like this approach, I do miss a replace word under cursor shortcut. To do that in "plain" vim, you have to type: :%s/<C-r><C-w>/type_new_word_here/gc (Where <C-r> stands for Ctrl+R, and <C-w> for Ctrl+W. That's a bit of a stretch on the mac where you only have a Left Ctrl key). What the previous command does is to look for the word under cursor in the current file and ask you if you want to replace it with the word you have entered in the second part of the command. The flag gc  tells vim that the action is global and that it must to ask you for confirmation before replacing matches. There is not a whole lotta of typing but we can do better. The good news is that with a bit of vimscript, you can avoid acrobatic motions and start replacing words with a copule of keystroke