Home Vim Cheat Sheet
Post
Cancel

Vim Cheat Sheet

I used Vim every now and then when I needed to do a quick file editing in the terminal. I was reluctant to take the learning curve until very recently. My IDE of choice is struggling with a relatively large codebase. I found many of my colleagues flying through source code using Vim. This changed my position on the learning curve. Now, I the potential IDE legginess justifies the learning time. Besides, Vim is cool.

Moving between words and characters

Motions

1
2
3
4
5
6
** To move the cursor, press the h,j,k,l keys as indicated. **
         ^
         k              Hint:  The h key is at the left and moves left.
   < h       l >               The l key is at the right and moves right.
         j                     The j key looks like a down arrow.
         v
  • w: Move to the next word
  • b: Move to the previous word
  • e: Move to the end of current word
  • 0: Move to the beginning of the line
  • $: Move to the end of the line

  • f๐’ธ: Move on the first ๐’ธ character
  • t๐’ธ: Move before the first ๐’ธ character
  • F๐’ธ: Move back on the first ๐’ธ character
  • T๐’ธ: Move back before the first ๐’ธ character
    • Use ; to repeat the last f, F, t or T
    • Use , to inverse-repeat direction of the last f, F, t or `T
  • %: Find the matching (,),],[,{ or }

  • ^+o: Back - Move to previous courser position (i.e. After jumping to a search result)
  • ^+i: Forward - Move to next courser position (i.e. after jumping to a search result)

Modes

As developers we tend to spend most of our time navigating through files and making smallish edits. Vim normal mode is the default mode the editor starts in. In that mode one canโ€™t write code directly to the editor, but instead move around the file. This is in contrast to insert mode where we type in the text or code directly, which is the normal behaviour of most other editors. This is why Vim is called modal text editor.

It is a good practice to spend most of our time in normal mode and use insert mode only when absolutely necessary.

In addition there is visual mode, which one can activate using v key. It highlights the motions that you are taking. You can consider this as a visual select mode as well. Use it sparingly because it is harder to automate selections this way compared to ones accomplished using motions in normal mode. While we are on the topic, using V turn visual mode to be more of line selection tool.

visual block mode is a mode where one can select a column or a rectangular block of text. This could be useful for an example to add * at the beginning of few lines and turn them into a list in markdown for example.

Moving around the file/page

  • H: Move to the top of the window (high)
  • M: Move to the middle of the window (middle)
  • L: Move to the bottom of the window (low)

  • zz: Bring the cursor line to the middle of the window
  • zt: Bring the cursor line to the top of the window
  • zb: Bring the cursor line to the bottom of the window

  • ^+d: Page Down
  • ^+u: Page Up

  • gg : Move to the beginning of the file
  • G : Move to the end of the file
  • ^+g : Information on cursor location in file
  • โ“+G: Move to the line number โ“

Undo and Redo

  • u : Undo last command
  • U : Undo all changes in the current line
  • ^+R: Redo
  • . : Repeat last command in the current context

Selecting, Copying and Pasting Text

  • v: Start visual selection (use motion operators to select text) then run a command against selected text. If you type :, you will get a prompt starting with โ€˜<,โ€™> to reference the selection, which you can save to another file for an example if you like
  • y: Copy (yank) selected text
  • p: Put (paste) copied text
  • /: Search Forward
  • ?: Search Backward
    • \c : add to the end of search to temporarily ignore case
    • :set ic : Ignore case for all search and replace
    • :set noic : unset ignore case
    • :set hls : Highlight search results
    • :set nohls: unset highlight search results
    • :set is : Incremental Search
    • :Set nois : unset incremental search
  • n: Next instance
  • N: Previous instance

Operations

  • x : delete letter under courser
  • i : insert
  • I : insert in the beginning of the line
  • a : append
  • A : append at the end of the line
  • o : open line - insert line below and move to insert mode
  • O : open line above - insert line above and move to insert mode
  • r : Replace the letter under courser (press r + the letter you want to add)
  • R : Overwriting insert
  • d : delete
  • dd: delete a line (cut)
  • p : put last deleted line (paste last deleted line)
  • c : change (combination of delete and insert)

Repeating a command twice, in most cases, applies the command to the current line

Objects

  • aw: Around word including its trailing space
    • daw: Deletes the current word including its trailing space
  • iw: Inner word, exactly the word without including any spaces
    • diw: Deletes the exact word without any spaces around it
  • ip: Inner paragraph
  • it: Inner tag
  • i": Within quotes
  • i(: Within brackets

Operations and Motions in action

1
2
3
4
5
6
7
The format for a change command is:
               operator   [number]   motion
     where:
       operator - is what to do, such as  d  for delete
       [number] - is an optional count to repeat the motion
       motion   - moves over the text to operate on, such as  w (word),
                  $ (to the end of line), etc.
  • dw : delete word
  • d$ : delete until the end of the line
  • 3w : Repeat move word motion three times
  • 3dw: Repeat both the operation and motion three time
  • 3dd: Delete three lines

Commands (press : first)

  • q! : quit without saving
  • w fileNem : Save As
  • wq : save (write) and exit
  • s/old/new : Substitute first instance in line
  • s/old/new/g : Substitute all instances in line
  • โ“,โ“s/old/new/g : Substitute all instances between two line numbers โ“ and โ“
  • %s/old/new/g : Substitute all instances in file
  • %s/old/new/gc : Prompt to Substitute all instances in file (will prompt in every instance)
  • !ls : Run bash command ls.
  • r : Read or Redirect into the current line, could be followed by a file name (it will be merged in) or a bash command.
  • r !ls : will read and redirect the output of the command where the courser is

Window management

  • To get a new window, go to command mode : then
    • new : New file in a new horizontal window
    • vnew : New file in a new Vertical Window
    • split or sp : Edit file in a new Vertical Window
    • vsplit or vsp: Edit file in a new Vertical Window
    • tabnew : New file in a new tab
    • tabedit : Edit file in a new tab
  • To Manage windows start with ^+w then
    • h, j, k and l (directions) to move between windows
    • H, J, K and L (directions) to move windows around
    • - or + to resize the window vertically (lines)
    • < or > to resize the window horizontally (columns)
    • = Restore windows to default configurations (roughly equal spaces)
    • T Break a window into a new tab
  • Managing tabs
    • gt: Move to next tab
    • gT: Move to previous tab

Jetbrains IDEs Vim Plugin and macOS key repeating

You can enable Vim mode in Jetbrains IDEs. Install the plugin and make sure to accept key repetition dialogue when the IDE restarts, otherwise when you keep your finger down on h with the intent to continuously move left, macOS will instead show accented versions of the letter h and in my experience the IDE just freezes and panics, or at least the Vim plugin does. If for some reason you want to disable the key repetition you can run the following command in the console.

1
defaults write -g ApplePressAndHoldEnabled -bool false

You can still find those accented characters if you want by using ^+โŒ˜+Space keyboard shortcut which will show a pop-up to search for special characters and other goodies ๐Ÿ˜‰.

For more information on enabling or disabling key repeating on macOS you can check How-To Geek.


Resources

This post is licensed under CC BY 4.0 by the author.
Contents

Metrics and Monitoring

Vim Configuration

Comments powered by Disqus.