Asset Publisher Asset Publisher

Back

VI Linux/Unix Editor

http://alphanrrrd.org/images/vi.gif

‘vi’ is a Linux best and famous editor on Linux/Unix environment. People who wish to master and advance their skills beyond the basic featuring of a normal editor. ‘vi’ has a huge collection of commands available for beginner and advance users.

The advantage of learning vi and learning it well is that one will find vi on all Unix based systems and it does not consume an inordinate amount of system resources. Vi works great over slow network ppp modem connections and on systems of limited resources. One can completely utilize vi without departing a single finger from the keyboard. (No hand to mouse and return to keyboard latency)

NOTE: Microsoft PC Notepad users who do not wish to use "vi" should use "gedit" (GNOME edit) or "gnp" (GNOME Note Pad) on Linux. This is very similar in operation to the Microsoft Windows editor, "Notepad". (Other Unix systems GUI editors: "dtpad", which can be found in /usr/dt/bin/dtpad for AIX, vuepad on HP/UX, or xedit on all Unix systems.)

Basic "vi" features

One edits a file in vi by issuing the command: vi file-to-edit.txt

The vi editor has three modes, command mode, insert mode and command line mode.

  1. Command mode: letters or sequence of letters interactively command vi. Commands are case sensitive. The ESC key can end a command.
  2. Insert mode: Text is inserted. The ESC key ends insert mode and returns you to command mode. One can enter insert mode with the "i" (insert), "a" (insert after), "A" (insert at end of line), "o" (open new line after current line) or "O" (Open line above current line) commands.
  3. Command line mode: One enters this mode by typing ":" which puts the command line entry at the foot of the screen.

Partial list of interactive commands:

Cursor Movement Commands:

Keystrokes

Action

h/j/k/l

Move cursor left/down/up/right

spacebar

Move cursor right one space

-/+

Move cursor down/up in first column

ctrl-d
n ctrl-d

Scroll down one half of a screen.
Set scroll to "n" lines. New default set for half screen.

ctrl-u
n ctrl-u

Scroll up one half of a screen
Set scroll to "n" lines. New default set for half screen.

ctrl-f
n ctrl-f

Scroll forward one screen
Scroll forward "n" screen

ctrl-b
n ctrl-b

Scroll back one screen
Scroll back "n" screen

ctrl-y
n ctrl-y

Scroll forward one line
Scroll forward "n" lines

ctrl-e
n ctrl-e

Scroll back one line
Scroll back "n" lines

M (shift-m)

Move cursor to middle of page

H (shift-h)

Move cursor to top of page

L (shift-l)

Move cursor to bottom of page

W
w
5w

Move cursor a word at a time (white space delimited)
Move cursor a word at a time (first non-alphanumeric)
Move cursor ahead 5 words

B
b
5b

Move cursor back a word at a time (white space delimited)
Move cursor back a word at a time (first non-alphanumeric)
Move cursor back 5 words

E
e
5e

Move cursor to end of word (white space delimited)
Move cursor to end of word (first non-alphanumeric)
Move cursor ahead to the end of the 5th word

0 (zero)

Move cursor to beginning of line

:30

Move cursor to line thirty

$

Move cursor to end of line

)

Move cursor to beginning of next sentence (delimeted by ".", "?" or "!")

(

Move cursor to beginning of current sentence

}

Move cursor to beginning of next paragraph (delimeted by blank line or nroff macros: .IP, .LP, .PP, .QP, .P, .LI and .bp) Also see "set paragraphs" to define a paragraph.

{

Move cursor to beginning of current paragraph

]]

Move cursor to beginning of next section (delimeted by nroff macros: .NH, .SH, .H, .HU). Also see "set sections" to define a section.

[[

Move cursor to beginning of current section

G

Move cursor to end of file

%

Move cursor to the matching bracket.
Place cursor on {}[]() and type "%".
Use the matchit or xmledit plug-in to extend this capability to XML/XHTML tags.

'.

Move cursor to previously modified line.

m
ma

Mark the line on which the cursor resides. Marking requires an identifier.
Mark the line as identified by the letter "a" by marking with keystroke "ma"

'a

Move cursor to line mark "a" generated by marking with keystroke "ma"

'A

Move cursor to line mark "A" (global between buffers) generated by marking with keystroke "mA"

]'

Move cursor to next lower case mark.

['

Move cursor to previous lower case mark.

Editing Commands:

Keystrokes

Action

i

Insert at cursor. Puts you in insert mode. Must use esc key to terminate insert mode.

I

Insert before the cursor. Puts you in insert mode. Must use esc key to terminate insert mode.

a

Append after cursor. Puts you in insert mode. Must use esc key to terminate insert mode.

A

Append at end of line. Puts you in insert mode. Must use esc key to terminate insert mode.

o

Open a new line below the current cursor position. Also puts you in insert mode. Must use esc key to terminate insert mode.

O

Open a new line above the current line. Also puts you in insert mode. Must use esc key to terminate insert mode.

ESC

Terminate insert mode. Terminates most other modes as well.

u

Undo last change

U

Undo all changes to entire line

dd
3dd

Delete line (stored in local buffer)
Delete 3 lines (stored in local buffer).

D

Delete contents of line after cursor

C

Delete contents of line after cursor and insert new text. Press esc key to end insertion.

dw
4dw
d)
d$
d-
dfx
d'x
'ad'b
d/cat

Delete word
Delete 4 words
Delete to end of sentence
Delete all characters from cursor to end of line
Delete current and previous line
Delete from cursor to first occurance of the letter "x"
Delete from the current line to the line marked with the identifier "x"
Delete from the line of mark "a" to the line marked "b".
Delete all characters from the cursor to the next occurance of (but not including) "cat"

cw
c)
c$

