SlideShare a Scribd company logo
1 of 69
Download to read offline
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
License
Creative 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)
i
j
k
h
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 objects
The 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 pP
p (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 Motion
Block 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 Motion
Block 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 default
Block 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 Knowledge
http://ce.uml.edu/vi.htm
http://www.eng.hawaii.edu/Tutor/vi.html
http://www.geog.ox.ac.uk/faq/vi.html
http://unix.t-a-y-l-o-r.com/Vreference.html
Exercises
http://www.rice.edu/Computer/Documents/Unix

More Related Content

Viewers also liked

Viewers also liked (7)

Dover beach
Dover beachDover beach
Dover beach
 
Matthew Arnold
Matthew ArnoldMatthew Arnold
Matthew Arnold
 
methew arnold
methew arnoldmethew arnold
methew arnold
 
The Study Of Poetry And Dover Beach
The Study Of Poetry And Dover BeachThe Study Of Poetry And Dover Beach
The Study Of Poetry And Dover Beach
 
Dover beach
Dover beachDover beach
Dover beach
 
Dover Beach
Dover BeachDover Beach
Dover Beach
 
The Study of Poetry - Matthew Arnold
The Study of Poetry - Matthew ArnoldThe Study of Poetry - Matthew Arnold
The Study of Poetry - Matthew Arnold
 

Similar to Vi Editor in 100 minutes

Similar to Vi Editor in 100 minutes (20)

Vi editor
Vi editorVi editor
Vi editor
 
Introduction to Vim, the text editor
Introduction to Vim, the text editorIntroduction to Vim, the text editor
Introduction to Vim, the text editor
 
Logo tutorial
Logo tutorialLogo tutorial
Logo tutorial
 
Tuffarsi in vim
Tuffarsi in vimTuffarsi in vim
Tuffarsi in vim
 
Unix - Filters/Editors
Unix - Filters/EditorsUnix - Filters/Editors
Unix - Filters/Editors
 
Introduction to Command Line & Batch files
Introduction to Command Line& Batch filesIntroduction to Command Line& Batch files
Introduction to Command Line & Batch files
 
Unix Tutorial
Unix TutorialUnix Tutorial
Unix Tutorial
 
GNU Emacs Reference Card.pdf
GNU Emacs Reference Card.pdfGNU Emacs Reference Card.pdf
GNU Emacs Reference Card.pdf
 
1_Editors_in_Unix
1_Editors_in_Unix1_Editors_in_Unix
1_Editors_in_Unix
 
Beginning with vi text editor
Beginning with vi text editorBeginning with vi text editor
Beginning with vi text editor
 
Introduction to Input/Output Functions in C
Introduction to Input/Output Functions in CIntroduction to Input/Output Functions in C
Introduction to Input/Output Functions in C
 
Mission vim possible-full
Mission vim possible-fullMission vim possible-full
Mission vim possible-full
 
Functions of stdio conio
Functions of stdio   conio Functions of stdio   conio
Functions of stdio conio
 
Doscommands
DoscommandsDoscommands
Doscommands
 
Doscommands
DoscommandsDoscommands
Doscommands
 
101 3.8.2 vim reference card
101 3.8.2 vim reference card101 3.8.2 vim reference card
101 3.8.2 vim reference card
 
3.8.b vim reference card
3.8.b vim reference card3.8.b vim reference card
3.8.b vim reference card
 
Vi editor commands
Vi editor commandsVi editor commands
Vi editor commands
 
Foss manual (1)
Foss manual (1)Foss manual (1)
Foss manual (1)
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 

Recently uploaded

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 

Recently uploaded (20)

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 

Vi Editor in 100 minutes

  • 1. 1 Viin 100 minutes. The Editor which no one knows how to use !
  • 3. Auther: Ahmed Maklad 3 http://maksoft.ch License Creative 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.
  • 4. 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
  • 5. 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.
  • 6. 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.
  • 7. 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.
  • 8. 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-[
  • 9. Auther: Ahmed Maklad 9 http://maksoft.ch Moving the cursor (command Mode) i j k h
  • 10. Auther: Ahmed Maklad 10 http://maksoft.ch Moving the cursor (command Mode)  Moving to end and beginning of a word WordWordWordWord eb
  • 11. 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
  • 12. 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.
  • 13. 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.
  • 14. 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.
  • 15. Auther: Ahmed Maklad 15 http://maksoft.ch the open (line) command Current line o O
  • 16. 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.
  • 17. 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)
  • 18. 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.
  • 19. 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
  • 20. Auther: Ahmed Maklad 20 http://maksoft.ch text editing - deletion  While in Command Mode press x to delete the character under the cursor.
  • 21. 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.
  • 22. Auther: Ahmed Maklad 22 http://maksoft.ch On commands and objects The 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
  • 23. 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.
  • 24. Auther: Ahmed Maklad 24 http://maksoft.ch text editing - insertion  Press i and type in the necessary additions .
  • 25. 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
  • 26. 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
  • 27. 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.
  • 28. 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.
  • 29. 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.
  • 30. 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
  • 31. Auther: Ahmed Maklad 31 http://maksoft.ch Searching (Cont.) /pattern fx tx ?pattern Fx Tx Find Next : n Find Previous : N
  • 32. 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.
  • 33. Auther: Ahmed Maklad 33 http://maksoft.ch How to describe a pattern (Cont.): Special characters: This is a Text line. …………………. ^ $
  • 34. 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).
  • 35. 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)
  • 36. 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)
  • 37. 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.
  • 38. 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).
  • 39. 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 !
  • 40. 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.
  • 41. 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.
  • 42. 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
  • 43. 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.
  • 44. 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).
  • 45. 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.
  • 46. 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.
  • 47. 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.
  • 48. 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.
  • 49. 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,
  • 50. Auther: Ahmed Maklad 50 http://maksoft.ch Pasting Cursor Position pP p (small) pasts buffer After Cursor Position P (Capital) pasts buffer Before Cursor Position
  • 51. 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
  • 52. 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
  • 53. 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)
  • 54. 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.
  • 55. Auther: Ahmed Maklad 55 http://maksoft.ch Copy’n’paste In Motion Block 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
  • 56. Auther: Ahmed Maklad 56 http://maksoft.ch Cut’n’paste In Motion Block 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
  • 57. Auther: Ahmed Maklad 57 http://maksoft.ch Copy’n’paste In a buffer other than default Block 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
  • 58. 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.
  • 59. 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
  • 60. 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
  • 61. 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.
  • 62. 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
  • 63. 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.
  • 64. Auther: Ahmed Maklad 64 http://maksoft.ch mapping commands  You could also map commands in Command and Insert mode.  :map key command_sequence
  • 65. 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'
  • 66. 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.
  • 67. 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
  • 68. Auther: Ahmed Maklad 68 http://maksoft.ch Line Numbers  :set number  :set nonumber
  • 69. Auther: Ahmed Maklad 69 http://maksoft.ch Sources for Knowledge http://ce.uml.edu/vi.htm http://www.eng.hawaii.edu/Tutor/vi.html http://www.geog.ox.ac.uk/faq/vi.html http://unix.t-a-y-l-o-r.com/Vreference.html Exercises http://www.rice.edu/Computer/Documents/Unix