android bsd linux unix & open-source world


Text Editors: vi commands and tips

WP Greet Box icon
Hello there! If you are new here, you might want to subscribe to the RSS feed or subscribe via E-mail for updates on this topic.

vi is a family of screen-oriented text editors which share certain characteristics, such as methods of invocation from the operating system command interpreter, and characteristic user interface features. The portable subset of the behavior of vi programs, and the ex editor language supported within these programs, is described by the Single Unix Specification and POSIX.

vi operates in either insert mode (where typed text becomes part of the document) or normal mode (where keystrokes are interpreted as commands). Typing "i" while in normal mode switches the editor to insert mode. Typing "i" again at this point places an "i" in the document. From insert mode, pressing the escape key switches the editor back to normal mode.

vi basic commands

:set ic
ignore case differences when searching.
:set ai
set automatic indent.
:set sm
show matching ( or { with ) or } in insert mode.
:set nu
show line numbers.

down-arrow up-arrow
move down/up 1 line.
right-arrow left-arrow
move right/left 1 character column.
0  $
go to 1st/last column of current line.
go down to 1st printable character of next line.
nw  nb
move right/left n words (1 word if n omitted).
go to line n (end of file if n omitted).
ctrl-f  ctrl-b
page forward/backward 1 screen.
ctrl-d  ctrl-u
page forward/backward half a screen.
[[  ]]
go to beginning of current/next c function.

search forwards for expression.
search backwards for expression.
n  n
repeat last / or ? command in same/reverse direction.

copy (yank) text up to target to buffer.
copy current line to buffer.

insert text before cursor.
open new line below cursor and insert text.
replace character under cursor with next typed.
replace text.

in insert mode, delete character before cursor.
x  x
delete character under/before cursor.
delete n characters under and to right of cursor.
delete n characters before cursor.
delete current line.
delete n lines.
delete from cursor to end of line.

p  p
put back yanked or deleted text below/above current line.
join current and next lines.
:m,n s/old/new/gc
global replace (g=every occurrence on line, c=prompt);

means from current position, n=$ means to eof.
undo last change.

quit, provided no changes were made.
quit without saving.
save (write) changes.
:m,n w file
save lines m through n (default=all) to file.
: x
save changes and quit.

Print This Post Print This Post
Comments (0) Trackbacks (0)

No comments yet.

Leave a comment


No trackbacks yet.