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
twice
~ - 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
Syntax:
:<command> <params|options>
Press Tab Key for options or auto-completion of commands
e.g:
: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
v/V/ctrl+v
3. You cant type text to the file in command mode

Commands for command mode


h,j,k,l

left, down, up, right

dd

Delete line

yy

Copy line

Paste line

iv:
u

undo

ctrl+r

redo

Delete character

Navigate to the beginning of the line

gg

Navigate to the beginning of the file

Navigate to the last line of the file

Navigate to the end of the line

w/e

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

Combine command
dw

Delete the word on cursor

dG

Delete content till end of file, from cursor location

dg

Delete content to the beginning of file, from cursor location

Yw/yG/yg

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

^/$

???

ww

???

Search for text or regular expression

ZZ

Save file and quit equal to :wq!

Combine the broken lines

g<h,j,k,l>
or
g<arrow>

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

Last Line Mode


:w

write to file

:w <file>

Write to the <file>

:q

quit

:q!

Force quit

:wq

Write and quit

:wq!

Write and force quite (multiple files)

:ls

List of files opened with their open index

:n <file>

Open the file named <file>

:bn/:bp

Next file, Previous file opened

:b<n>

Open the file with index <n>

ctrl+6

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

:!<sh-cmd>

Execute shell command e.g: !ls /root

:.!<sh-cmd>

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

:
%s/<pat>/<su
btext>/gc

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


confirmation

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> +
Esc

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

V+
s/<pat>/<subtext>/g

Only search replace on selected text without


confirmation (NOTE: add c for confirmation)

gv

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
paste
gf

Open the headerfile

Open the man page

Search text where cursor placed

(ctrl+])

Go to the function defination

(ctrl+t)

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

(ctrl+6)

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)
(ctrl+enter)}ki(ctrl+tab)
: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)

Hints
.vimrc
In your home directory /home/varadhan/.vimrc add all the settings
:map
:set
:ab
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
YOUR FILE IS NOT SAVED LEAD TO DATA LOSS
Use :wq :q :q! Etc
$ vimtutor

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