The Vi and Vim text editors

Vi and Vim are two powerful text editors which are very useful if we take time to understand how they work and what they are capable of.

Intro

Configuration file :

Vim files configuration are located in two paths :

/etc/vimrc
~/.vimrc

or

/home/USER/.vimrc

We can for example put syntax on, in order to activate the color syntax.

Open a file

vim file

Open two, three or four files version and show the differences between them


vimdiff file1 file2 … 

Open files one above the other

vim -o file1 fichier2

Open files side by side

vim -O file1 fichier2

Open a file and go directly to line "x"

vim +"x" file1

Some commands

Commands to move the cursor or the display
h Move left.
j Move down.
k Move up.
l Move right.
b Move "Back" from a word to the left.
B "Back" same as b but will only stop to a space.
v "Visual" mode.
V "Visual" mode, select by line.
Ctrl+v "Visual" bloc mode.
gv Select the last "visual".
w Move "Word" from a word to the right.
W "Word" same as w but will only stop to a space.
G Move the cursor to the last line.
Ctrl+f Move the display down.
Ctrl+b Move the display up.
$ Move to the end of the line.
0 Move to the beginning of the line.
^ Move to the first character of the line.
{ Move the cursor to the next paragraph.
} Move the cursor to the paragraph before.
Edit mode
dd Cut the current line.
dw Cut "Word".
dl Cut "Letter".
x Cut a character.
X Cut a character to the left.
yy Copy the current line..
yl Copy "Letter".
yw Copy "Word".
D Cut from the current position to the end of the line.
p Paste.
P Paste before the current line.
u Cancel last operation.
Ctrl+r Redo a command previously canceled.
ZZ Save and quit.
Text Mode
. Repeat the last action.
a "Add" insertion mode on character after the cursor.
A "Add" insertion mode at the end of the current line.
i "Insert" insertion mode.
I "Insert" insertion mode at the first non blank character of the current line.
J "Join", join two lines.
o insertion mode one line under the cursor.
O Switch to insertion mode one line under the cursor.
r "Replace" current character by the next typed character.
R "Replace" mode Replace, each new character by a new one. Press Esc to go back to command mode.
cl "Cut Letter" cut a character.
ct+ Cut until + character.
cw "Cut Word" cut from the cursor to the next space, comma or full stop.
cW "Cut Word" cut from the cursor to the next space.
C "Cut" from the cursor to the end of line and go to Insertion mode.
xp Swap two characters.
<ESC> Quit TEXT-INPUT. mode
ctrl+ ou ctrl+a Increment a number.
ctrl- ou ctrl+x Decrement a number.
ctrl+R "Redo" play the last cancelled command.
~ Modify the current character case.
guu or vu Swap current line to lower case.
gUU ou vU Swap current line to upper case.
veu Swap current word to lower case.
vEU Swap current word to upper case.
gqap ou vapgq Justify current paragraph.
Command Mode
:w "Write" Save the file.
:w file "Write" Save the file with a different name inside current folder.
:q "Quit" if file hasn't been altered.
:q! "Quit" even if file altered.
:wq "Write and Quit"
:x Save and quit if edited.
:n Go to "n" line.
:!foo Execute the foo external command.
:chdir or :!chdir Change current directory.
:open or :e file Edit file "file".
:e ++enc=latin1 Re-open a file with a different encoding.
:e ++ff=dos Re-open a file with a different encoding.
:set fileformat? Print the encoding format.
/word Search the word "word" from the top to the bottom. Type n to go to the next word and N to the previous.
?word Search the word "word" from the bottom to the top. Type n to go to the next word and N to the previous.
:$ Move the cursor to the last line.
:syntax on Enable syntax, put some color on the text.
:set nu(mber) Enable the line number print.
:set nonu Disable the line number print.
:set list Print the hidden character (set nolist to disable).
Completion
Ctrl+n Word completion (repeat to go down).
Ctrl+p Word completion (repeat to go up).
Ctrl+r then =x+n Insert arithmetic result x+n.
Ctrl+r then % Insert the name and current path of file.
Ctrl+r then / Insert current search buffer.
Multi Windows Mode
Ctrl+w Action
w Change current windows.
n Create a new windows.
+ or - Extend/Reduce current windows.
x Switch two windows.
q Close current windows.
h or j or k or l Move the cursor to a different windows.
:sp Separate a same file in two windows (Action+q to go back to normal mode).
Easter Eggs
:help! No panic!
:help holy-grail You found it, Arthur!
:help 42 Refer to H2G2.
:help UserGettingBored When the user hits CTRL-C. Just kidding! :-)
:Ni! Do you demand a shrubbery?

Examples

:%s/\s\+$//g
:s/$/<\/br>/12
:10,50g/check_command/s/\(.*\!\w*\).*/\1 1\ 3\ 5\ \/home\/

Explanations :

:g/^$/d
:g/^\(\d\{,3}\.\d\{,3}\.\d\{,3}\.\d\{,3}\)//
:s/<\(\/\=\)\(pre\)/<\1ul><\1\2/g

Explanations :

Others

Buffers

It's possible to put some text inside buffers which allow to keep inside memory some elements that we can use later. To do so, we will use following combination "+letter

"ayy
"ap

Automatic line break each 72 characters

echo 'set textwidth=72' >> ~.vimrc

spell-checker

echo 'set spelllang=fr' >> ~.vimrc
wget ftp://ftp.vim.org/pub/vim/runtime/spell/fr.latin1.spl
wget ftp://ftp.vim.org/pub/vim/runtime/spell/fr.latin1.sug
wget ftp://ftp.vim.org/pub/vim/runtime/spell/fr.utf-8.spl
wget ftp://ftp.vim.org/pub/vim/runtime/spell/fr.utf-8.sug
:set spell

We will use z= on every red words to access to a correction list.

references : Comment ça marche, linuxfr.org, http://www.tuteurs.ens.fr/, http://www.lyxia.org

Licence Creative Commons
This website http://shebangthedolphins.net is licensed to the public under a licence Creative Commons Attribution licence.
Contact :