Mastering the Linux vi Editor: Essential Commands and Techniques

12 views 46 slides Dec 10, 2024
Slide 1
Slide 1 of 46
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46

About This Presentation

Tags
Linux vi Editor
vi Commands
Linux Text Editor
Unix vi Editor
vi Tutorial
ViBasics
Command-Line Editors
vi Editor Guide
Linux File Editing
vi Modes
Text Editing in Linux
vi Shortcuts
Advanced vi Features
vi vs Vim
vi Navigation
Linux Command-Line Tools
vi Editor Tips
Customizing vi
vi Save and Q...


Slide Content

Linux- Vi Editor

vi & vim editorvi & vim editor
Vi is known as the visual editor.
It is a standard editor under Linux and Unix system.
Vim is the improved version of vi editor
Vi have three modes of operation
•Command mode
•Insert mode
•Esc mode

vi Modesvi Modes
Command mode: Operations such as cut, copy,
paste, delete, undo, redo etc., are done in this mode.
Insert mode: Type in new text
Ex-mode : Extended commands for saving, exiting,
search-and-replace can executed here

Working with vi editorWorking with vi editor
exam is a
file created
or opened
with the vi
editor.

Command ModeCommand Mode
Press ‘i’ to
get into
the insert
mode

Insert ModeInsert Mode
This is the
insert mode

After typing
the data’s hit
escape to get
into command
mode
Insert ModeInsert Mode

Ex- ModeEx- Mode
Press :w
q to write
and quit

Ex- ModeEx- Mode
Press q!
to quit
without
saving
the data

YankYank
Hit escape and
type 3yy to yank
three lines from
the cursor position
Like this example,
you can copy any
number of lines by
typing numbers of
lines and yy

In the previous
slide, we saw how
to yank the text.
After yanking we
see the message
as below
Text yankedText yanked

Now the yanked
text is pasted from
the cursor position
by hitting the ‘p‘
Text putText put

deletedelete
To delete text, place
the cursor in the
beginning of the line
and give the total
number and dd

Now 3 lines are
deleted and
message get
displayed as 3
fewer lines as
shown below
deletedelete

cut and putcut and put
To cut a line
after curser
position hit 1cc.

Get into insert mode
and place the cursor
where the data should
be pasted. Press esc
to get in to command
mode
cut and putcut and put

Now hit p to
paste the data.
cut and putcut and put

Input Mode CommandsInput Mode Commands
 i Insert text to left of the cursor(Existing
text shifted right)
 a Append text to right of cursor(Existing
text shifted right)
 I Insert text at the beginning of line
 A Append text at the end of the line
 o Open line below
 O Open line above
 rch Replaces single character under cursor
with ch

Input Mode CommandsInput Mode Commands
 R Replaces text from cursor to right
 s Replaces single character under
cursor with any number of characters
 S Replaces entire line

 Advanced Uses of VI and
VIM

File RepositioningFile Repositioning
Press G, Cursor go to last line of a file.
Press 1G, Cursor go to first line of a file.
Press Ctrl+f, Cursor forward one full screen.
Press Ctrl+b, Cursor go back one full screen.
Press Ctrl+d, Cursor go down half a screen.
Press Ctrl+u, Cursor go up half a screen.

Screen RepositioningScreen Repositioning
Press H, Cursor go to first line on screen.
Press M, Cursor go to middle line on screen.
Press L, Cursor go to last line on screen.
Press Z+Enter, Make current line is first line on screen.
Press - (hyphen) Make current line last line on screen.

It is also possible to read a other file content in
your current files.
The following command help you :
–Go to exmode in a vi editor
–Syntax : r <filename>
–Example :r /root/file1
While working in a editor we can copy the file
content to another file
–Syntax : starting line, endingline w <file name>
–Example : 1,20w file2
–Example : 21,40w >> file2
Advanced Reading and SavingAdvanced Reading and Saving

FilteringFiltering
A powerful feature of vi is the ability to include into
your document the output of Linux commands.
For example if you want to display any command
output in your file see following syntax .
 !!command
The syntax will be working in exmod in editor.
For example to display date ,type following command
in exmod.
 !!date

FilteringFiltering
Open the file
in vi mode.

This will replace
the current line
with output of the
date command.
FilteringFiltering

The output of the
date command is
written here.
FilteringFiltering

SearchSearch
Get into ex mode
type / followed by
data to be
searched and
press enter

When we press enter
it shows the first
found match from
bottom to top.
SearchSearch

Then if we press ‘n’ it will
shows the second match
for that data.If we continue
pressing n it will show
successive matches.
SearchSearch

ex mode : search and replaceex mode : search and replace
vi can perform search and replace operations
much like sed command.
 Syntax:
 ex mode:1,$s/searchname/replacename/g
 1,$ - address starting from line one.
 s - String of character
 g - replace multiple matches in a line

Ex Mode

It is also possible to read a other file content
in your current files.
The following command help you :
–Go to ex mode in a vi editor
–Syntax : r <filename>
–Example :r /root/file1
While working in a editor we can copy the file
content to another file
–Syntax : starting line, ending line w <file name>
–Example : 1,20w file2
–Example : 21,40w >> file2
Advanced Reading and SavingAdvanced Reading and Saving

Here you can view
output of test file.
Open a file in vi
mode.
Advanced Reading and SavingAdvanced Reading and Saving

To display the
output of another
file named test in
the current file.
Advanced Reading and SavingAdvanced Reading and Saving

The output of test
file is displayed..
Advanced Reading and SavingAdvanced Reading and Saving

The contents of the
existing file.
Advanced Reading and SavingAdvanced Reading and Saving

To write the contents of
the existing file in
another file named test1.
Advanced Reading and SavingAdvanced Reading and Saving

The contents are
appended to the
test1 file.
Advanced Reading and SavingAdvanced Reading and Saving

You can see the
appended contents
in the test1 file.
Advanced Reading and SavingAdvanced Reading and Saving

Configuring VI and VIMConfiguring VI and VIM
Number of configuration are available in vi editor, see some of
example .
:set number - shows a line number on the left margin in a vi
editor
:set showmatch - when a right curly braces or a right
paranthesis is typed, a cursor will jump to the matching left curly
braces or left paranthesis.
:set all - it shows all configurations.

Set numberSet number
File contents.

To assign numbers
to all line in vi
editor.
Set numberSet number

Set allSet all
To view all the
possibilities of
configurations
available in vi
editor.

All the available
option are
displayed here.
Set allSet all

Q & AQ & A