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

VIM Cheatsheets

http://www.tuxfiles.org/linuxhelp/vimcheat.html http://www.viemu.com/vi-vim-cheat-sheet.gif http://vim.rtorr.com/

Tabs & windows/viewports

  • Multiple Tabs can be opened
  • Each tab can be split into multiple windows

Vim viewport keybinding quick reference

http://vimdoc.sourceforge.net/htmldoc/windows.html https://www.linux.com/learn/tutorials/442415-vim-tips-using-viewports :sp will split the Vim window horizontally. Can be written out entirely as :split . :vsp will split the Vim window vertically. Can be written out as :vsplit . Ctrl-w Ctrl-w moves between Vim viewports. 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. Ctrl-w q will close the active window. Ctrl-w r will rotate windows to the right. Ctrl-w R will rotate windows to the left

Tab Pages

http://vimdoc.sourceforge.net/htmldoc/tabpage.html When starting Vim “vim -p filename …” opens each file argument in a separate tab page

create a new tabpage

  • :tabnew

swtich to another tabpage

  • :tabn[ext]
  • :tabp[revious]
  • gt ○ go to the next tabpage
  • {count}gt ○ go to tabpage #{count}

Close all tabs :qa

Swap panes from horizontal <-> Vertical

To change two vertically split windows to horizonally split

Ctrl-w t Ctrl-w K

Horizontally to vertically:

Ctrl-w t Ctrl-w H

Explanations:

Ctrl-w t makes the first (topleft) window current Ctrl-w K moves the current window to full-width at the very top Ctrl-w H moves the current window to full-height at far left

Macros

http://robots.thoughtbot.com/post/15348543318/vim-macros-and-you 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

refresh/reload document from disk

  • :e

visual diff of files

  • vimdiff <file1> <file2>

Sort lines

lines can be selected before hand & they will appear in “range”

  • :{range}sort u

Set colors in VIMDIff so BG & FG aren’t the same

  • hi clear Normal
  • set bg=dark
  • hi clear

jumping around a document

Jump back & forth between the current & previous area

  • `` (double backtick)

Mark the current location

  • ma

Jump to mark

  • `a

Select text

  • Shift-v
    • Select whole lines of text
  • Cntrl-v
    • Select in block mode
  • gv
    • re-select last selection (useful when running multiple commands on one selection)

vimdiff commands

  • ]c : - next difference
  • [c : - previous difference
  • do - diff obtain
  • dp - diff put
  • zo - open folded text
  • zc - close folded text
  • :diffupdate - re-scan the files for differences

ignore whitespace

  • set diffopt+=iwhite

indent code block

  • place cursor on open or close character (paren, curly or square)
  • type: ‘>%’ OR ‘<%’
  • to increase or decrease indent of block

find & replace with backreferences

  • %s:\(.*\):\1\t\1:g

Insert text from named file below cursor

  • :r filename

Turn on soft line wrap

  • :set wrap

Regular Expressions

find this or that

  • /red\|green\|blue

    replace this or that with new

  • :%s/red\|green\|blue/purple/g

Setting searches to case insensitive or case sensitive

turn on ignore case to ignore case entirely

  • :set ignorecase

    turn on smartcase (searching in lowercase ignores case, adding in an uppercase matches case)

    smartcase requires ignorecase to be set

  • :set smartcase

    or use the \c \C options in any of the modes

    /copyright “ Case insensitive /Copyright “ Case sensitive /copyright\C “ Case sensitive /Copyright\c “ Case insensitive

Marks (bookmarks)

http://vim.wikia.com/wiki/Using_marks

Marks are for a specific line and column.

Apostrophe ' jumps to the beginning of the line of the mark. Backtick ` jumps to the line & column of the mark.

  • Each file can have mark a – use a lowercase mark to jump within a file.
  • There is only one file mark A – use an uppercase mark to jump between files. (you do not need to open the file prior to jumping to the mark)

Command Description ma set mark a at current cursor location ‘a jump to line of mark a (first non-blank character in line) a jump to position (line and column) of mark a d'a delete from current line to line of mark a da delete from current cursor position to position of mark a c’a change text from current line to line of mark a ya yank text to unnamed buffer from cursor to position of mark a :marks list all the current marks :marks aB list marks a, B Commands like d'a operate "linewise" and include the start and end lines. Commands like da operate “characterwise” and include the start but not the end character. It is possible to navigate between lowercase marks: Command Description ]’ jump to next line with a lowercase mark [’ jump to previous line with a lowercase mark ] jump to next lowercase mark [ jump to previous lowercase mark The above commands take a count. For example, 5] jumps to the fifth mark after the cursor. Special marks Vim has some special marks which it sets automatically. Here are some of the most useful: Command Description . jump to position where last change occurred in current buffer " jump to position where last exited current buffer 0 jump to position in last file edited (when exited Vim) 1 like 0 but the previous file (also 2 etc) '' jump back (to line in current buffer where jumped from) jump back (to position in current buffer where jumped from) [ or ] jump to beginning/end of previously changed or yanked text < or `> jump to beginning/end of last visual selection See the full list at :help ’[ and following.

Deleting marks

If you delete a line containing a mark, the mark is also deleted. If you wipeout a buffer (command :bw), all marks for the buffer are deleted. The :delmarks command (abbreviated as :delm) may be used to delete specified marks. Command Description :delmarks a delete mark a :delmarks a-d delete marks a, b, c, d :delmarks abxy delete marks a, b, x, y :delmarks aA delete marks a, A :delmarks! delete all lowercase marks for the current buffer (a-z)

From http://vim.wikia.com/wiki/Using_marks

Jump to matching brace/bracket/paren

% - typing the percent symbol while a brace/bracket/paren is highlighted will jump to the matching item

RegEx

Reuse Put: source ~/.regexlist.vim into your vimrc. Do the search. Decide you might want to use that one again. Type this literally: :sp ~/.regexlist.vim olet MyRegExName = '/' :w :so % :q Where is CtrlR, is ENTER and is ESC. Next time you want to use it: /=MyRegExName Ctrl-R is your friend! Ctrl-R followed by / pulls up the last search. Ctrl-R followed by = allows you to enter an expression.

From <https://stackoverflow.com/questions/2201174/save-commonly-used-regex-patterns-in-vim> 

Open Multiple Files at once

To edit all files in the current folder, use: vim *

To edit all files in tabs, use: vim -p *

To edit all files in horizontally split windows, use: vim -o *

diff two already open files

Open the files in vertical panes & then run command ˋ:diffthisˋ in each pane.

turn off diff

:diffoff

Update Diff

:diffupdate

highlight duplicate lines

:syn clear Repeat | g/^\(.*\)\n\ze\%(.*\n\)*\1$/exe 'syn match Repeat "^' . escape(getline('.'), '".\^$*[]') . '$"' | nohlsearch

From: https://stackoverflow.com/questions/1268032/how-can-i-mark-highlight-duplicate-lines-in-vi-editor