You are on page 1of 13

The list of Vim commands

Working with files


:e filename Open a new file. You can use the Tab key for automatic file name completion, just like at the shell command prompt. :w filename Save changes to a file. If you don't specify a file name, Vim saves as the file name you were editing. For saving the file under a different name, specify the file name. :q :q! :wq Quit Vim. If you have unsaved changes, Vim refuses to exit. Exit Vim without saving changes. Write the file and exit.

:x Almost the same as :wq, write the file and exit if you've made changes to the file. If you haven't made any changes to the file, Vim exits without writing the file.

Moving around in the file


These Vim commands and keys work both in command mode and visual mode. j or Up Arrow k or Down Arrow h or Left Arrow l or Right Arrow e E b B 0 ^ $ H Move the cursor up one line. Move the cursor Down one line. Move the cursor Left one character. Move the cursor Right one character.

To the end of a word. To the end of a whitespace-delimited word. To the beginning of a word. To the beginning of a whitespace-delimited word. To the beginning of a line. To the first non-whitespace character of a line. To the end of a line. To the first line of the screen.

M L :n

To the middle line of the screen. To the the last line of the screen. Jump to line number n. For example, to jump to line 42, you'd type :42

Inserting and overwriting text


i I a A o O C Insert before cursor. Insert to the start of the current line. Append after cursor. Append to the end of the current line. Open a new line below and insert. Open a new line above and insert. Change the rest of the current line.

r Overwrite one character. After overwriting the single character, go back to command mode. R Enter insert mode but replace characters rather than inserting.

The ESC key Exit insert/overwrite mode and go back to command mode.

Deleting text
x X Delete characters under the cursor. Delete characters before the cursor. Delete the current line.

dd or :d

Entering visual mode v Start highlighting characters. Use the normal movement keys and commands to select text for highlighting. V Start highlighting lines.

The ESC key Exit visual mode and return to command mode. Editing blocks of text Note: the Vim commands marked with (V) work in visual mode, when you've selected some text. The other commands work in the command mode, when

you haven't selected any text. ~ Change the case of characters. This works both in visual and command mode. In visual mode, change the case of highlighted characters. In command mode, change the case of the character uder cursor. > (V) Shift right (indent). < (V) Shift left (de-indent). c (V) Change the highlighted text. y (V) Yank the highlighted text. In Windows terms, "copy the selected text to clipboard." d (V) Delete the highlighted text. In Windows terms, "cut the selected text to clipboard." yy or :y or Y dd or :d Yank the current line. You don't need to highlight it first.

Delete the current line. Again, you don't need to highlight it first.

p Put the text you yanked or deleted. In Windows terms, "paste the contents of the clipboard". Put characters after the cursor. Put lines below the current line. P Put characters before the cursor. Put lines above the current line.

Undo and redo


u U Undo the last action. Undo all the latest changes that were made to the current line. Redo.

Ctrl + r

Search
/pattern n N Search the file for pattern. Scan for next search match in the same direction. Scan for next search match but opposite direction.

Replace
:rs/foo/bar/a Substitute foo with bar. r determines the range and a determines the arguments.

The range (r) can be nothing number % Work on current line only. Work on the line whose number you give.

The whole file.

Arguments (a) can be g Replace all occurrences in the line. Without this, Vim replaces only the first occurrences in each line. i I Ignore case for the search pattern. Don't ignore case.

c Confirm each substitution. You can type y to substitute this match, n to skip this match, a to substitute this and all the remaining matches ("Yes to all"), and q to quit substitution. Examples :452s/foo/bar/ number 452. Replace the first occurrence of the word foo with bar on line

:s/foo/bar/g Replace every occurrence of the word foo with bar on current line. :%s/foo/bar/g whole file. Replace every occurrence of the word foo with bar in the

:%s/foo/bar/gi The same as above, but ignore the case of the pattern you want to substitute. This replaces foo, FOO, Foo, and so on. :%s/foo/bar/gc Confirm every substitution.

