Advanced Vim Motions That I Learned Over The Years

    Published on

    As a front-end web developer, knowing Vim makes your workflow much smoother. For my normal work, I have found some quite useful Vim motions throughout time. This page will walk over five techniques to raise your Vim game.

    Instant Jump

    One of the most useful motions I've learned is the gi command. This command returns you into insert mode right away, back to where the cursor was last in insert mode. When you have to pick back up editing from where you left off, it's rather convenient.

    Efficient Word Deletion

    When it comes to deleting words, I've found that daw (delete a word) is the most efficient command. While dbx and bdw achieve similar results, daw requires fewer keystrokes and works more consistently with the . (repeat) command.

    Powerful Substitutions

    Vim's substitution command is incredibly powerful. Here are some advanced techniques I use regularly:

    1. vip:s/$/;/ - This selects a whole paragraph using text objects and adds a semicolon at the end of each line.
    2. :s/>/ > /g - This adds spaces around ">" symbols in the current line. The g flag affects all occurrences in the line.
    3. :s/content/copy/gc - The c flag prompts for confirmation before each substitution.

    Mastering the Dot Command

    The dot command (.) is a powerful tool for repeating the last change. I've found it particularly useful in combination with macros and visual mode selections.

    Macros with Dot Command

    When creating macros, I make sure to choose commands that raise an error if they can't be executed. This ensures that Vim aborts the macro when it's no longer applicable, preventing unintended changes.

    Dot Command in Visual Mode

    To make the dot command work in visual mode, I use this remapping:

    xnoremap . :normal .<CR>
    

    This allows me to apply changes to multiple lines easily.

    Operator Pending vs Visual Mode

    I've learned to prefer operator pending mode over visual mode for many operations. For example:

    These commands work more intelligently with the dot command compared to their visual mode counterparts.

    Advanced Search Techniques

    Here are some search techniques I've found incredibly useful:

    1. :s/lang/&guage/g - This appends "guage" to all occurrences of "lang".
    2. /hello/e - This places the cursor at the end of each "hello" match when using n to navigate.
    3. //e - This moves the cursor to the end of the current search match.
    4. //s or // - This moves the cursor to the start of the current search match.

    Parallel Macros

    Finally, here's a fantastic remapping I use for applying macros to multiple lines:

    xnoremap @ :<C-u>call ExecuteMacroOverVisualRange()<CR>
    
    function! ExecuteMacroOverVisualRange()
      echo "@".getcmdline()
      execute ":'<,'>normal @".nr2char(getchar())
    endfunction
    

    This allows me to run macros on selected ranges by pressing @ and then the register key.

    Remember, the key to mastering Vim is constant practice and exploration. Always look for ways to normalize your actions, act efficiently, and advance your skills. Happy Vimming!