Change word
Change sentence
Change from cursor to end of line
(See "d" delete above for other variations)

x

Delete character at cursor

X

Delete character before cursor

Y
or
yy

Yank (copy) current line into "unnamed" storage buffer.

p

Paste unnamed storage buffer after current line.

P

Paste unnamed storage buffer before current line.

r

Replace character

R

Overwrite characters from cursor onward

s

Substitute one character under cursor continue to insert

S

Substitute entire line and begin to insert at beginning of line

J

Join current and following line into one line

~

Change case of individual character

ctrl-a
ctrl-x

Increment number under the cursor.
Decrement number under the cursor.

.

repeat last command action.

Control Characters: Note that to enter control characters while in insert mode, prefix the the control character with "ctrl-v" and then type the control character (ex. Carriage control: ctrl-M, Form feed: ctrl-L, Backspace: ctrl-H, Delete: ctrl-P, ...). Each control character must first be preceeded by ctrl-v while in insert mode.

Delete/Restore Buffers: Each time you delete or yank a line, it is stored in a local buffer and can be recalled and pasted. See "vi line buffers" examples below.

Search Commands:

Keystrokes

Action

/search_string{CR}

Search for search_string

?search_string{CR}

Search backwards (up in file) for search_string

/\<search_string\>{CR}

Search for search_word
Ex: /\<s\>
Search for variable "s" but ignore declaration "string" or words containing "s". This will find "string s;", "s = fn(x);", "x = fn(s);", etc

n

Find next occurrence of search_word

N

Find previous occurrence of search_word

fx
nfx
;

Move cursor to first occurance of letter "x" after the cursor but in the same line
Move cursor to "n"th occurance of letter "x" in line
Go to next occurance in line

Fx
nFx
;

Move cursor backwards to next occurance of letter "x" in line
Move cursor backwards to "n"th occurance of letter "x" in line
Go to previous occurance in line

tx
ntx
;

Move cursor to one char before the next occurance of letter "x" in line
Move cursor to one char before the "n"th occurance of letter "x" in line.
Go to one char before the next occurance in line

Tx
nTx
;

Move cursor backwards to one char before the next occurance of letter "x"
Move cursor backwards to one char before the "n"th occurance of letter "x"
Go to one char before previous occurance in line

Where search strings can have the following patterns:

Pattern

Description

.

A period matches any single character

^

Finds the beginning of a line

^A

Finds the beginning of a line where the first character is the letter 'A'

$

Matches the end of a line

[abc]

Matches a string which contains any of the letters (a, b or c) between the brackets

\

Turn off the special meaning of a character. Example "\." does not match the period to any character but to the period character specifically

\d

Match any single digit (0 to 9)

*

A search expression followed by a '*' matches zero or more of the search expression. For example "A*" will match A, AA and AAA

+

Same as '*' above except that it matches one or more of the search expression.

?

Same as '*' and "+" except that it matches zero or one occurances

string1|string2

Match any either string 1 or string 2

a.b

Matches a string beginning with the letter 'a' followed by any character, again followed by the letter 'c'

^.$

Matches an entire line containing only a single character

a(b*|c*)d

Matches a string beginning with the letter 'a' followed by zero or more of the letter 'b', followed by zero or more of the letter 'c' and then followed by the letter 'd'

Linux.*Linux

Finds a line containing two instances of the string "Linux"

.* [a-z]+ .*

Finds a line containing a word comprised of all lower case letters with a single blank on either side of the word

Information Commands:

Keystrokes

Action

ctrl-g
or
:f

List file info: fine name, number of lines in file, position of cursor in file.

:set list
:set nolist

Show tabs and end of line markers
Turn of tab and eol markings

:args

Show command line arguments used

Terminate session:

  • Use command: ZZ
    Save changes to current file and quit.
  • Use command line: ":wq"
    Save (write) changes to current file and quit.
  • Use command line: ":w"
    Save (write) changes to current file without quitting.
  • Use command line: ":w!"
    Save (write) changes to current file overriding the file permissions if the user has the privileges to change the file permissions. For example this will save a file with read only privileges if the user is the owner or has the ability to modify the privileges to allow a write. This will not permanently modify the file privileges. Note that there is no space between the two characters. A space will infer that the output is streamed to a Unix command following the "!".
  • Use command line: ":w filename"
    Save (write) changes to a new file of name "filename" without quitting.
  • Use command line: ":q!"
    Ignore changes and quit. No changes from last write will be saved.
  • Use command line: ":qa"
    Quit all files opened.

New session:

  • Use command: ":e filename"
    Start new edit session on specified file name without closing current vi / vim editor process.

This all may looks very difficult but when you are familiar with these commands you can have expert level of working experience on Linux/Unix Environment

Average (1 Vote)


No comments yet. Be the first.