Work with vi vim

Hi and welcome everyone, today in iThePro we will take a deep look to vi and  get start with it. This tutorial is for both of beginners and a reminder for professionals :) !

So vi or vim is :
1- Available on all most OS. (even Android!)
2- Small, fast and simple.
3- No UI (User Interface), need to memorize commands.

VI Modes:

Entering and leaving vi:
•     $ vi file edits existing or new file
•     [Shift] + ZZ writes file and quits (or :wq[Return] )
•     :q![Return] quits without saving changes

File management:
:w     Write without quitting     :q     Quit without writing
:q!     Abandon changes           :vi     Edit another file
:n     Go to next file                    :N     Go to previous file
:rew Rewind to first file           :r     Read file into this one                   
•     Use [Control] + g to display line number and file status

Basic cursor movement:
h     Left one character
j      Down one line
k     Up one line
l      Right one character (lowercase L)

More ways of moving the cursor:
•     Arrow keys
•     [Space] and [Backspace] for forward and backward
•     [Return] to move to beginning of next line
•     – (minus) to move to beginning of previous line
•     Can use a number before any movement command                           

Moving by textual units:
w     Forward a word   
b     Backward a word
e     Forward to end of current or next word   
    Forward a sentence         ( Backward a sentence
}     Forward a paragraph     { Backward a paragraph
•     Can use a number before any movement command

Moving by lines:
^      Beginning of line       $     End of line
1G   First line of file         G     Last line of file
nG   Line n of file              %     Matching paren/brace                   
•     Use [Control] + g to display line number and file status
•     Can use a number before any movement command

•     [Control] + e     Scroll down one line (“expose”)
•     [Control] + y     Scroll up one line
•     [Control] + d     Scroll down half a screen
•     [Control] + u     Scroll up half a screen
•     [Control] + f      Scroll down one screen (“forward”)
•     [Control] + b     Scroll up one screen (“back”)  

Inserting text:
i      Insert before cursor               I     Insert at beginning of line
a     Append after cursor              A     Append at end of line
o     Open a new line below         O     Open a new line above
•     All of these commands enter insert mode
•     Use [Esc] to return to command mode

Deleting text:
x        Current character     dd     Current line
de     To end of word          d$     To end of line (also D)
dw     Current word             d^     To beginning of line           
•     Can use dx, where x is any movement command
•     Can use a number before any deletion command

Changing text:
r         Current character *          s     Current character
cc      Current line                       cw     Current word (also ce)
c^      To beginning of line         c$     To end of line (also C)                  
•     * r leaves you in command mode. Others: insert mode
•     As with delete, c can use number or any movement cmd

Miscellaneous editing commands:
•     R enters overwrite mode ([Esc] to exit)
•     ~ changes the case of the character at the cursor
•     J joins the next line to the current line
•     Can use a number before any of these

Undo, redo, and repeat:
•     u undoes last change (repeat to undo more)
•     [Control] + r redoes last change (undoes undo)
•     U undoes all changes to current line
•     . (period) repeats last change
•     Multiple undo and redo are Vim features

Delete, yank, and put:
dd     Delete (cut) line        yy     Yank (copy) line
dx     Delete an amount     yx     Yank an amount
p       Put after cursor            Put before cursor
•     As with delete, y can use number or any movement cmd
•     Deletes and yanks go into vi’s “buffer,” not OS clipboard

•     /text searches forward for text
•     ?text searches backward for text
•     n repeats previous search
•     N repeats previous search in opposite direction

Regular expressions:
•     . matches any single character
/a.c is to search any word with char in between a and c,
"abc", "aac". but NOT "ac" !!!.

•     \ removes special meaning
Example :
/a\.c is for the word "a.c", so here we remove the meaning of previous regular expression.
/a\/c here we remove the meaning of searching "/", so we looking for "a/c" word.

•     ^ and $ match line starts and ends
Example :
/^abc Search for line begging with "abc".
/abc$ Search for line end with "abc".

•     [ ] matches any character in set
Example :
/a[xyz]c match "axc","ayz" or "azc", BUT NOT "axyzc".
/a[a-z]c match range in between "a" and "c" .
/a[a-zA-Z]c match any upper or lower case in between "a" and "c".
/a[^a-z]c match any char not include in a-z range, "a$c" !

•     *  matches zero or more repeats
Example :
/ab*c matches "abc", "ac" Or "abbbbbbbbc".

•     \( \) repeats multiple items
Example :
/\(ab\)*c matches "abababababc".

Search and replace:
•     :s/old/new/        replaces first old with new on current line.
•     :s/old/new/g      replaces every old with new on current line.
•     :%s/old/new/     replaces first old with new on every line.
•     :%s/old/new/g   replaces every old with new on every line.
      Can actually be any character like {/;,...}
old    is a regular expression
new   is generally a string,
&       is replaced with the found text
\n      is replaced with the number of the text found
:%s/the[yum]/(&)/g                 this will add () the founds !
:%s/\(they\) \(were\) / \2 \1/   this will replace 1 with 2 (they with were) !

Indenting, auto-indent, and word wrap:
•     >> indents current line
•     << outdents current line
•     As with delete, can use number or movement command
•     :se ai enables auto-indent              :se noai disables it
•     :se wm=8 enables wrap margin   :se wm=0 disables it

Filtering through shell commands:
!!     filters current line through shell command
n!!     filters n lines                 !%     filters to matching paren
!}     filters next paragraph    !{     filters previous paragraph
•     Useful commands include fmt, tr, grep, sed, and awk
:.!tr a-z A-Z  this will replace current line all lower case with upper case.

Using line ranges with colon commands:                    
•     : line [, line] cmd performs cmd on line(s) specified as:
n      Line number n              .     Current line
     Last line of file             ‘x     Mark x (set with mx)
/re/  Regular expression      %     All lines (same as 1,$)
•     cmd includes s, d, y, c, !, >, and others
s: Search    d: Delete     y: Yank    c: Change    !: Filtering     >: indent
:1,. d
Delete lines from first to current line.
:.,$ s/t/T/g
replace all t with T from current to the last line.
:/marched/,/half-way/ ! tr a-z A-Z 
look for a line contains "marched" until a line contains "half-way" and replace all lines between to upper case characters .
:'n,'m s/old/new/g
from mark n to m change old with new !

Using the "~/.exrc" file:
This file will contains the default configuration of vi for the user !

set ai
set nu
set nocp




Oussama Amri 21 years, security professional from Tunisia. Lover of computer science especially programming and secuirty. Follow me On :

Popular Posts Powered by Blogger.