Home   Profile   Fun
#69 Linux  03.04.2007

Basic commands and tips for the text editor Vim


Tip: you can use a shell and have vim open at the same time. Use screen to split the shell horizontally, start vim in one region and a shell in the other one.

Start Vim
vim

Start Vim and open/create a file for editing
vim filename

Go to normal mode to enter commands.
<esc>

This brings you back any time you are lost, sometimes you have to press it twice!

In normal mode you have 3 possibilities:
************************************
Press i to insert and manipulate text (insert mode)
<i>

Operate just by pressing a combination of keys, for example dd to delete a whole line
<dd>

For other commands or more complex operations press the colon first, enter the command and hit return. For example :q! to quit vim without saving.
:q! <return>
************************************

Remember that <esc> always brings you back!

The trick is to switch between insert mode for manipulating text <i> and normal mode to enter commands <esc>.

For an example workflow open a shell and type:
vim newfile.txt
<i>
insert some text
<esc>
:wq

For the following examples I assume you are in normal mode to enter commands.

Open/create a file inside vim
:edit filename

Save file
:w

Save file and quit vim
:wq

Save file as filename
:w filename

Quit vim
:q

Quit vim without saving
:q!

Undo
:u

Redo
<ctrl-r>

Mark text
<v>
and move the cursor

Copy text
<y>

Paste text
<p>

Delete text
<x>

Split screen horizontally and open/create another file
:split filename2

Split screen vertically and open/create another file
:vs filename2

Move between split screens
<ctrl-w>
Now press the arrow key which points to the region you want


Delete a whole line
<dd>

Delete everything from cursor position to end of file
<dG>


Delete everything from cursor position to begin of file
<dgg>


Delete from cursor until begin of line
<d0>

Delete from cursor until end of line
<d$>

Go to line 120
:120

Scroll down one page
<ctrl-f>

Scroll up one page
<ctrl-b>

Search a string
:/searchstring

Search a string backwards
:?searchstring

Jump to next occurence of a found string
<n>

Jump to previous occurence of a found string
<shift-n>

You can use <*> to find the next occurence of the word on which the cursor currently resides. <#> will do the same backwards. Move the cursor over a bracket and press <%> to find the corresponding bracket.

Sort lines and remove duplicates
:sort u

Remove empty lines
:g/^\s*$/d

Compare the current open file with another file
:vert diffsplit filename2

Replace every occurence of string1 with string2 in the current line and all 900 following lines
:.,.+900 s/string1/string2/g

Activate syntax highlighting
:syntax on

Deactivate syntax highlighting
:syntax off

Show/hide line numbers
:set number
:set nonumber

Enable/disable case insensitive searches
:set ignorecase
:set noignorecase


You can use the configuration file .vimrc to set parameters permanently. If the file does not exist in your home directory, create it. Here is an example:
"**********************************
"enable syntax highlighting
syntax on

"set the tab size to 4
set tabstop=4

"always display the status line
set ls=2

"disable the bell
set vb t_vb=

"disable autoindention
set noautoindent
set nocompatible
"**********************************


If you need a shell but don't want to end your vim session go to normal mode with <ESC> and press Ctrl-z.
To get back to vim type fg. These aren't vim commands but very helpful though.