Using VI Editor
Bok, JongSoon
javaexpert@[Link]
[Link]
Linux Text Editors - gedit
Lab 1 : Installation gedit Plugins
Installation gedit Plugins (1/3)
1. $ sudo apt-get install –y gedit-plugins
Installation gedit Plugins (2/3)
2. Preferences > Plugins > Embedded Terminal
Installation gedit Plugins (3/3)
3. Check View > Bottom Panel
Linux Text Editors – Nano
Linux Text Editors - Emacs
Lab 2 : Installation Emacs on
Ubuntu 22.04 LTS
Installation Emacs (1/5)
1. $ sudo add-apt-repository ppa:kelleyk/emacs
Installation Emacs (2/5)
2. $ sudo apt update
Installation Emacs (3/5)
3. $ sudo apt-get install emacs28
Installation Emacs (4/5)
Installation Emacs (5/5)
Linux Text Editors – VI or VIM
Why use VI Editor?
Although easy-to-use graphical text editors, most power users still
use VI to edit text files.
VI will work from any shell (no GUI required).
Offer other advantages such as your hands never having to leave
the keyboard and integration with useful utilities.
Are usable over slow Internet connections such as dial-up or satellite.
What is VI ?
Is pronounced
Is a screen-oriented text editor
originally created for the Unix.
The original code was written by Bill Joy,
Chuck Haley in 1976.
The visual mode for a line editor called ex.
Bill Joy's ex 1.1 was released as part of the
first BSD Unix release in March, 1978.
What is VI ? (Cont.)
It was not until version 2.0 of ex, released as part of Second
BSD in May, 1979 that the editor was installed under the
name VI.
Why learning VI ?
Occasions
Operating Systems
Efficiency
Regular Expression
Quick-lunch keys
License
Etc.
Learning curves
Features
Powerful text editor The most letters with less number
Character based of keystrokes.
Interactive Syntax highlight for most
languages.
Cryptic
All natural languages.
Available on virtually ALL Unix
systems Thousands of plug-ins.
Designed to work with a Automatic backup (.swp)
alphanumeric only terminal (no Highly customizable
arrow keys, pgup, pgdw, etc) Without mouse
VI 3 Modes
VI 3 Modes (Cont.)
Six modes
NAME DESCRIPTION HELP PAGE
normal Navigation & Editing text :help Normal-mode
insert Inserting new text :help Insert-mode
visual Navigation & Manipulating selections :help visual-mode
select Similar to visual (but like MS-Window) :help select-mode
command-line Entering editor commands :help Command-line-mode
mode
Ex-mode Similar to command-line (but optimized for batch :help Ex-mode
processing)
VI Simple Cheat Sheet
Source : [Link]
VI Simple Cheat Sheet (Cont.)
Source from : [Link]
VI Simple Cheat Sheet (Cont.)
Source : [Link] File download from : [Link]
VIM Installation
[Link]
[Link]
VIM Installation (Cont.)
$ sudo apt install –y vim
VIM Installation (Cont.)
Using the Vim tutor
Can use the vimtutor to learn your first Vim commands.
On Linux/Unix, if Vim has been properly installed, can start it
from the shell:
$ vimtutor
Using the Vim tutor (Cont.)
Starting the VI
VI is working on a copy of your file in the buffer.
Will not affect original file until save the buffer.
$ vi [filename]
If the filename is omitted, VI will open an unnamed buffer.
A filename
Must be unique inside its directory.
Can include any ASCII character except a slash.
Starting the VI (Cont.)
1. $ cp /usr/share/doc/base-files/copyright /tmp
2. $ vi /tmp/copyright
Begin on line 10
$ vi +10 [filename]
Starting the VI (Cont.)
Begin editing file on the last line
$ vi + [filename]
Edit file in read-only mode
$ view [filename]
$ vi –R [filename]
To display line numbers
:set nu Return
Saving & Exiting the VI
First check that you are in command mode by pressing [ESC]
To save file and quit the VI:
ZZ
:wq
:x Return
To save file without quitting VI:
:w <filename> Return (write to specified file)
:w Return (write again)
:w! <filename> Return (to override existing file)
:w >> filename Return
To quit without saving changes:
:q! Return (Exit – DON’T save changes)
Basic Editing
Moving the Cursor
Up k
Down j
Left h
Right l
N <direction>
3h
5k
Moving the Cursor (Cont.)
Command Description
0 or | Moves to beginning of the current line.
^ Moves to the first letter of the current line
$ Moves to the end of the current line.
-(minus) Moves to beginning of previous line.
+(plus) Moves to beginning of next line
w Move cursor to next word.
b Move cursor to previous word.
e Move to the end of your word.
:$ Move to the last line.
G Move to the last line.
gg Move to the first line.
1G Move to the first line.
Moving the Cursor (Cont.)
Command Description
( Moves to beginning of sentence
) Moves to end of sentence
{ Moves to beginning of paragraph
} Moves to end of paragraph
H Moves to top of screen(window)
M Moves to middle of screen(window)
L Moves to bottom of screen(window)
:n Moves to the line number
nG Moves to the line number
Control Commands
Command Description
Ctrl+d Moves forward ½ screen full
Ctrl+u Moves backward ½ screen full
Ctrl+f Moves forward 1 screen full
Ctrl+b Moves backward 1 screen full
Ctrl+e Moves screen up one line
Ctrl+y Moves screen down one line
Ctrl+l Refresh screen (useful in telnet)
Entering insert mode in VI
Command Description
i Insert text before cursor
I Insert text at beginning of current line
a Append text after cursor
A Append text at end of current line
o Open new line above current line
O Open new line below current line
Deleting Characters in VI
Command Description
x Deletes the character under the cursor location.
X Deletes the character before the cursor location.
dw Deletes to the next word.
d^ Deletes to the beginning of the line.
d0 Deletes to the beginning of the line.
d$ Deletes to the end of the line.
D Deletes to the end of the current line.
dd Deletes the line the cursor is on.
Change Commands (Insert mode) in VI
Command Description
cc Change line, blanks line. To clean, ESC.
cw Change word to the $ sign. To clean, ESC.
c$ Change to end of line. To clean, ESC.
C Change to end of line. To clean, ESC.
r Replace current character
R Overwrite until ESC
s Substitute (ESC) – 1 char with string
S Substitute (ESC) – Rest of line with text
Copy and Paste Commands in VI
Command Description
yy Copies the current line.
yw Copies the current word.
p(lower) Puts the copied text after the cursor.
P(Upper) Puts the copied text before the cursor.
Using Visual Block Mode in VI
Command Description
v Start visual mode per character.
V Start visual mode linewise.
Ctrl+V Start visual mode blockwise.
y Copy
~ Switch case
d Delete
dd Delete current line
p Puts
c Change
Searching in VI
Command Description
/chars Return Search forward for chars
?chars Return Search backward for chars
N Repeat search in opposite direction
n Repeat search in same direction
/ Return Repeat search forward
? Return Repeat search backward
Regular Expressions (Search Strings)
Character Description
^ Matches beginning of line
. Matches any single character
* Matches any previous character
$ Matches end of line
.* Matches any character
Finding help
To get generic help use
:help
To get help on a given subject
:help subject
Ex) :help [Link]
To get help on the “x” command
:help x
To find out how to delete
:help deleting
Miscellaneous
Commands Description
J Join the current line with the next one.
<< Shifts the current line to the left by one shift width.
>> Shifts the current line to the right by one shift width.
~ Switch the case of the character under the cursor.
U Undo all changes on line
u Undo last change
Ctrl+u Redo
. Repeat last change
Miscellaneous (Cont.)
Commands Description
:!command Runs command from editor
:r filename Reads file and inserts it after current line.
:nr filename Reads file and inserts it after line n.
:e filename Opens another file with filename.
:f Displays current position in the file in % and
filename, total number of file.
:f filename Renames current file to filename.
VIM Tabs
To open many files in tabs
$ vi –p *.txt
To switch between tabs
Type gt
To create a new empty tab
:tabnew
To open a file in a new tab
:tabe filename
VIM Tabs (Cont.)
VIM Window
To split the current window horizontally
:split(:sp) or Ctrl + w, s
To split the current window vertically
:vertical split(:vs) or Ctrl + w, v
To switch between open windows
Ctrl + w, {h, j, k, l}
To close the current window
Ctrl + w, c
To close all windows except the current one.
Ctrl + w, o
VIM Window (Cont.)
VIM Window (Cont.)
To open a new file
:new or Ctrl + w, n
To open a file horizontally
:sp filename
To open a file vertically
:vsp filename
To switch between windows in command mode.
Ctrl + w {hjkl}
VIM Window (Cont.)
VIM Window (Cont.)
To maximize in current working window horizontally
Ctrl + w, _(underline)
To maximize in current working window vertically
Ctrl + w, |(pipe sign)
To divide equal size
Ctrl + w, =
VIM Buffer
List of open buffers
:ls
Previous buffer
:bp or :N
Next buffer
:bn or :n
Move to n’th buffer
:b NUMBER
To save current open file
:w
VIM Buffer (Cont.)
Using shell commands
To escape temporarily to a shell
:sh
To return from shell to VI
Ctrl + d
To execute shell command without leaving VI
:! command
Set Commands
Command Description
:set ic or :set ignorecase Ignores case when searching
:set ai or :set autoindent Sets auto indent
:set noai or :set noautoindent To unset auto indent.
:set nu or :set number Show line numbers
:set nonu or :set nonumber Don’t show line numbers
:set sm or :set showmode Display mode on last line of screen
:set nosm or :set noshowmode Turn off show mode
:set ts=n or :set tabstop=n Sets the width of a software tabstop
Set Commands (Cont.)
To display current values of VI variables
:set all
Using vimrc
Contains optional runtime configuration settings to initialize
Vim when it starts.
On Linux/Unix based systems, the file is named .vimrc.
Locate in $HOME.
Examples :help vimrc-intro
“ Always wrap long lines:
set wrap
“ Show line number :
set number
Lab 3 : Practice with VIM
Practice with VIM
1. $ cp /usr/share/doc/ubuntu-docs/copyright
vi_exercise.dat
2. $ vi vi_exercise.dat
Practice with VIM (Cont.)
VIM Reference
Tutorial
[Link]
[Link]
[Link]
[Link]
[Link]
[Link]
guide
VIM Learing Game
[Link]