VIM cheatsheet

21 views
Skip to first unread message

Maxim Vexler

unread,
May 2, 2005, 9:29:18 AM5/2/05
to bash...@googlegroups.com
vim
Geoffrey D. Bennett

Modal editing:
- command mode
- insert mode (traditionally didn't permit movement)

Inserting:
- i - before cursor
- a - after cursor
- I - beginning of line
- A - end of line
- o - after line
- O - before line

Things that move you around:
- h j k l - character
- w W b B - words or WORDs
- e E - end of words or WORDs
- ( ) - sentence
- { } - paragraph
- H L - top or bottom of screen
- gg or G - beginning or end of file

Things that do something and need a motion:
- d - delete ("cut")
- y - yank ("copy")
- c - change (same as delete then insert)

Eg. dw, yw, cw, dW, yW, cW

- double character (dd, yy, cc) means operate by line
- capital (D, Y, C) means to the end of line (except for "y" by default :-( )

- x - delete character under cursor
- s - substitute (delete character, then insert)
- p - put ("paste") after cursor
- P - put before cursor

Everything (so far) can be prefixed with a "count". Eg.:
- 3j - go down 3 lines
- 3w - forward 3 words
- 3dw - 3x delete word
- d3w - delete 3 words
- 3d3w - 3x delete 3 words (ie. delete 9 words)
- 3yy - yank 3 lines
- 3cc - change 3 lines
- 3i ... ESC - repeat insert 3 times
- i-----ESC10a8<-----ESC

. - repeat last command

/ - search (regular expressions)
:noh - turn off highlight

.n.n.n - interactive find and replace (instead of :%s/ferd/fred/gc)

Visual mode:
- v, V, ^V - by character, line, or block
(use any movement command while in visual mode)
- o - go to other end of the highlighted text
- gv - re-highlight last highlighted text
then any delete, yank, change command (and others, like J, ~, :s, ...)

Visual-block-mode funkyness: I A r
(try ":set virtualedit=block" or "all")

:help or :help anycommand
CTRL-] - follow "link"
CTRL-T - "back"
- user guide

Windows
- CTRL-wn - new window
- CTRL-ws - split current window
- CTRL-wj (k, w, Up, Down) - move between windows
- CTRL-w+ (-, _) - resize windows
- :q or CTRL-Wc - close window
- CTRL-wv - vertical split
- vim -o * - open lots of files into windows

Or use gvim :-).

More searching:
- * or # - search forward or backwards
- Use with windows to search for one thing in another window

Numbers:
- ^A - add
- ^X - subtract
(of course, can be prefixed with a count)

Macros:
- qa - start recording macro "a"
- q - finish recording
- @a - run macro "a"
- @@ - run last macro

Eg. to create a numbered list:
- i1ESC - insert a "1"
- qa - start recording
- yyp - copy line
- ^A - increment number
- q - stop recording
- 8@a - run macro 8 times

Eg. editing lots of files:
- qa - start recording
- gg - top of file
- OblahESC - insert some text
- :wn - write, and go to next file
- q - stop recording

Registers:
- "ay - yank to register "a"
- "ap - paste from register "a"

Swapping:
- xp - characters
- dWWP - words
- ddp - lines

Editing config files:

#ServerName new.host.name:80

- yyp - copy line
- x - delete "#"
- w - go to next word
- Cblah - change text to blah

Reading from a command:
- :r!ls -la

Filtering lines (eg. delimited files):
- ! - works like d, y, c (ie. requires a motion)
- Tab-separated files: gg!Gcut -f1-3 -d'TAB'
(enter "TAB" by pressing ^V then TAB)
- Calculator: yyp!!bc<ENTER>kJi =<ESC>

Scrolling: zz, zt, zb, ^E, ^Y

Append/prepend: :%s/$/stuff/
(and with visual mode)

Tabs:
- :set ts=8 - tabstops
- :set et - expand tabs
- :set noet - don't expand tabs
- :retab! - remove or insert tabs as necessary

Indent/outdent:
- set sw=2 - shiftwidth
- < - outdent
- > - indent
Requires a motion (eg. "<<", ">>", ">}".

Formatting:
- :set tw=70
- gq - format (requires a motion)
- :set paste
- :set nopaste
- :set pastetoggle=<F11>

vimdiff

Syntax highlighting

Tab completion, eg.:
- :e ^I
- :set ^I

Command line history:
- :e (Up)

Marks:
- ma - mark this spot as "a"
- `a - go back to "a"

Use of swp files

viminfo file

vimrc file

Regexes need lots of backslashes
:%s/^\(.*\),\(.*\)/\2,\1/
instead of:
:%s/^(.*),(.*)/$2,$1/

`` - back to last "jump" (usually "big" movements)

Cool plugins:
- editing gzipped files: vi blah.txt.gz
- editing remote files: vi scp://remotehost//etc/hosts
- file explorer: vi directory

set -o vi - for vi-editing on the bash command line

:help emacs-keys - for emacs keybindings on the vi command line

Fun stuff:
- hanoi
- mandelbrot
- tetris

=================================
http://www.linuxsa.org.au/meetings/2005-01/
=================================

--
Cheers,
Maxim Vexler (hq4ever).

Do u GNU ?

hq4...@gmail.com

unread,
May 9, 2005, 12:46:31 PM5/9/05
to bash...@googlegroups.com
Cursor movement

* h - move left
* j - move down
* k - move up
* l - move right
* 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)
* 0 - (zero) start of line
* ^ - first non-blank character of line
* $ - end of line
* G - Go To command (prefix with number - 5G goes to line 5)

Note: Prefix a cursor movement command with a number to repeat it. For
example, 4j moves down 4 lines.
Insert Mode - 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
* . - repeat last command

Marking text (visual mode)

* 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
* 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

Visual commands

* > - shift right
* < - shift left
* y - yank (copy) marked text
* d - delete marked text
* ~ - switch case

Cut and Paste

* 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
* P - put (paste) before cursor
* dd - delete (cut) a line
* dw - delete (cut) the current word
* x - delete (cut) current character

Exiting

* :w - write (save) the file, but don't exit
* :wq - write (save) and quit
* :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
* :bnext (or :bn) - go to next buffer
* :bprev (of :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+ws - Split windows
* ctrl+ww - switch between windows
* ctrl+wq - Quit a window
* ctrl+wv - Split windows vertically

Another good vim commands cheatsheet:
http://www.fprintf.net/vimCheatSheet.html

vi introduction using the "cheat sheet" method:
http://www-105.ibm.com/developerworks/education.nsf/linux-onlinecourse-bytitle/8b6edc18e0ec5fe4862569b5006e5ae1

=====================================================
http://www.worldtimzone.com/res/vi.html
=====================================================

Reply all
Reply to author
Forward
0 new messages