:%s/foo/bar/cFor each line on the file, replace the first occurrence of foo with bar and confirm every substitution.

VIM Editor Commands


Vim is an editor to create or edit a text file. There are two modes in vim. One is the command mode and another is the insert mode. In the command mode, user can move around the file, delete text, etc.

In the insert mode, user can insert text. Changing mode from one to another From command mode to insert mode From insert mode to command mode Some useful commands for VIM type a/A/i/I/o/O ( see details below) type Esc (escape key)

Text Entry Commands (Used to start text entry)


a Append text following current cursor position A Append text to the end of current line i Insert text before the current cursor position I Insert text at the beginning of the cursor line o Open up a new line following the current line and add text there O Open up a new line in front of the current line and add text there The following commands are used only in the commands mode.

Cursor Movement Commands


h Moves the cursor one character to the left l Moves the cursor one character to the right k Moves the cursor up one line j Moves the cursor down one line nG or :n Cursor goes to the specified (n) line (ex. 10G goes to line 10) ^F (CTRl F) Forward screenful ^B Backward screenful ^f One page forward ^b One page backward ^U Up half screenful ^D Down half screenful

$ Move cursor to the end of current line 0 (zero) Move cursor to the beginning of current line w Forward one word b Backward one word

Exit Commands
:wq Write file to disk and quit the editor :q! Quit (no warning) :q Quit (a warning is printed if a modified file has not been saved) ZZ Save workspace and quit the editor (same as :wq) : 10,25 w temp write lines 10 through 25 into file named temp. Of course, other line numbers can be used. (Use :f to find out the line numbers you want.

Text Deletion Commands


x Delete character dw Delete word from cursor on db Delete word backward dd Delete line d$ Delete to end of line d^ (d caret, not CTRL d) Delete to beginning of line Yank (has most of the options of delete)-- VI's copy commmand yy yank current line y$ yank to end of current line from cursor yw yank from cursor to end of current word 5yy yank, for example, 5 lines Paste (used after delete or yank to recover lines.) p paste below cursor

P paste above cursor "2p paste from buffer 2 (there are 9) u Undo last change U Restore line J Join next line down to the end of the current line

File Manipulation Commands


:w Write workspace to original file :w filename Write workspace to named file :e filename Start editing a new file :r filename Read contents of a file to the workspace To create a page break, while in the insert mode, press the CTRL key And l. ^L will appear in your text and will cause the printer to start A new page.

Other Useful Commands


Most commands can be repeated n times by typing a number, n, before the command. For example 10dd means delete 10 lines. . Repeat last command cw Change current word to a new word r Replace one character at the cursor position R Begin overstrike or replace mode use ESC key to exit :/ pattern Search forward for the pattern :? pattern Search backward for the pattern n (used after either of the 2 search commands above to continue to find next occurrence of the pattern. :g/pat1/s//pat2/g replace every occurrence of pattern1 (pat1) with pat2 Example :g/tIO/s//Ada.Text_IO/g

This will find and replace tIO by Ada.text_IO everywhere in the file. :g/a/s// /g replace the letter a, by blank :g/a/s///g replace a by nothing note: Even this command be undone by u Examples Opening a New File Step 1 type vim filename (create a file named filename) Step 2 type i Step 3 enter text Step 4 hit ( switch to insert mode) (enter your Ada program) (switch back to command mode)

Esc key

Step 5 type :wq

(write file and exit vim)

Editing the Existing File Step 1 type vim filename (edit the existing file named filename) Step 2 move around the file using h/j/k/l key or any appropriate command h Moves the cursor one character to the left l Moves the cursor one character to the right k Moves the cursor up one line j Moves the cursor down one line nG or :n Cursor goes to the specified (n) line (ex. 10G goes to line 10) Step 3 edit required text (replace or delete or insert) Step 4 hit Esc key (exit from insert mode if you insert or replace text) Step 5 type :wq

100 Vim commands every programmer should know

Basics
:e filename Open filename for edition :w :q :w! Save file Exit Vim Exit Vim without saving

Search
/word Search word from top to bottom ?word /jo[ha]n /\< the /the\> /\< the\> /\< .\> /\/ Search word from bottom to top Search john or joan Search the, theatre or then Search the or breathe Search the Search all words of 4 letters

Search fred but not alfred or frederick Search fred or joe Search exactly 4 digits

/fred\|joe

/\<\d\d\d\d\> /^\n\{3}

Find 3 empty lines Search in all open files

:bufdo /searchstr/

Replace
:%s/old/new/g :%s/old/new/gw :2,35s/old/new/g :5,$s/old/new/g :%s/^/hello/g :%s/$/Harry/g Replace all occurences of old by new in file Replace all occurences with confirmation Replace all occurences between lines 2 and 35 Replace all occurences from line 5 to EOF Replace the begining of each line by hello Replace the end of each line by Harry

:%s/onward/forward/gi Replace onward by forward, case unsensitive :%s/ *$//g Delete all white spaces

:g/string/d Delete all lines containing string :v/string/d Delete all lines containing which didnt contain string :s/Bill/Steve/ :s/Bill/Steve/g :%s/Bill/Steve/g :%s/\r//g :%s/\r/\r/g Replace the first occurence of Bill by Steve in current line Replace Bill by Steve in current line Replace Bill by Steve in all the file

Delete DOS carriage returns (^M) Transform DOS carriage returns in returns Delete HTML tags but keeps text Delete lines which appears twice

:%s#<[^>]\+>##g :%s/^\(.*\)\n\1$/\1/ Ctrl+a Ctrl+x ggVGg?

Increment number under the cursor Decrement number under cursor Change text to Rot13

Case
Vu b Lowercase line Uppercase line

g~~ Invert case vEU vE~ Switch word to uppercase Modify word case Set all text to lowercase Ignore case in searches Ignore case in searches excepted if an uppercase letter is Sets first letter of each word to uppercase Sets first letter of each word to lowercase

ggguG

:set ignorecase :set smartcase used :%s/\<./\u&/g :%s/\<./\l&/g

:%s/.*/\u& :%s/.*/\l&

Sets first letter of each line to uppercase Sets first letter of each line to lowercase

Read/Write files
:1,10 w outfile Saves lines 1 to 10 in outfile Appends lines 1 to 10 to outfile :1,10 w >> outfile :r infile :23r infile

Insert the content of infile Insert the content of infile under line 23

File explorer
:e . Open integrated file explorer :Sex Split window and open integrated file explorer :browse e :ls Graphical file explorer

List buffers

:cd .. Move to parent directory :args List files :args *.php Open file list :grep expression *.php Returns a list of .php files contening expression gf Open file name under cursor Interact with Unix

:!pwd Execute the pwd unix command, then returns to Vi !!pwd Execute the pwd unix command and insert output in file :sh Temporary returns to Unix

$exit Retourns to Vi

Alignment
:%!fmt Align all lines !}fmt Align all lines at the current position 5!!fmt Align the next 5 lines

Tabs :tabnew gt Creates a new tab

Show next tab Show first tab Show last tab

:tabfirst :tablast

:tabm n(position) Rearrange tabs :tabdo %s/foo/bar/g :tab ball Execute a command in all tabs

Puts all open files in tabs

Window spliting
:e filename Edit filename in current window :split filename ctrl-w up arrow ctrl-w ctrl-w Split the window and open filename Puts cursor in top window 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 Define dict as a dictionnary

:set dictionary=dict

Ctrl+x Ctrl+k mk k

Complete with dictionnary Marks

Marks current position as k Moves cursor to mark k

dk Delete all until mark k

Abbreviations
:ab mail mail@provider.org mail@provider.org Define mail as abbreviation of

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-dIndent/un-indent in insert mode >> << Indent Un-indent

Syntax highlighting
:syntax on Turn on syntax highlighting :syntax off Turn off syntax highlighting :set syntax=perl Force syntax highlighting

You might also like