VIM

From Vaddina.com

Jump to: navigation, search

Contents

Basic Commands

 wq/ZZ

Save and exit

 wq file-name

Save the current file as a new file with the name, file-name

 q

Quit

 !q

Forced quit. All changes are lost

 w

save

Navigation

 0(numeric Zero)

Move the cursor to home

 $(dollar-sign)

Move the cursor to the end of the line

 Ctrl + u

Page-up

 Ctrl + d

Page-down

 b

Moves the cursor back to one word back

 w

Moves the cursor forward by one word

 I

Start inserting text at the beginning of the current line

 A

Start inserting text at the end of the current line

 gt/gT

Move to the next/previous tab window in the editor

Copy/Yank

 yy, 8yy

Yank the current line. The 8, indicates the number of lines to be yanked.

 yl

Yank a the current character

 yw

Yank a word from the current cursor position

 y$

Yank from the current cursor position to the end of the line

 y)

Yank from the current cursor position to the end of the sentence

 " * <number> yy

Copies the specified number of lines starting from the current line to the clipboard. Ex: "*23yy --> Will copy, 23 lines


Paste

 p/P

When p, pastes the text, before the current position of the cursor. Similarly, P, pastes the text after the current cursor.

 " * p/P

Paste the contents of the clipboard. When 'p' is used, the text is copied before the cursor and when 'P' is used, after the cursor


Cut/Delete

 d/x

Internal Cut

 dd, 8dd

Cuts the current line. The 8, indicates the number of lines to be cut.


Search/Replace

 /<search-word>

All occurances of the <search-word> are highlighted.

 [range]s/old/new/gic

Replace all occurrances of the search-word, old with the word, new, and ask for confirmation Examples

 %s/^/Insert this text/

Use the above command to insert text at the beginning of every line in the file.

 %s/$/Insert this text/

Use the above command to insert text at the end of every line in the file.

 '<,'>s/^/Insert this text/

Use the above command to insert text at the beginning of every line of a selection.

In all the below cases, to delete lines that does not match the corresponding patters, use either v or g!

 g/^#/d

Delete all lines that begins with #. Note that the ^ is used to indicate the beginning of the line. Ignoring it will search the whole line for the character # and deletes the line

 g/^$/d

Delete all lines that are empty

 g/^search_text/s/^/add_text/

Search the whole file for lines which begin with, search_text, and then insert the text, add_text at the beginning. Please note that by default the range of the above command is % (the whole file), contrary to the command, substitute, which works only on a single line.

 g/^[^$]/s/^/add_text/

Insert text at the beginning of all lines, that are non-empty

 g/^[a-zA-Z]/s/^/add_text/

Search the file for lines having atleast one character, beginning with either of a-z or A-Z and then insert the text, add_text at the beginning of the line

 g/^[a-zA-Z]./s/^/add_text/

Search the file for lines having more than one character, beginning with either of a-z or A-Z and then insert the text, add_text at the beginning of the line

 g/^echo/s/$/add_text/

Search the file for lines beginning with echo and then insert the text, add_text at the end of the line


Some advanced VIM Search and Replace commands at this link: http://www.brezeale.com/technical_notes/vim_notes.shtml#search

Diff

Starting the diff view

 gvim -d file1 file2

Shows the diff between the two files

Commands to use in diff

 ]c

Show the next difference

 [c

Show the previous difference

 do

To replace a diff in the current viewport with that from the other viewport

 dp

To replace a diff in the other viewport with that from the current viewport

Bookmarks

 m<letter>

Shall assign a bookmark to the current line with the character, <letter>

 '<letter>

Move to the bookmark marked with the character, <letter>

vimrc hacks

Insert the following lines in the vimrc file to insert spaces instead of tab characters, and to choose the number of spaces to insert for a tab

set tabstop=2
set shiftwidth=2
set expandtab

Character case's

 gU/gu

Convert the selected text to Uppercase/Lowercase respectively.

 gUGG

Will apply the Uppercase command to the whole file. Every character will be in Uppercase. Similarly, guGG can be used to convert all characters to lowercase.

 %s/\(monitor\)/\U\1/gi

Will look for word monitor (case insensitive) in the current buffer and converts it to MONITOR

 %s/\(monitor\)/\L\1/gi

Will look for word monitor (case insensitive) in the current buffer and converts it to monitor

 %s/\(monitor\)/\u\1/gi

Will look for word monitor (case insensitive) in the current buffer and converts it to the title format Monitor

Spaces/Tabs

1. C-d
2. <<
3. <

Command to move to the left, the current line in Insert mode, the current line in command mode and the current selection in visual mode, respectively.

1. C-t
2. >>
3. >

Command to move to the right, the current line in Insert mode, the current line in command mode and the current selection in visual mode, respectively.

Miscelleneous

 ~

Toggle the cases of all the characters of the selection

 v

Enters visual mode

 V

Selects the entire line

 %

Find the matching braces, brackets, etc ...

 tabnew <filename>

Will create a new tab. Only with VIM versions greater than 7.0

 marks

Displays all the bookmarks

 set number

Sets line numbers in the file

 syntax on/off

Turn on/off syntax highlighting with the default colors

 Ctrl + w + v

Vertical split of the text editor

 Ctrl + w + w

When the editor screen is split into two vertical halfs, the above key combination can be used to navigate between the two split screens

Personal tools
Namespaces
Variants
Actions
Navigation
My Work
C/C++
Tutorials/Books/eBooks
Miscelleneous
Contact
Other
Toolbox