<

Vim Tips and Tricks

This document contains 50+ Vim tips and tricks to help you become a more efficient Vim user. These tips cover everything from basic navigation to advanced editing techniques.


Opening Files

  1. Open a file to a specific line number

    vim +34 filename
    

    Opens filename and jumps to line 34.

  2. Open a file in Vim from the command line

    vim filename
    

Cursor Positioning

  1. Move cursor to the top of the screen
    Press H (capital H).

  2. Move cursor to the middle of the screen
    Press M (capital M).

  3. Move cursor to the bottom of the screen
    Press L (capital L).


Deleting and Changing Text

  1. Delete a line
    Press dd.

  2. Delete a word
    Press dw (deletes from cursor to end of word).

  3. Delete a single character
    Press dl or x.

  4. Delete multiple lines
    Press 5dd to delete 5 lines.

  5. Change a word
    Press cw (deletes the word and enters insert mode).

  6. Change a single character
    Press cl or s.

  7. Change text inside parentheses
    Press ci( or ci).

  8. Change text inside quotes
    Press ci" or ci'.

  9. Change text inside brackets
    Press ci[ or ci].


Quitting and Saving

  1. Quit without saving
    Press ZQ (capital Z followed by Q).

  2. Save and quit
    Press ZZ (capital Z twice).

  3. Save a file
    Press :w.

  4. Quit without saving changes
    Press :q!.


Navigating Lines

  1. Jump to a specific line number
    Press :100 to jump to line 100.

  2. Jump down 10 lines
    Press 10j.

  3. Jump up 5 lines
    Press 5k.

  4. Jump to matching bracket
    Press %.


Search and Replace

  1. Search for a word
    Press /word and hit Enter.

  2. Search and replace globally

    :%s/old/new/g
    

    Replaces old with new throughout the file.

  3. Search for two words

    /word1\_.*word2
    
  4. Clear search highlights
    Press :noh.

  5. Search for the word under the cursor
    Press * to search forward, # to search backward.


Copying and Pasting

  1. Yank (copy) a line
    Press yy.

  2. Yank a word
    Press yw.

  3. Yank a single character
    Press yl.

  4. Paste after the cursor
    Press p.

  5. Paste before the cursor
    Press P.


Working with Buffers

  1. Switch between buffers
    Press Ctrl + o to go back, Ctrl + i to go forward.

  2. Open a file in a new buffer

    :e filename
    
  3. Split window horizontally

    :sp filename
    
  4. Split window vertically

    :vs filename
    
  5. Close the current buffer
    Press :q.


File Management

  1. Rename a file

    :saveas newfilename
    
  2. Move a file to a different directory

    :w /path/to/newlocation/filename
    
  3. Open a file manager in Vim

    :Ex
    

Advanced Editing

  1. Insert output of a command into the file

    :r !command
    

    Example: :r !ls inserts the output of ls.

  2. Sort lines
    In visual mode, select lines and press :!sort.

  3. Repeat the last command
    Press . (period).

  4. Auto-complete in insert mode
    Press Ctrl + n.

  5. Delete blank lines

    :g/^$/d
    

Visual Mode

  1. Select character by character
    Press v.

  2. Select line by line
    Press V (capital V).

  3. Select block (column-wise)
    Press Ctrl + v.

  4. Select inside parentheses
    Press vi( or vi).

  5. Select inside quotes
    Press vi" or vi'.

  6. Select inside brackets
    Press vi[ or vi].

  7. Select a paragraph
    Press vip.


Miscellaneous Tips

  1. Undo changes
    Press u.

  2. Redo changes
    Press Ctrl + r.

  3. Reload Vim configuration

    :source ~/.vimrc
    
  4. Search for lines containing a word

    :g/word
    
  5. Search for lines containing two words

    :g/word1.*word2
    
  6. Insert current date and time

    :r !date
    
  7. Run the current line as a shell command

    :!!sh
    
  8. Switch two characters
    Press xp.


Conclusion

These tips and tricks will help you become more proficient in Vim. Practice them regularly to build muscle memory and improve your workflow.