Turning My Favorite VS Code Feature into a Vim Macro
I've decided to begin migrating to Vim from VS Code. One of my favorite VS Code shortcuts is
Ctrl+Shift+L, which is sort of a real-time "find and replace all" feature. Vim doesn't allow true multi-cursor editing, but I managed to squeeze the important functionality I wanted out of a simple macro:
nnoremap <leader>r /<C-r><C-w><Enter>N<Esc>:%s/<C-r><C-w>//g<Bar>normal<Space>``<Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left>
This whole mess just means:
- Search for the word under the cursor (this will automatically jump to the next occurrence)
- Jump back one search result (return to the original cursor position)
- Type out a global substitution command
- Type out a "jump back to the last cursor position command (this will prevent losing one's place after performing the substitution command)
- A whole bunch of left-arrow-key's returning the cursor to be ready to type out the intended substitution.
'Til next time!