Useful tips for vi
vi is one of the most frequently used editing tools for the unix operating system. vim is more powerful editor. It has so much functions, it is hard to remember each one. The following notes can be referenced when you don’t remember the commands.
vi configuration settings
:set : Displays all the vi variables that are set
:set all : Displays all vi variables and their current values
:set tabstop=4 : specify how many positions a tab character will jump
:set shiftwidth=4 : shift interval
:set ignorecase : ignore case when performing a search, substitution
:set noignorecase
:set number, :set nu : Shows line numbers
:set nonumber, :set nonu : Hides line numbers
:set ai : set autoindent(ai) always set autointenting on
:set noai
:set paste : off autoindenting when paste
:set list : Turns on the display of invisible characters such as \t, \r, \n
:set nolist : Turns off the display of invisible characters
:set hlsearch : highlight searches
:set wrap : wrap long lines
:set nowrap
:set showmode : Displays the current mode of operation
:set noshowmode : Turns off the mode of operation display
:set tenc=korea
:set enc=euc-kr
:set enc=utf-8
vi commands
Open
vi <filename> Open files
vi +n <filename>
vi + <filename>
vi -r filename If the system crashes while you are editing a file, you can use the -r option to recover the file.
vi -R filename The file opens in read-only mode to prevent accidental overwriting of the contents of the file
Input mode
You can insert text into a file in the input mode. The vi editor interprets everything you type in the input mode as text. To invoke input mode, press one of the following lowercase keys:
a : Appends text after the cursor
A : Appends text at the end of the line
i : Inserts text before the cursor
I : Inserts text at the beginning of the line
o : Opens a new blank line below the cursor
O : Opens a new blank line above the cursor
Command mode
The command mode is the default mode for the vi editor. In this mode, you can run commands to delete, change, copy, and move text. You can also position the cursor, search for text strings, and exit the vi editor.
Moving the cursor
Ctrl-B - Previous Page
Ctrl-F - Next Page
b - Previous Word by space
B - Previous Word
w - Next Word
W - Next Word by space
e - Move to the end of word
E - Move to the end of word by space
0 - move the cursor to the beginning of the current line
$ - move the cursor to the end of the current line
l - move the cursor one character to the right
h - move the cursor one character to the left
k,↑ - move the cursor up one row
j,↓ - move the cursor down one row
G - move the cursor to the bottom of the file
1G - move the cursor to the top of the file
H - move the cursor to the top row on the screen
M - move the cursor to the middle row on the screen
L - move the cursor to the last row on the screen
^,0 Move to the first CHAR of the line
[Enter] Move to next line
- Move to the first CHAR <*> - 1 lines lower.
) Move to * sentences forward
( Move to * sentences backward
} Move to * paragraphs forward
{ Move to * paragraphs backward
[Ctrl] + D
[Ctrl] + U
nG :n
z.
z Enter
Deleting text
R Overwrites or replaces characters on the line at and to the right of the cursor. To terminate this operation, press Escape.
C Changes or overwrites characters from the cursor to the end of the line
s Substitutes a string for a character at the cursor
x Deletes a character at the cursor
dw Deletes a word or part of the word to the right of the cursor
dd Deletes the line containing the cursor
D Deletes the line from the cursor to the right end of the line
:n,nd Deletes lines n -n (For example, :5,10d deletes lines 5-10.)
dG - delete to bottom of file
d) - delete a sentence (sentences are delimited by TWO spaces)
d% - delete to the matching bracket, brace, or parenthesis
d” - delete to the last place we jumped from
dtp - delete through the letter p, but leaves the p
dfp - delete to and including the letter p
nx, nX, ndw, ndw
Editing
cw : Changes or overwrites characters at the cursor location to the end of that word
r : Replaces the character at the cursor with one other character
J : Joins the current line and the line below
xp : Transposes the character at the cursor and the character to the right of the cursor
~ : Changes letter casing to uppercase or lowercase, at the cursor
u : Undoes the previous command
U : Undoes all changes to the current line
. - repeats the last command
x - delete the character under the cursor
r* - replacs the character under the cursor with *
sstring ESC - substitute the character under the cursor with string
cwstring ESC - change current word to string
c$string ESC - change from cursor to end of line with string
ccstring ESC - change current line to string
Cut & Paste
yy : yank lines into a temporary buffer, where they are saved for later use
p paste the previously yanked lines
The yanked lines can be put as often as one likes, which is a good way to repeat things. Also, deleted lines can be put as well, so dd is the same as yy dd. Also, there is only one default buffer. To move two things, or to yank and put with other work in between, you can save to named buffers. For example “ayy will yank a line in buffer a. Then buffer a can be pasted using “ap.
Pasting from a file
:r name : read in the file name at the point of the cursor
:r!cmd : execute the shell command cmd and inserts output at the point of the cursor
Search and Replace Commands
/aszBuffer [Enter] aszBuffer
?aszBuffer [Enter] aszBuffer
n
N
r
R
Etc
J
.
~
ZZ :q!
[Ctrl] + G
[Ctrl] + L
nyw ynw
nyy
ncw
cnl Change n letters (ex: c2l), l means letter
c0, c$
"ayy buffer name : a~z
"ap buffer name : a~z
"Ayy
Y
p
P
mx
'x
!!command run shell command
!!cat aa.txt :r <filename>)
Last line mode
You can use advanced editing commands in the last line mode. To access the last line mode, enter a colon (:) while in the command mode. Entering the colon (:) character places the cursor at the bottom line of the screen.
Change and Replace
:<range> s/pattern/string/cgiI
. <range> : 1,50 1,$ %
. s : substitute
. <pattern>
. <string>
- For each line in the range replace a match of the pattern with the string where:
c Confirm each substitution
g Replace all occurrences in the line (without g - only first).
i Ignore case for the pattern.
I Don't ignore case for the pattern.
ex) :1,50 s/DOS/sos/g
- g : replace all
- /gc : replace all with checking
Save and Quit Commands
:w - write the file and save the changes
:w name - write the file and save the changes into a file with the new name
:x,y w name - write only the lines x to y to the file
:wq - write and quit
:w [<filename>] Saves the file with changes by writing to the disk
:[address] w <filename> Saves some parts
:[address] w!
:w test Save data to the file test
:w Save data
:1, 50 w test Write to test file from line 1 to 50
:1, 50 w! Save from line 1 to 50 by force
:q quit
:q! Quits without saving changes
Etc
:r
vi tips
-
Copy some data of file and paste copy some data from out.txt and paste to aa.txt vi aa.txt :e out.txt mk “ay’k :e # “ap => mk mark with name k => “a Use buffer => y’k yank from mark to current position (‘k : move to the marked point) => :e # Move to the previous editing file => “ap Paste buffered data
-
sqlplus editor SQL>define _editor=vi
-
shell script EDITOR=vi; export EDITOR (export EDITOR=vi) set -o vi
-
vi tab : $HOME/.exrc (set ts=4)
Find and replace
/aa/_&_/g
---> From aa to _aa_
/\(ab\)\(cd\)/\2\1/g
abcd ---> cdab
/\(ab\)/\=printf("%4s", submatch(1))/g
---> formatting
Source code
- Sort : Blocking and = command
- Move to the start of the function : [[
- Move to the end of the function : ]]
- Move to the previous position : ‘’
Leave a comment