Skip to content

Instantly share code, notes, and snippets.

@SofHad
Last active December 22, 2015 09:42
Show Gist options
  • Save SofHad/e88c8f6308706e92adaa to your computer and use it in GitHub Desktop.
Save SofHad/e88c8f6308706e92adaa to your computer and use it in GitHub Desktop.
VIM Cheat sheet #vim

Cursor movement

h - move left
j - move down
k - move up
l - move right
ctrl-b - page up
ctrl-f - page down
% - jump to matching brace
w - jump by start of words (punctuation considered words)
W - jump by words (spaces separate words)
e - jump to end of words (punctuation considered words)
E - jump to end of words (no punctuation)
b - jump backward by words (punctuation considered words)
B - jump backward by words (no punctuation)
ge - jump backward to end of words (punctuation considered words)
gE - jump backward to end of words (no punctuation)
0 - (zero) start of line
^ - first non-blank character of line
$ - end of line
gg - Go to first line
GA - Jump to the bottom of the screen.
[N]G - Go To line N. No N: last line
Note: Prefix a cursor movement command with a number to repeat it. For example,
4j moves down 4 lines.

Inserting/Appending text

i - start insert mode at cursor
I - insert at the beginning of the line
a - append after the cursor
A - append at the end of the line
o - open (append) blank line below current line (no need to press return)
O - open blank line above current line
ea - append at end of word
Esc - exit insert mode

Editing

r - replace a single character (does not use insert mode)
J - join line below to the current one
cc - change (replace) an entire line
cw - change (replace) to the end of word
c$ - change (replace) to the end of line
s - delete character at cursor and subsitute text
S - delete line at cursor and substitute text (same as cc)
xp - transpose two letters (delete and paste, technically)
u - undo
ctrl-r - redo
. - repeat last command
~ - switch case
g~iw - switch case of current word
gUiw - make current word uppercase
guiw - make current word lowercase
>> - indent line one column to right
<< - indet line one column to left
== - auto-indent current line
ddp - swap current line with next
ddkP - swap current line with previous

Cut and Paste

dd - delete (cut) a line
dw - delete the current word
x - delete current character
X - delete previous character
D - delete from cursor to end of line
yy - yank (copy) a line
2yy - yank 2 lines
yw - yank word
y$ - yank to end of line
p - put (paste) the clipboard after cursor/current line
P - put (paste) before cursor/current line
:set paste - avoid unexpected effects in pasting

Visual Mode - Marking text

v - start visual mode, mark lines, then do command (such as y-yank)
V - start Linewise visual mode
o - move to other end of marked area
U - upper case of marked area
ctrl+v - start visual block mode
O - move to Other corner of block
aw - mark a word
ab - a () block (with braces)
aB - a {} block (with brackets)
ib - inner () block
iB - inner {} block
Esc - exit visual mode

Commands

> - shift right
< - shift left
c - change (replace) marked text
y - yank (copy) marked text
d - delete (cut) marked text
~ - switch case

Cut and paste

1. Place the cursor at the start of your text.
2. ma (marks the location as point 'a')
3. Place the cursor at the end of your text.
4. d'a (cuts back to location 'a')

Delete Multiple lines

  1. Press SHIFT - V to enter visual mode
  2. Select the lines to delete
  3. Press d

Exiting

:w - write (save) the file, but don't exit
:wq - write (save) and quit
:x - same as :wq
:q - quit (fails if anything has changed)
:q! - quit and throw away changes

Search/Replace

/pattern - search for pattern
?pattern - search backward for pattern
n - repeat search in same direction
N - repeat search in opposite direction
:%s/old/new/g - replace all old with new throughout file
:%s/old/new/gc - replace all old with new throughout file with confirmations

Working with multiple files

