0% found this document useful (0 votes)
19 views16 pages

02 VI ELEC462

Uploaded by

이정화
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)
19 views16 pages

02 VI ELEC462

Uploaded by

이정화
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/ 16

vi Editor Tutorial

Dukyun Nam
HPC Lab@KNU

1
About vi
● vi (pronounced as distinct letters, /ˌviːˈaɪ/)
○ A screen-oriented text editor
○ The standard Unix editor
○ It is short for “visual”
○ There are lots of clones (vim, Elvis, nvi, etc.)

< ADM-3A terminal keyboard layout > < ADM-3A terminal with keypad >
* https://en.wikipedia.org/wiki/Vi 2
Entering the vi Editor
Command Description
vi filename Edits an existing filename or creates a new one if
filename does not exist.
vi –r filename Edits filename using .filename.swp as the input .
The file .filename.swp must be deleted, using:
rm .filename.swp, after use.
vi –R filename Edits filename in read-only mode does not allow
changes to filename.
vi +/pattern filename Edits filename at the line containing the pattern.

3
File Interaction in the vi Editor
Command Description
:w Write to the file that your are editing without exiting the editor.
Good for intermediate saves.
:w def Write to a new file def, but stay in the existing file.
:w! def Write to an existing file def, but stay in the existing file.
:r def add (read) file def into the current file
:f Provides information on the file currently editing, including the
current line number, if it has been modified etc. <Ctrl+g>

4
Exiting the vi Editor
Command Description
:q Exit (quit) without changes. Assumes no changes have been
made.
:q! Exit (quit) without changes. Assumes changes have been made.
ZZ or :x Save the file only if changes have been made, then exit the
editor
:wq Save the file and exit the editor. This command will save the file
even if no changes are made.

5
vi Modes
● Command mode:
○ The mode you are in when you start (default mode)
○ The mode in which commands are given to move around in the file, to make
changes, and to leave the file
● Insert (or Text) mode
○ The mode in which text is created
○ There is more than one way to get into insert mode but only one way to
leave: return to command mode by pressing <Esc>

6
vi Modes (cont.)

7
vi Modes (cont.)

8
From Command Mode to Insert Mode
Key Operation

i Insert text before current character

a Append text after current character

I Begin text insertion at the beginning of a line

A Append text at end of a line

o Open a new line below current line

O Open a new line above current line

9
Command Mode
Key Operation
● Basic cursor movement
k Up one line
○ h, j, k, l j Down one line

○ Arrow key l Right one character


(or use <Spacebar>)
■ Only support in vim
h Left one character

w Right one word

b Left one word

W forward one word

B back one word

gg go to the first line

G go to the last line

10
Command Mode (cont.)
● General command syntax
○ The general format for commands is
ncm
○ Where:
■ n is an optional multiplier value
■ c is the command
■ m is an optional scale modifier
○ Examples:
■ 3dw – delete 3 special character delimited words
■ dW – delete a single, space, delimited word

11
Command Mode (cont.)
● Delete commands
d Delete this line

w Delete special character delimited word

W Delete whitespace delimited word

} Delete to next paragraph

^ Delete to beginning of the line

● Delete character commands


nx Delete current [and n-1] character[s]

nX Delete previous n character[s]

12
Command Mode (cont.)
● Splitting and joining lines
○ If you wish to split a line between words, position yourself on a space between the words to be
split, then use the replace (r) followed by the return key. This will replace the space with a
carriage return forming a new line.
○ If you wish to join or combine two lines go to the upper of the two lines to be joined, then key in
the uppercase “J” for join.
● Example:
○ We wish to split the line below between the word “should” and “be”.
“This line should be split.”
1. Position the cursor here
2. Key in “r” followed by “enter”
○ To join the lines:
“This line should 1. Position the cursor anywhere on this line
2. Key in “J”
be split.”

13
Command Mode (cont.)
● Search commands
/text Search for text forward (wraps to beginning)

?text Search for text backward


n Search for same text again
N Search for text reverse direction of initial search

14
Command Mode (cont.)
● Copy/Paste commands
[n]Y or Copy (yank) n lines
[n]ym Copy (yank) a portion of the file determined by the measurement
(e.g. 2yW will yank 2 space separated words.
P,p Paste yanked or deleted data before (P), after (p) cursor position

■ Note: to cut and paste, use the dm commands to delete and the p or P to paste

● Visual block command


v Start Visual mode per character
V Start Visual mode linewise

15
Command Mode (cont.)
● Some “nice to know” features
○ To turn line numbers on and off use:
■ :set nu and :set nu!
○ To execute a single Unix command from inside the editor use:
■ :!cmd
○ To go to linenum
■ :linenum
○ To temporarily return to the shell use:
■ :sh (type “exit” to return to editor)
○ To repeat a colon command or to go back to earlier colon commands use:
■ : ↑ or↓(up arrow or down arrow)
○ To show the name of the current file use:
■ :f
● Resource: https://www.fprintf.net/vimCheatSheet.html
16

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