Vous êtes sur la page 1sur 14

VIM Editor

What is Vim Editor ?

Vi Improved Editor, Vi is a basic text editing tool
available in all *inx based platform.
Why Vim Editor ?
Modern Linux have got lots of IDEs but when it
comes to command line, Vi or Emacs or nano are
the options available in *inx based platforms.

Why Vim ?
Simple reason: once you are familiar with it, you cant get anything better.
Features like auto completion, syntax highligting, search / replace features
like anyother editors will encourage to use Vim.
Navigating with the keyboard is just way faster.
gVim a graphical version also available without need for console.
Programmer Friendly and Faster
Mostly Keyboard is enough or better way to work with editor
More than you need from text editor and its not an text editor

First Step

Installing in your Linux (ubuntu)

sudo apt-get install gtypist

Lets start with Vim Basic

$ sudo apt-get install vim vim-common vim-enhanced
Vim Modes (Fundamentals)
> Command Mode - Esc
> Insert Mode i
> Visual Mode v / V / Ctrl+v
Hint If you are not sure which mode you are in, press the Esc key
~ - Symbol means empty lines
$ vim <filename-1> <filname-2>

Command Mode
To get in Command mode press Esc key (twice to ensure)
I. editor command
Provides you following operations
insert, append, and delete, and other search and navigation commands.
NOTE: when you're in command mode you can't insert text immediately
II. last line mode command
:<command> <params|options>
Press Tab Key for options or auto-completion of commands
:set colorscheme <tab>
You can enter command by typing
Esc :<command>

Insert Mode
To get in Insert Mode press i or a, when you are in Command mode
Once you enter in Insert mode you can see in last screen displaying -INSERT -Insert mode allows you to enter text
i will start enter Insert mode while cursor on character
a will start enter Insert mode by moving cusor to the next character
You can switch back to command mode by pressing Esc key from Insert mode

Switching Mode Rules

1. You cant type commands in Insert mode, you need to press Esc key to enter
command mode
2. You cant switch to last line mode or Visual mode directly from insert mode,
you need to switch to command mode by pressing Esc and then press : or
3. You cant type text to the file in command mode

Commands for command mode


left, down, up, right


Delete line


Copy line

Paste line





Delete character

Navigate to the beginning of the line


Navigate to the beginning of the file

Navigate to the last line of the file

Navigate to the end of the line


Move word by word (w-beginning, e-end of word)

Combine command

Delete the word on cursor


Delete content till end of file, from cursor location


Delete content to the beginning of file, from cursor location


Copy y + w/G/g operation (like Delete d + w/G/g)





Search for text or regular expression


Save file and quit equal to :wq!

Combine the broken lines


Move with the wrapped line (long text line broken in screen for
display purpose use :set wrap)

Last Line Mode


write to file

:w <file>

Write to the <file>




Force quit


Write and quit


Write and force quite (multiple files)


List of files opened with their open index

:n <file>

Open the file named <file>


Next file, Previous file opened


Open the file with index <n>


Swap alternatively between files (symbol index % - current / #)


Execute shell command e.g: !ls /root


Insert the text output of the command under the cursor


% replaces current file name (:ls displays with/without path)

%< removes the extension from the file name
e.g: :!gcc % -o %< ; :!gcc prog.c -o prog


% means global to file, search and replace g global line, c for


Visual Mode
Visual mode is a selection mode. There are three types of Visual selection.
v select from text like a mouse cursor
V select full line by line
Ctrl + v select block, one of the prominent features of Vim
Combine the selection with command mode commands to do editing
e.g V select text press d to delete / y to copy / w t move word by word
v + w.. + <cmd>

Select word by word

V + G + <cmd>

Select till end of line from the cursor

(ctrl+v) + I + <text> +

Visual block + into insert mode to add the add text on

all selected block

(ctrl+v) + d/y/p

Visual block select to delete, copy and paste


Only search replace on selected text without

confirmation (NOTE: add c for confirmation)


Re-do last selection

Programmer Special
$ sudo apt-get install ctags
$ cd program-dir/
$ ctags -R #program specific option
$ vim <file1>.c/.php/.cpp/.java/.js etc...
Install gpm for mouse in terminal mode, select with mouse do middle click to

Open the headerfile

Open the man page

Search text where cursor placed


Go to the function defination


Go back to the stacked call using (ctrl+])


Go back to the previous alternate files (:ls to list of files)

Match braces/brackets/macros

:map <key> <cmd>

:ab inc #include <stdio.h>(ctrl+enter)main() {(ctrl+enter)
:set ts=4 et

Ttab stop to length 2, expand tab to space

:split / :vsplit

(ctrl+v) to switch btw windows / vsplit for vertical

Text(ctrl+p/n) Auto completion (NOTE: Esc to undo during autocomp)

In your home directory /home/varadhan/.vimrc add all the settings
Etc,. Settings will be loaded automatically
swp files
Swp files are created when you open file with prefix dot(.) and ext (.swp) its for
recovery. Make sure to close the files with write or force quit to clear the swp files.
Too many swp files will lead loss of data by mistakenly recovered file.
ctrl + z: DONT
Use :wq :q :q! Etc
$ vimtutor

VIM Learning
Its harder, but it pays you well.
Practise makes things better.