:e filename - Edit a file in a new buffer
:n **/*.pl - Open all perl files under the current directory, recursively
:tabe filename - Edit a file in a new tab (Vim7, gVim)
:bnext (or :bn) - go to next buffer
:bprev (or :bp) - go to previous buffer
:bd - delete a buffer (close a file)
:sp filename - Open a file in a new buffer and split window
ctrl-w s - Split windows
ctrl-w w - switch between windows
ctrl-w q - Quit a window
ctrl-w v - Split windows vertically

Tabs (Vim7)

:tabnew create a new tab gt - Next tab gT - Previous tab :tabr - First tab :tabl - Last tab


Window spliting

:e filename Edit filename in current window

:split filename Split the window and open filename

ctrl-w up arrow Puts cursor in top window

ctrl-w ctrl-w Puts cursor in next window

ctrl-w_ Maximise current window

ctrl-w= Gives the same size to all windows

10 ctrl-w+ Add 10 lines to current window

:vsplit file Split window vertically

:sview file Same as :split in readonly mode

:hide Close current window

:­nly Close all windows, excepted current

:b 2 Open #2 in this window


Auto-completion

Ctrl+n Ctrl+p (in insert mode)	Complete word
Ctrl+x Ctrl+l	Complete line
:set dictionary=dict	Define dict as a dictionnary
Ctrl+x Ctrl+k	Complete with dictionnary
Marks
mk	Marks current position as k
˜k	Moves cursor to mark k
d™k	Delete all until mark k
Abbreviations
:ab mail mail@provider.org	Define mail as abbreviation of mail@provider.org
Text indent
:set autoindent	Turn on auto-indent
:set smartindent	Turn on intelligent auto-indent
:set shiftwidth=4	Defines 4 spaces as indent size
ctrl-t, ctrl-d	Indent/un-indent in insert mode
>>	Indent
<<	Un-indent

Other

:Explore - Open file explorer menu :set paste - Forces VIM no to re-indent pasted code. (:set nopaste for re-enable) :set number - Show line numbers :set nonumber - Disable line numbers

How do I Cut/copy and paste using visual selection?

  • Position the cursor at the beginning of the text you want to cut/copy.
  • Press v to begin character-based visual selection, or V to select whole lines, or Ctrl-v or Ctrl-q to select a block.
  • Move the cursor to the end of the text to be cut/copied. While selecting text, you can perform searches and other advanced movement.
  • Press d (delete) to cut, or y (yank) to copy.
  • Move the cursor to the desired paste location.
  • Press p to paste after the cursor, or P to paste before.
  • Visual selection (steps 1-3) can be performed using a mouse.

If you want to change the selected text, press c instead of d or y in step 4. In a visual selection, pressing c performs a change by deleting the selected text and entering insert mode so you can type the new text.

How do I: Delete block o text

  • In command mode, type V (capital). Then move up/down to highlight the block you want deleted (all the usual movement commands work). Then remove it with x or d.

Working with tabs

Opening and closing tabsEdit When starting Vim, the -p option opens each specified file in a separate tab (up to the value of the 'tabpagemax' option). Examples:

vim -p first.txt second.txt gvim -p *.txt Once Vim has been launched, there are many commands that directly create or close tabs:

:tabedit {file} edit specified file in a new tab :tabfind {file} open a new tab with filename given, searching the 'path' to find it :tabclose close current tab :tabclose {i} close i-th tab :tabonly close all other tabs (show only the current tab) The :tabfind command uses Vim's 'path' option to determine which directories should be searched when opening the specified file. For example, the following tells Vim to look in the directory containing the current file (.), then the current directory (empty text between two commas), then each directory under the current directory ('**').

:set path=.,,** Remember, as with any Vim commands, you only need type enough characters in the command for Vim to be able to unambiguously identify it. For example, you could use :tabe and :tabf instead of :tabedit and :tabfind.

In addition to these commands, because Vim already has a plethora of commands for working with split windows, Vim provides the :tab command-line modifier, to use a new tab instead of a new window for commands that would normally split a window. For example:

:tab ball show each buffer in a tab (up to 'tabpagemax' tabs) :tab help open a new help window in its own tab page :tab drop {file} open {file} in a new tab, or jump to a window/tab containing the file if there is one :tab split copy the current window to a new tab of its own A command like :sp myfile.txt creates a new window in the current tab editing the specified file. That window can be moved to a new tab by pressing Ctrl-W T, and can be copied to a new tab with the command :tab sp (split the current window, but open the split in a new tab).

You can type Ctrl-W c to close the current window. If that window is the last window visible in a tab, the tab is also closed (if another tab page is currently open).

If the file you are editing contains the name of another file, you can put the cursor on the name and type gf to edit the file (goto file). If you type Ctrl-W gf the file is displayed in a new tab.

In gvim, you can right click the tab label bar for a popup menu with Close, New Tab, and Open Tab... items.

NavigationEdit :tabs list all tabs including their displayed windows :tabm 0 move current tab to first :tabm move current tab to last :tabm {i} move current tab to position i+1

:tabn go to next tab :tabp go to previous tab :tabfirst go to first tab :tablast go to last tab In normal mode, you can type:

gt go to next tab gT go to previous tab {i}gt go to tab in position i Note that the gt command counts from one. That means 3gt will jump to the third tab. Also note is 0gt and 1gt mean the same thing: jumping to the first tab.

When using gvim, in normal mode and in insert mode, you can type:

Ctrl-PgDn go to next tab Ctrl-PgUp go to previous tab Jumping to a specific tab with {i}gt is easier if you set up your tabline to show the tab number.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment