b.k.

Vim

Note: Items with multiple keystrokes seperated by a comma should by typed one at a time in that order. ex: g, g – Press “g” twice in a row.

Items with multiple keystrokes joined with a plus should be pressed at the same time. ex: Shift + g – Press shift & “g” to create an uppercase G.

Basics:

Open a document:

vim filename.txt

Modes: Command and Edit

Command mode is the default, this mode allows you to use keys for commands/movement.

Edit(insert) mode allows you to add text to a file.

When in Command mode type one of the following to enter edit mode:

  • i - Before the current character
  • Shift + i - At the beginning of the line
  • a - After the current character
  • Shift + a - After the current line.
  • o - Open a new line below the current line.
  • Shift + o - Open a new line above the current line.

Press Esc to exit edit mode, and return to command mode.

Note: Pressing Esc after typing something will become habit after a while. Doing this outside of vim will cause you to accidentally close an IM window at some point.

Command Mode Commands

All of the following are used in command mode.

Press Esc to exit edit mode.

Moving around in command mode.

Basic “Arrow” keys:

  • h - move cursor left one character
  • j - move cursor down one line
  • k - move cursor up one line
  • l - move cursor right one character

Larger movements:

  • b - Back one “word” (will stop @ punctuation)
  • Shift + b - Back one “word” (will NOT stop @ punctuation)
  • w - Forward one “word” (will stop @ punctuation)
  • Shift + w - Forward one “word” (will NOT stop @ punctuation)
  • \^ - Beginning of line
  • $ - End of line
  • g, g - Top of document
  • Shift + g - bottom of document

Deleting

Deleting something will cut that item into the paste buffer so it can be pasted elsewhere.

  • x - delete current selection
  • d, w - delete to end of current word
  • d, $ - delete to end of line
  • d, d - delete the selected line

Copy (yank)

Note: When you copy in Vi this is a different clipboard than your desktop clipboard.

  • y - copy current selection
  • y, w - copy to end of current word
  • y, $ - copy to end of line
  • y, y - copy the selected line

Paste (put)

  • p - paste clipboard after cursor
  • Shift + p - paste clipboard before cursor

Undo/Redo

  • u - Undo
  • Cntrl + r - Redo

Finding & Replacing Things

Find:

  • / + string_to_search_for - “/cat” will find instances of “cat”
  • * Find next occurance of word currently under cursor.

Replace:

  • :%s/FIND/REPLACE/g - global find & replace for string “FIND” replace with “REPLACE”

Selecting Text

After starting a selection use Vim command mode keys to move around.

Once text is selected use commands to alter text.

  • Shift + v - Start line select
  • Cntrl + v - start character select

Most commands can be performed after selecting text. This can be useful for things like:

  • deleting a word, line, or block of text (select & then d or x)

Quiting Vim

  • :q - quit without saving
  • :q! - quite without saving (ignore warnings)
  • :wq - save and quit
  • :wq! - save and quit (ignore warnings)
  • Shift + z, Shift + z - save and quit

Using VimDiff

Open a new vim instance in diff mode:

vimdiff <file1> <file2>

Use the normal movement & viewport switching keys. Update the file as needed to get them into the desired state.

With your cursor over a difference you can use the commands below:

  • d, o - Diff Obtain (pull diff value from other viewport)
  • d, p - Diff Put (push diff value to other viewport)

Sort

Sort the current selected lines (or entire file if no selection).

:sort

Remove duplicates lines on sort

:sort u

Opening multple documents/screens/viewports

  • :sp <filepath/name> - Split window horizontally to open another document
  • :vsp <filepath/name> - Split window vertically to open another document

Moving between windows

  • Ctrl + w, Cntrl + w - move to next viewport
  • Ctrl + w, j - moves one viewport down.
  • Ctrl + w, k - moves one viewport up.
  • Ctrl + w, h - moves one viewport to the left.
  • Ctrl + w, l - moves one viewport to the right.
  • Ctrl + w, = - tells Vim to resize viewports to be of equal size.
  • Ctrl + w, - - reduce active viewport by one line.
  • Ctrl + w, + - increase active viewport by one line.

Tabs

  • :tabnew - Open a new tab
  • :tabn - Next Tab
  • :tabp - Previous Tab
  • g, t - Go to the next tabpage
  • {count}g, t - Go to tabpage #{count}

Indent/Dedent Code Blocks

Blocks of code can be moved/tabbed left/right as a group.

Place your cursor on a line to be moved and press >, > to move the line right one “tab”, or <, < to move the line left one “tab”.

Select lines to be moved and press > to move right, or < to move left.

  • Quickly format a whole bunch of code by selecting the block & then pressing = this will auto indent everything.

Performing actions multiple times

Many commands can be preceeded by a count in order to perform them multiple times. For example to delete 3 lines type 3, d, d.

The previous command can also be redone by pressing .

Matching Brackets

Use the % key to jump from an open or close bracket (parenthesis, square, or curly), to its matching close or open bracket.

Macros

Macros can be recorded to replay multiple commands.

  1. press q to start recording
  2. press a character a-z0-9(A-Z?) to assign the macro to that character
  3. enter the commands that you want to store
  4. press q to stop recording

To Replay type @, A (A is the character you chose in step 2 above).

To replay 7 times do 7, @, A

Folding

  • Create fold from selection: z, f
  • Delete fold (does not delete text in fold): z, d

Shell Foo

  • Run the text from the current line in bash:
    • :.w !bash