Quick start with the Vi Editor

The Vi editor, though a daunting thought at first and difficult to grasp for all you GUI fans and newbies can prove to be a powerful and time saving solution especially for developers. Once mastered it can become an indispensible tool. The best part about this wonderful editor is its speed. Here is my tribute to this wonderfull editor –

Opening a file
vi filename

Creating text

i Insert before current cursor position
I Insert at beginning of current line
a Insert (append) after current cursor position
A Append to end of line
r Replace 1 character
R Replace mode
Terminate insertion or overwrite mode

Deletion of text

x Delete single character
dd Delete current line and put in buffer
ndd Delete n lines (n is a number) and put them in buffer
J Attaches the next line to the end of the current line (deletes carriage return).

Oops

u Undo last command

Cut and paste

yy Yank current line into buffer
nyy Yank n lines into buffer
p Put the contents of the buffer after the current line
P Put the contents of the buffer before the current line

Cursor positioning
^d Page down
^u Page up
:n Position cursor at line n
:$ Position cursor at end of file
^g Display current line number
h,j,k,l Left,Down,Up, and Right respectivly. Your arrow keys should also work if
if your keyboard mappings are anywhere near sane.

String substitution

:n1,n2:s/string1/string2/[g] Substitute string2 for string1 on lines
n1 to n2. If g is included (global),
all instances of string1 on each line
are substituted. If g is not included,
only the first instance per line is
substituted.

^ matches start of line
. matches any single character
$ matches end of line

These and other “special characters” (like the forward slash) can be “escaped” with
i.e to match the string “/usr/STRIM100/SOFT” say “/usr/STRIM100/SOFT”

Examples:

:1,$:s/dog/cat/g Substitute ‘cat’ for ‘dog’, every instance
for the entire file – lines 1 to $ (end of file)

:23,25:/frog/bird/ Substitute ‘bird’ for ‘frog’ on lines
23 through 25. Only the first instance
on each line is substituted.

Saving and quitting and other ex commands

These commands are all prefixed by pressing colon (:) and then entered in the lower
left corner of the window. You cannot enter a ex command when you are in an edit mode.
Press to exit from an editing mode.

:w Write the current file.
:w new.file Write the file to the name ‘new.file’.
:w! existing.file Overwrite an existing file with the file currently being edited.
:wq Write the file and quit.
:q Quit.
:q! Quit with no changes.

:e filename Open the file ‘filename’ for editing.

:set number Turns on line numbering
:set nonumber Turns off line numbering

Advertisements

About synapse
Programming, motorcycles and photography. Want to do more, but only have time for so much!

10 Responses to Quick start with the Vi Editor

  1. Pingback: swiss air force watch

  2. Pingback: olympic symbol

  3. Pingback: santa cruz seascape resort

  4. Pingback: The vi editor. Quickstart cheatsheet. « заметки на полях

  5. Pingback: How to Use the vi Text Editor in Linux

  6. Paul says:

    sadly you have spam on this post – I use defensio with my chyrp blog and it works for me…. (free though I guess you’ll need a WP plugin, there must be a ton of them).

    I’m going to blog roll you in my geeks section on account of the usefulness of this alone 🙂

  7. Действительно интересно написано, но мне кажется, что все-таки автор что-то не договаривает 🙂

  8. Антон says:

    Большое спасибо, пост действительно толково написан и по делу, есть что почерпнуть.

  9. Гамлет says:

    На громких заголовках и шумихе можно делать и не такие успехи 🙂

  10. Steven says:

    Came looking for vi tips.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: