Professional Documents
Culture Documents
: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.
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
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.
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.
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.
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)
$ 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.
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
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
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
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}
: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
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
:%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
:tabfirst :tablast
:tabm n(position) Rearrange tabs :tabdo %s/foo/bar/g :tab ball Execute a command in all 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
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