vi editor in 100 minutes
DESCRIPTION
A quick intoruction into the "vi" editor in 100 minutes.TRANSCRIPT
1
Viin 100 minutes.
The Editor which no one knows how to use !
Auther: Ahmed Maklad 2
http://maksoft.ch
V.I.P ?
Vee eye
Auther: Ahmed Maklad 3
http://maksoft.ch
LicenseCreative Commons
You can use the material in this document for non-commercial purposes on the condition that you reference http://MakSoft.ch . you can’t modify the material without written approval from Ahmed Maklad (MakSoft.ch) first.
This Document is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unsupported License.
Auther: Ahmed Maklad 4
http://maksoft.ch
How to use these slides ?
These Slides were designed to serve as a Tutorial in guided and unguided modes.
Each Slide attempts to answer the questions raised in the previous slide.
The slides could also be used as a Desktop Quick Reference.
Please Remember to reference the Author and source of those slides :
Ahmed Maklad
http://MakSoft.ch
Auther: Ahmed Maklad 5
http://maksoft.ch
Disclaimer All data and information provided on this site are for
informational purposes only. Ahmed Makald, Maksoft.ch and sub-domains makes no representations as to accuracy, completeness, currentness, suitability, or validity of any information in this document and will not be liable for any errors, omissions or delays in this information or any losses, injuries, or damages arising from its display or use. All information is provided on an as-is basis.
Auther: Ahmed Maklad 6
http://maksoft.ch
This session we discuss the following: vi Modes
Syntax of vi commands
Examples of each command
Usability scenarios and how to use vi in real world
Remember : you need to practice after wards on your own to memorize everything.
Auther: Ahmed Maklad 7
http://maksoft.ch
The Two Modes of VI
Command mode: The command mode allows the entry of commands to manipulate text. These commands are usually one or two characters long, and can be entered with few keystrokes.
Insert mode :puts anything typed on the keyboard into the current file
VI starts in command mode.
Auther: Ahmed Maklad 8
http://maksoft.ch
Navigating the “modes”
Command
Mode
Insert
Mode
ESC or control-[
a , A , i, o,O,r,C …etc
ESC or control-[
Auther: Ahmed Maklad 9
http://maksoft.ch
Moving the cursor (command Mode)
ij
kh
Auther: Ahmed Maklad 10
http://maksoft.ch
Moving the cursor (command Mode)
Moving to end and beginning of a word
WordWordWordWord
eb
Auther: Ahmed Maklad 11
http://maksoft.ch
Moving the cursor (command Mode)
$ in Command mode moves to the end of current line.
0 or ^ moves to beginning of line
:$ moves to last line of file.
:0 moves to 1st line of file
Auther: Ahmed Maklad 12
http://maksoft.ch
Exiting vi
Before you learn how to fly you have to learn first how to land !
:q! Quit/Exit WITHOUT saving any changes made.
:wq Write then Exit/Quit.
Auther: Ahmed Maklad 13
http://maksoft.ch
1) The Insertion Mode: the append commands
a (lowercase) to append text AFTER the character under the cursor
A (Uppercase) appends to the end of the line.
Auther: Ahmed Maklad 14
http://maksoft.ch
the open (line) command
o to open a line BELOW the cursor and place you in insert mode.
To open up a line ABOVE the cursor, simply type a capital O , rather than a lowercase o.
Auther: Ahmed Maklad 15
http://maksoft.ch
the open (line) command
Current line
o
O
Auther: Ahmed Maklad 16
http://maksoft.ch
To replace and change
cw To change part or all of a word.
C (shift-c) Change Rest of Line.
s Substitute chars.
S substitute lines (cc)
rx to replace One character under the cursor with new character x.
R overwrite mode, no insertion.
Auther: Ahmed Maklad 17
http://maksoft.ch
2) The Command Mode
The command mode commands are normally in this format:
[count] command [where]
(Optional arguments are given in the brackets)
Auther: Ahmed Maklad 18
http://maksoft.ch
Command Mode (cont.)[count] command [where]
The count is entered as a number beginning with any character from 1 to 9. For example, the x command deletes a character under the cursor. If you type 23x while in command mode, it will delete 23 characters.
Some commands use an optional where parameter, where you can specify how many lines or how much of the document the command affects, the where parameter can also be any command that moves the cursor.
Auther: Ahmed Maklad 19
http://maksoft.ch
[count] command [where]
d,r,c,s,y ..etc
l line
wword
b back a word
e end of word
h one char back
$ end of line
^ begin. of line
… etc
Auther: Ahmed Maklad 20
http://maksoft.ch
text editing - deletion
While in Command Mode press x to delete the character under the cursor.
Auther: Ahmed Maklad 21
http://maksoft.ch
deletion commands
Type d$ to delete to the end of the line.
Type d^ to delete from beginning of line.
Type dw to delete to the end of a word.
Type db to delete a word backwards.
Type dL to delete all lines till end of file.
Auther: Ahmed Maklad 22
http://maksoft.ch
On commands and objectsThe format for the d delete command is as follows:
[number] d object
or
d [number] object
number - is how many times to execute the command (optional, default=1).
d - is the command to delete.
object - is what the command will operate on (listed below).
– w - from the cursor to the end of the word, including the space. – e - from the cursor to the end of the word, NOT including the
space. – $ - from the cursor to the end of the line
Auther: Ahmed Maklad 23
http://maksoft.ch
an exception to 'command-object'
dd
to delete a whole line.
Due to the frequency of whole line deletion, the designers of VI decided it would be easier to simply type two d's in a row to delete a line.
2dd
(remember number-command-object) to delete the two lines.
D (shift-d)
Deletes the rest of a line.
Auther: Ahmed Maklad 24
http://maksoft.ch
text editing - insertion
Press i and type in the necessary additions .
Auther: Ahmed Maklad 25
http://maksoft.ch
the undo command
u to undo the last command,
U to fix a whole line.
. (dot) repeats the last command you made
Auther: Ahmed Maklad 26
http://maksoft.ch
more changes using c (same as d)
[number] c object
or
c [number] object
number - is how many times to execute the command (optional, default=1).
c - is the command to change.
object - is what the command will operate on (listed below).
– w - from the cursor to the end of the word, including the space. – e - from the cursor to the end of the word, NOT including the
space. – $ - from the cursor to the end of the line
Auther: Ahmed Maklad 27
http://maksoft.ch
Joining lines:
shift-J (capital J) Joins current line with next line.
------------------------------------------------------Changing Case: ~ changes the case of letter.
10~ changes case of 10 characters.
Auther: Ahmed Maklad 28
http://maksoft.ch
location and file status
CTRL-g to show your location in the file and the file status.
SHIFT-G to move to a line in the file.
Press shift-G to move you to the bottom of the file.
Type in the number of the line you want to go to then shift-G. (When you type in the numbers, they will NOT be displayed on the screen.)
:m places cursor at beginning of line m.
:set number to show the line numbers in vi.
:set nonumber to hide line numbers.
Auther: Ahmed Maklad 29
http://maksoft.ch
the searching for the needle
/pattern to search for the pattern downwards.
?pattern to search for the pattern upwards.
n Find Next
N / shift-n Find Previous
To search for the same phrase again, simply type n . To search for the same phrase in the opposite direction, type Shift-N .
If you want to search for a phrase in the backwards direction, use the command ?pattern instead of /pattern.
Auther: Ahmed Maklad 30
http://maksoft.ch
More Search Tricks
/pat/+n nth line after pat
?pat?-n nth line before pat
Simple search for one character in a line.
– fx find next x– Fx find previous x– tx move to character following the next x– Tx move to character following the previous x– ; repeat last f, F, t, or T– , repeat inverse of last f, F, t, or T
Auther: Ahmed Maklad 31
http://maksoft.ch
Searching (Cont.)
/pattern
fx
tx
?pattern
Fx
Tx
Find Next : n
Find Previous : N
Auther: Ahmed Maklad 32
http://maksoft.ch
How to describe a pattern :Special characters:
^ Beginning of the line. (At the beginning of a search expression.)
. Matches a single character.
* Matches zero or more of the previous character.
$ End of the line (At the end of the search expression.)
[
– Starts a set of matching, or non-matching expressions... For example: /f[iae]t matches either of these: fit fat fet In this form, it matches anything except these: /a[^bcd] will not match any of these, but anything with an a and another letter: ab ac ad
<
– Put in an expression escaped with the backslash to find the ending or beginning of a word. For example: /\<the\> should find only word the, but not words like these: there and other.
>
– See the '<' character description above.
Auther: Ahmed Maklad 33
http://maksoft.ch
How to describe a pattern (Cont.):Special characters:
This is a Text line.
………………….
^ $
Auther: Ahmed Maklad 34
http://maksoft.ch
Searching Examples (Cont.)
/[ab]cde
/[ab]*cde
/Hi
~$
A[^aeoui]B
acde, bcde
acde, bcde, aaacde, bbbcde …etc
Hi
Empty line
A[anything but a vouel char]B
More comes in a next session for Regular Expression (RE).
Auther: Ahmed Maklad 35
http://maksoft.ch
a way to substitute patterns:s/old_pattern/new_replaced/ :s/old/new to substitute once 'new' for 'old' in the current line.
:s/old/new/g to substitute ALL 'new' for 'old' in the current line.
:%s/old/new/g to change every occurrence in the whole file.
:#,#s/old/new/g where #,# are the numbers of the two lines, To change every occurrence of a character string between two lines. (more on next slide)
Auther: Ahmed Maklad 36
http://maksoft.ch
More on :#,#s/old/new/g
.,$ current line to the end of file
5,. line 5 to the current line
1,$ line 1 to end of the file (entire file)
% all the lines in file (1,$)
.,.+5 current line to 5 lines down from current line (relative reference)
.-2,. 2 lines above current line to current line (relative reference)
Auther: Ahmed Maklad 37
http://maksoft.ch
More to do with searching
:g/pattern/d
– Deletes every line with pattern. :v/pattern/d
– Deletes every line which doesn’t have the pattern. :6&
– Repeats last s/old/new command on line 6. :10,16&
– Repeats last s/old/new command on lines 10 to 16.
Auther: Ahmed Maklad 38
http://maksoft.ch
% matching parentheses () [] {} search %
use % to find a matching ),], or }
– Place the cursor on any (, [, or { .– Now type the % character .– The cursor should be on the matching parenthesis
or bracket .– Type % to move the cursor back to the first
bracket (by matching).
Auther: Ahmed Maklad 39
http://maksoft.ch
More matching (helpful in code Navigation)
]] next section/function
[[ previous section/function
( beginning of sentence
) end of sentence
{ beginning of paragraph
} end of paragraph
Nice for programmers !
Auther: Ahmed Maklad 40
http://maksoft.ch
writing files to disk
:w filename
:#,# w filename
To save part of the file. where #,# are the two numbers (top,bottom) in your filename
:w >> filename
– Append the contents of the buffer to the filename. :w! Write the file even if read only.
Auther: Ahmed Maklad 41
http://maksoft.ch
retrieving and merging files
:r filename
To insert the contents of a file into the current cursor’s position.
Auther: Ahmed Maklad 42
http://maksoft.ch
Bookmarks / marking (command mode)
You could mark a location with a letter and then when you want to move the cursor ‘ to that location you use the marker (the letter) to do so:
mx makes a mark called x(could be any other letter).
ma ,mb mc …. etc
‘x goes to mark x
‘a , ‘b ,’ac … etc
Auther: Ahmed Maklad 43
http://maksoft.ch
Marking (cont.)
You could use marks as a description for location in the file:
:‘a,’b w filename
– writes a file with lines between markup a and b :‘a,’bs/short/int/g
– Replaces all short with int in the lines between marks a and b.
Auther: Ahmed Maklad 44
http://maksoft.ch
Buffers (vi clipboards)
VI has 36 buffers for storing pieces of text.
Those buffers have absolutely no relationship with the windows clipboard.
There are other buffers also for general purposes (delete and undo).
Any time a block of text is deleted or yanked from the file, it gets placed into the general purpose buffer
if it is specified. The buffer is specified using the “letter (quote) command, the letter has to be lower case [a-z] (i.e. 26 buffers).
Auther: Ahmed Maklad 45
http://maksoft.ch
Buffers
After typing "letter specifying the buffer to be used.
For example, the command: "mdd uses the buffer m, and the last two characters stand for delete current line.
vi saves the last 9 deleted buffers in cells numbering from 1-9. In order to recover the most recent delete use "1p or "1P command and so on. (i.e. 9 buffers)
9 (delete undo buffers) + 26 ([a-z] named buffers) = 36 buffers.
Auther: Ahmed Maklad 46
http://maksoft.ch
Pasting text
text can be pasted in with the p or P command.
"mp pastes the contents of buffer m after the current cursor position.
Auther: Ahmed Maklad 47
http://maksoft.ch
Cut d^
– deletes from current cursor position to the beginning of the line. d$
– deletes from current cursor position to the end of the line. dw
– deletes from current cursor position to the end of the word. 3dd
– deletes three lines from current cursor position downwards. .,$d
– Deletes everything from current position till end of file.
Auther: Ahmed Maklad 48
http://maksoft.ch
Copy
y (yank) copies the text to the buffer.
yy copies the current line to buffer.
y^
– Yanks from current cursor position to the beginning of the line.
y$
– Yanks from current cursor position to the end of the line. yw
– yanks from current cursor position to the end of the word. 3yy
– yanks three lines from current cursor position downwards.
Auther: Ahmed Maklad 49
http://maksoft.ch
Pasting
p
to put the last deletion after the cursor.
1. Move the cursor to the first line in the set below.
2. Type dd to delete the line and store it in VI's buffer.
3. Move the cursor to the line ABOVE where the deleted line should go.
4. While in Command Mode, type p to replace the line. 5. Repeat steps 2 through 4 to put all the lines in correct order.
d) Can you learn too?
b) Violets are blue,
c) Intelligence is learned,
a) Roses are red,
Auther: Ahmed Maklad 50
http://maksoft.ch
Pasting
Cursor
Position pPp (small) pasts buffer After
Cursor Position
P (Capital) pasts buffer Before Cursor Position
Auther: Ahmed Maklad 51
http://maksoft.ch
Pasting a whole Line:
Cursor
Position
p
P
p (small) pasts buffer After
Cursor Position
P (Capital) pasts buffer Before Cursor Position
Auther: Ahmed Maklad 52
http://maksoft.ch
Another way to MOVE lines
:3m10 move line 3 to line 10. (cut’n’paste).
:3,10m20 move lines 3 to 10 to line 20
Auther: Ahmed Maklad 53
http://maksoft.ch
Yet another way to transfer lines.
:3t10 transfer line 3 to line 10. (copy’n’paste).
:3,10t20 transfer lines 3 to 10 to line 20
:+1,+4t10 1 line to 4 lines down the cursor’s position to be transferred to position 10 (relative positioning)
Auther: Ahmed Maklad 54
http://maksoft.ch
An easier way to use buffers markers to copy/cut and paste. Got to beginning of the block you want to copy/delete.
Mark it with mletter
Go to the other end of the block and type y’letter.
Now you have yanked that block to the default buffer go to the desired location and paste the buffer with p or P.
The previous example uses the default buffer, but if you want to use named buffers:
– "c2yy will yank 2 lines into the name buffer c.– “c3,10y will yank lines 3 to 10 to buffer c.
Auther: Ahmed Maklad 55
http://maksoft.ch
Copy’n’paste In MotionBlock to be yanked
Block to be yanked
Block to be yanked
ma
y’a.
.
Insert Block below here
Block to be yanked
Block to be yanked
Block to be yanked
p
Auther: Ahmed Maklad 56
http://maksoft.ch
Cut’n’paste In MotionBlock to be moved
Block to be moved
Block to be moved
ma
d’a.
.
Insert Block below here
Block to be moved
Block to be moved
Block to be moved
p
Auther: Ahmed Maklad 57
http://maksoft.ch
Copy’n’paste In a buffer other than defaultBlock to be yanked
Block to be yanked
Block to be yanked
ma
y’a.
.
Insert Block below here
Block to be yanked
Block to be yanked
Block to be yanked
p
“b
“b
It would have been a good practice to use
the same buffer name as a the mark name
Auther: Ahmed Maklad 58
http://maksoft.ch
And yet another Copy’n’paste !Using temporary files
:10,20 w temp_flname.txt
– Writes the contents of lines 10 through 20 to a temporary file.
Move to the required position.
:r temp_flname.txt
– Reads the contents of the temporary file to the cursor’s position.
Auther: Ahmed Maklad 59
http://maksoft.ch
execute a UNIX command from vi !
:!cmd
– to execute the command. :x,y!cmd
– Execute a shell <cmd> [on lines x through y these lines will serve as input for <cmd> and will be replaced by its standard output.
:x,y!! arrgs
– Repeat last shell command [and append <args>]. :r!cmd
– Put the output of <cmd> onto a new line
Auther: Ahmed Maklad 60
http://maksoft.ch
More shell !
You shell out of vi, when you wish to execute more than one command.
– :sh gives you a new shell, and when you finish with the shell, ending it by typing a ^D, the editor will clear the screen and continue.
:1,$!sort
– This passes the file (between line 1 and last line) to the sort command and output (sorted) replaces the lines … nice for sorting file content, consider fmt, nroff ,cb , fold ,cut… etc
Auther: Ahmed Maklad 61
http://maksoft.ch
More to do with shell
:!spell %
– The % passes the filename to the spell command so eventually it becomes :!spell filename .
– This checks the spelling and displays a list of misspelled words at the bottom of screen.
:!spell % > %.sp
– The misspelled words are in the file filename.sp . :%!sort : (same as :.,$!sort )
– will pass the whole file to be sorted and replaced :!sort %
– Runs the command sort filename, display result and return to file without changing anything.
Auther: Ahmed Maklad 62
http://maksoft.ch
Fix text files from CR/LF windows characters Sometimes when a text file is not properly transferred
from windows to *NIX system, you see an extra line at the end of each line, which appears as “^M”.
To fix this you have to replace this CONTROL character in VI with nothing throughout the file, do the following:
:%s/[press CTRL-V][press CTRL-M]//g
Auther: Ahmed Maklad 63
http://maksoft.ch
Abbreviations
Another imported feature form ex to use abbreviations:
:ab Ex Example
– While in Insert Mode every time you type Ex , immediately it converts into Example.
:una Ex removes the Abbreviation for Ex
:ab Lists all registered abbreviations.
Auther: Ahmed Maklad 64
http://maksoft.ch
mapping commands
You could also map commands in Command and Insert mode.
:map key command_sequence
Auther: Ahmed Maklad 65
http://maksoft.ch
Maintain your preferences
1. The $HOME/.exrc file is executed automatically every time vi starts.
2. Also the EXINIT environmental variable could be used to set the vi into certain behavior.
– In .profile you could do the following:– export EXINIT– EXINIT='set ai nu wm=3|map g G'
Auther: Ahmed Maklad 66
http://maksoft.ch
Indention :set sw=4
Sets the Indention to 4 spaces.
<<
Shifts the current line to the left by one shift width.
>>
Shifts the current line to the right by one shift width.
4>>
Indents 4 lines once to the left.
:set autoindent
Very nice for our dear programmers.
Auther: Ahmed Maklad 67
http://maksoft.ch
set environment variable
:set ic Changes the environment so a search or substitute ignores case.
:set noic doesn’t ignore cases in searching.
Perform the following sequence on the file that contains multiple occurrences of Ignore, ignore and IGNORE.
– /Ignore – n– :set ic– /ignore
Auther: Ahmed Maklad 68
http://maksoft.ch
Line Numbers :set number
:set nonumber
Auther: Ahmed Maklad 69
http://maksoft.ch
Sources for Knowledgehttp://ce.uml.edu/vi.htmhttp://www.eng.hawaii.edu/Tutor/vi.htmlhttp://www.geog.ox.ac.uk/faq/vi.htmlhttp://unix.t-a-y-l-o-r.com/Vreference.html
Exerciseshttp://www.rice.edu/Computer/Documents/Unix/unix4.pdf