0% found this document useful (0 votes)
13 views22 pages

Unit 7 The VI Editor: Course Code LX13 Linux Basics

Unit 7 covers the vi editor, teaching students how to create, edit, and manipulate files using its command and input modes. Key features include flexible text manipulation, search and replace functions, and the ability to execute Linux commands from within vi. The unit also provides a cheat sheet for quick reference on commands and options available in the vi editor.

Uploaded by

mhmdfoad66
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views22 pages

Unit 7 The VI Editor: Course Code LX13 Linux Basics

Unit 7 covers the vi editor, teaching students how to create, edit, and manipulate files using its command and input modes. Key features include flexible text manipulation, search and replace functions, and the ability to execute Linux commands from within vi. The unit also provides a cheat sheet for quick reference on commands and options available in the vi editor.

Uploaded by

mhmdfoad66
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Unit 7 The vi Editor

Course code LX13


Linux Basics
Objectives

After completing this unit, students should be able to use


vi to:
Create and edit files
Manipulate text within a file
Set up defaults for the vi editor
Execute command-line editing
Define the uses for the other forms of vi
vi

Full-screen editor
Two modes of operation
command
text
Utilizes one-letter command
Does not format text
Flexible search and replace facility with pattern matching
Allows for user-defined editing functions using macros
Starting vi

$ vi vifile
~
~
~
~
~
~
~
~
~
~
~
~
"vifile" New file
Two Modes

Command Input
mode mode
a append text after cursor
A append text at end of line
i insert text at the cursor
I insert text at beginning of line
o add an empty line under cursor
O add an empty line above cursor
s substitute a character with text

<ESC>
Cursor Movement

<ctrl-b>
H
k
<up-arrow>
0 $
b,B w,W
h l
<left-arrow> <right-arrow>
<down-arrow>
j
L
<ctrl-f>
G
Editing Text

To delete a single character x


To delete to the end of the current word dw
To delete to the end of the line d$
To delete to the start of the line d0
To delete the whole line dd
To delete a range of lines :20,40d
Replace text by overtyping Rnewtext
To change to the end of next word c2w
Undo the last change u
Adding Text

Keystroke
i This file contains some lines.
Line 2.
And this is line 3.
Line 4 follows line 3.
The last line is line 5.
~
~
~
~
~
~
~
"vifile" New file
Exiting the Editor

Keystroke
This file contains some lines.
<esc> Line 2.
And this is line 3.
Line 4 follows line 3.
The last line is line 5.
~
~
~
~
~
~
~
:wq
Search for a Pattern

Keystroke
<esc> This file contains some lines.
Line 2.
And this is line 3.
n Line 4 follows line 3.
The last line is line 5.
~
~
~
~
~
~
~
/line
Find and Replace

Keystroke
<esc> This file contains some lines.
Line 2.
And this is lINE 3.
Line 4 follows lINE 3.
The last lINE is lINE 5.
~
~
~
~
~
~
~
:g/ line /s// lINE /g
Cut and Paste

Keystroke This file contains some lines.


<esc> Line 2.
And this is lINE 3.
The last lINE is lINE 5.
dd Line 4 follows lINE 3.
~
p ~
~
~
~
~
~
"vifile" 5 lines, 108 characters
Copy and Paste

Keystroke
<esc> This file contains some lines.
Line 2.
And this is lINE 3.
yy Line 4 follows lINE 3.
The last line is lINE 5.
p Line 4 follows lINE 3.
~
~
~
~
~
~
"vifile" 5 lines, 108 characters
vi - Executing Linux Commands

You can run Linux commands from within vi.

The vi command :! command will execute command in


a subshell. After the completion of command, you will be
placed back in vi.

Examples:
:! ls will execute the ls command
:! bash will give you a shell prompt in which you
can enter a series of commands.
vi Options

Options entered in the vi session change the behavior


of the vi editor:
:set all
:set autoindent/noautoindent
:set number/nonumber
:set list/nolist
:set showmode/noshowmode
:set tabstop=x
:set ignorecase/noignorecase
:set wrapmargin=x

Options can be stored in $HOME/.exrc


Macros can be written and new commands created
Example .exrc

set showmode

set tabstop=5
set list

ab lx linux
ab thansk thanks
ab sohw show
~
~
~
~
~
".exrc" 8 lines, 81 characters
vi Editors

vi Full-screen, full-function editor

view Read-only form of vim. Changes cannot be


saved unless overridden with a force (!)

ex Subset of vim working in line mode, can


access the screen editing capabilities of vim

ed Subset of vim working in line mode, can


access the screen editing capabilities of vim
Command Line Editing

Uses the same keystrokes as vi


Can correct mistakes in the current line
Uses editor keys to edit/re-enter previous lines

$ set -o vi

h j k l
vi Cheat Sheet
Start vi Change text
$ vi file name cw R

Exit vi Delete text


:wq :x :ZZ :q x dd dw

Force Copy/Paste
! yy p

Go to input mode Undo


i a o I A O s u

Go to command mode Cursor Movement


<Esc> h j k l
Checkpoint

1. When using the vi editor, what are the two modes of


operation?

2. While using vi, how do you get to command mode?

3. Which of the following could you use to enter in text?


a. a
b. x
c. i
d. dd
Checkpoint (2)

4. vi can be used to globally change the first occurrence


of a pattern on every line with a given pattern.
True or False?
Unit Summary

The VIM editor is started with the vi command


The VIM editor starts a full-screen editor
VIM has two modes of operation: text mode (input) and
command mode
VIM makes a copy of the file you are editing in an edit
buffer. The contents are not changed until you save the
changes
Subcommands with the : , / , ? , ! read input from a line
displayed at the bottom of the screen.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy