Vim improved howto

Saturday, June 13th, 2009

VIM is the most useful standard text editor on your system. (vi is short for visual editor
and is pronounced “vee-eye.”
Unlike Emacs, it is available in nearly identical form on every modern Unix system, thus providing
a kind of text-editing

With a screen editor, you can scroll the page, move
the cursor, delete lines, insert characters, and more, while seeing the results of your
edits as you make them. Screen editors are very popular, since they allow you to make
changes as you read through a file, like you would edit a printed copy, only faster.

Intro

Quitting Without Saving Edits

When you are first learning vi, especially if you are an intrepid experimenter, there are
two other ex commands that are handy for getting out of any mess that you might create.
What if you want to wipe out all of the edits you have made in a session and then return
to the original file? The command:

:e!

To quit :

:q!

”The exclamation point added to the :e or :q command causes vi to override this prohibition, performing the operation
even though the buffer has been modified.”

Saving file

You can save in a new file

:w newfile or :w pathname/file

Delete a file :

:!rm file

Free space:

:!df

Shell commands :

:!ls /tmp
:!sh :!shell (For shell) – :exit to exit or CTRL+D CTRL+Z for background (fg + bg)

You try to write your file, but you get a message telling you that your disk quota has
been reached.
Try to force the system to save your buffer with the ex command :pre (short
for :preserve). If that doesn’t work, look for some files to remove. Use :sh (or
CTRL-Z if you are using a job-control system) to move out of vi and remove files.
Use CTRL-D (or fg) to return to vi when you’re done. Then write your file
with :w!.

vi Commands

vi has two modes: command mode and insert mode. As soon as you enter a file, you
are in command mode, and the editor is waiting for you to enter a command. Commands
enable you to move anywhere in the file, to perform edits, or to enter insert
mode to add new text. Commands can also be given to exit the file (saving or ignoring
your edits) in order to return to the Unix prompt.

Moving the Cursor

In command mode you can position the cursor anywhere in the file. Since you begin
all basic edits (changing, deleting, and copying text) by placing the cursor at the text
that you want to change, you want to be able to move the cursor to that place as quickly
as possible.

Single Movements

The keys h, j, k, and l, right under your fingertips, will move the cursor:

h
Left, one space
j
Down, one line
k
Up, one line
l
Right, one space

You can also use the cursor arrow keys ( ? , ? , ? , ? ), + and – to go up and down, or
the ENTER and BACKSPACE keys, but they are out of the way. At first, it may seem
awkward to use letter keys instead of arrows for cursor movement. After a short while,
though, you’ll find it is one of the things you’ll like best about vi—you can move around
without ever taking your fingers off the center of the keyboard.

Numeric Arguments

You can precede movement commands with numbers. For Ex command 4l moves the cursor four spaces to the right, just as if you had typed l four times
(llll).

e.g 4l – 4 spaces to the right
4h – 4 spaces to the left
4k – 4 lines up
4j – 4 lines down

vi has an option that allows you to set a distance from
the right margin at which vi will automatically insert a newline character. This option
is wrapmargin (its abbreviation is wm). You can set a wrapmargin at 10 characters:

:set wm=10

Movement Within a Line

Two useful commands that involve movement within a line are:

0 (digit zero)
Move to beginning of line.
$
Move to end of line.

Line numbers can be displayed
in vi by using the number option, which is enabled by typing

:set nu in command mode

Tricks:
1). edit $HOME/.vimrc and add : set ts=4 (tab space, default is 8 )