The VI Editor

“vi” is the acronym for “Visual Iinterface”. The origin of this name is due to the following fact: when the vi was created (beginning of the 80’s), was not common there are text editors as in the days of today. At that time, you typed in a text but I could not see him! That’s right! In 1992, it was created vim (I saw it Formed), a clone true to the vi, but with many other features, that have just been added. Some time later, I came spent to be called the `Vi IMproved’ (vi improved).

Vim is one of the editors of the texts most used in the Unix world. In some systems, there is a symbolic link (/bin/vi) pointing to the /usr/share / vim. In others, the /bin/vi that is the executable only that directly executes the vim. A lot of people think that uses vi, but in fact uses vim, and they have some differences. What you see below talks about the vim.

Vim is a text editor very powerful, it can: open multiple files at the same time, has autocorrect, auto-indentation, visual selection, macros, selection vertical text, use regular expressions, syntax colored,and much more. It is not unique to Unix, and can be be run on other platforms, such as Amiga, MacOS, Sun, Windows other.

There is also gvim, which is vim in graphical mode, with all the features of vim in full operation, which changes it is only the graphic mode.

Vim has several modes, that is, states in which he finds himself. They are: insert mode, command prompt, command line, visual, search and replacement. I will discuss the two main:

Mode and insert commands

To identify the mode (state) of vim, simply view the footer of the screen.

Now, let’s go to practice. To run vim, use:

$ vi => Open vim empty, without any file, and displays the screen presentation.
$ vi file => Open the file named “file”.
$ vi file + => Opens the file named “file”, with the cursor at the end of the same.
$ vi file, +10 => Opens the file named “file”, with the cursor in line 10.
$ vi file +/Copag => Opens the file named “file”, in the first occurrence of the word “Copag”.

To run vim, it starts directly in command mode. To prove, just look at the last line (footer) and not there will be nothing there. This means that you will not be able to write nothing, you can type the will, which will only hear beeps. For start typing, press “i” on your keyboard. Vim enters insert mode, which you prove (as spoken to previously) at the bottom of the screen, where is the following marking:

– – — INSERT —

Assume that you have already typed enough, and you want to save, by security. Press the ESC key to get back in command mode. And here are the commands to save/exit:

:w => Save the file that is being edited at the moment.
:q => Exit.
:wq => Save and exit.
😡 => Idem.
ZZ => Idem.
:w! => Saves forced.
:q! => Exit forced.
:wq! => Save and exit forced.

So, you edited a good amount of text and want to save:

:w

Now, go back to edit the text:

i

Remember that using the “i” for insertion, the same if you start inserting text before the cursor. See now other subcommands text insertion:

The => Inserts the text at the end of the line where the cursor is
=> Adds an empty line below the current line
=> Adds an empty line above the current line
Ctrl + h => Erases the last character to the left

Returning to the command mode:

Now see subcommands to move around the text:

Ctrl + f => Goes to the next screen.
Ctrl + b => Goes to the previous screen.
H => Moves the cursor to the first line of the screen.
M => Moves the cursor to the middle of the screen.
L => Moves the cursor to the last line of the screen.
h => Moves the cursor to the character to the left.
j => Move the cursor to the line below.
k => Moves the cursor to the line above.
l => Moves the cursor to the character to the right.
w => Move the cursor to the beginning of the next word (not ignoring the score).
W => Move the cursor to the beginning of the next word (ignoring the score).
b => Move the cursor to the beginning of the previous word (not ignoring the score).
B => Move the cursor to the beginning of the previous word (ignoring the score).
0 (zero) => Moves the cursor to the beginning of the current line.
^ => Move the cursor to the first character of a non-white line.
$ => Move the cursor to the end of the current line.
nG => Moves the cursor to the line number “n”

(substitute n with the number of the line)..
G => Moves the cursor to the last line of the file.

Copying and pasting text in vim (using the mouse)

Select the required text with the left mouse button. When you are pasting, to know that the text will be pasted from where is the cursor (which appears, at times flashing and sometimes not, when you’re typing). To paste, after you have selected the text, you can use one of these options:

1) by Pressing the right button of the mouse;
2) by Pressing the right button + left button together;
3) by Pressing the middle mouse button (3 button mouse);

Note: Remember that vim must be in insert mode.

Using the visual mode of vim

Enter visual mode: v
Now, use the scroll keys (arrow keys) on the keyboard, to select the text you want.
Press and paste, using the key “p” (paste).

Now see how to delete a particular text:

Using typically the keys, Backspace/Delete, or by entering mode visual (v) and pressing the Delete key.

You can remove up to the end of a word, using: dw
You can also remove up to the end of a sentence: d$

Undoing an action

Of course, you can undo an action that you consider wrong, or that wrong when typing the text. Isonly use: u
If you need to go back to the text on the screen, use the keys Ctrl + r.

Subcommands for text localization

/word => Search by word or character above or below the text.
?word =>: Moves to the previous occurrence of the word (for repeat your search use the “n”).
n => Repeat the last command using the / or ?.
N => Repeat the last command / or ? unlike (low to the top).
Ctrl+g => Shows the name of the file, the number of the current line and the number of rows.

More options for removing characters

x => Erases the character where the cursor is.
dd => Delete the entire line where the cursor was
D => Delete the line from the cursor position to the end.
J => Joins the current line to the next.
:5dd => Removeas next 7 rows from the position the current of the cursor (any number).

More to copy and paste

:y => Copy the line where the cursor is.
:5yy => Copy the next 5 rows from the position the current cursor.
:p => Paste what was copied in the line below the current cursor.

Options for replacement texts

rCARACTER => Replaces the character where the cursor is by the character specified in the CHARACTER.
RTEXTO => Replaces the text of current through the typed text (overlaps).
cw => Removes the current word for replacement.
cc => Removes the current line for replacement.
C => Replaces the remainder of the current line, waiting for the text logo after the command.
J => Joins the current line to the next.
:s/old/new => Replace the first occurrence of “old” the “new” on current line.
:% s/old/new => Replace in entire file (%) the first occurrence “old” by “new” in each row.
:% s/old/new/g => Replace in entire file (%), all (g) the occurrences of “old” by “new”.
:% s/old/new/gc => Same as above, but asking for confirmation for each substitution.
:% s/^String[0-9]//gc => regular Expressions work, as in the sed.
:% s/./\u&/gc => Converts to uppercase (\u) the first character (.) of each line.

Abbreviations

:ab => Shows all of the abbr.
:abc[lear] => Remove all.
:iab => Only for insert mode.
:iabc[lear] => Strip all insertion.
:cab => Only p/command mode ( : ).
:cabc[lear] => Strip all of the command modes.
:una vc => Strip ab to vc.

Note: Punctuation, spaces, or the ENTER, trigger the expansion of an abbreviation. However, Ctrl+] can also be used, to expand without adding characters.

Options for the SET command

:set
autowrite aw => Save every change.
backspace bs => Behavior of backspace (1 or 2).
errorbell eb => Buzzer error.
expandtab et => Exchange tab for spaces.
fileformat=dos ff => Converts the file to DOS.
hidden hid => Preserves the buffer.
hlsearch hls => Elumina the last search.
ignorecase ic => Case insensitive search.
incsearch is => Lights search while you type.
laststatus=2 => Shows status line.
lazyredraw lz => No redraws in macros.
lines=N => Amount of lines on the screen.
magic => Use magic in search patterns.
number nu => Show num of the line.
report=N => Show warning when N rows have changed (0=always).
showcmd => Shows the command is doing.
showmatch sm => Shows the marriage of {},[],().
smartcase scs => Defaults to “noic” when you have case.
textwidth=N => line Break in the text.
undolevels ul=N => Store the last N commands to undo (default=1000).
vb t_vb= => remove the “beep” error.

Now reversing uppercase/lowercase

5~ => Reverses the next 5 characters.
g~$ => Reverses all characters up to the end of the line.
select, u => Convert to lowercase.
select, U => Converts to uppercase.
selects, ~ => Reverses.

Note: Where is written “select”, it is to do using the visual mode (v).

Now here’s how to set the column break line (the problem I had when I started learning vim):

:set textwidth=N

If you are already in a file ready:

:set wm=5 => The number 5 here are the columns that will be “cut off”.
gqG => Until the end of the file.

Let’s now see what we can do by pressing the “Ctrl”key:

Of course, that is holding Ctrl + .

In COMMAND mode:

The => Increment a number (Add)
X => Decrements a number
S => ScrollLock
L => Redraws the screen
V => visual Mode (Visual Vertical)
G => the Status of the file
M => Beginning of the next line
And => the Line below without moving the cursor
Y => the above Line without moving cursor
N => Next line (Next)
P => the previous Line (Previous)
F => PageDown (Forward)
B => PageUp (Backyard)
U => PageUp, / 2 (Up)
D => Pgdn / 2 (Down)

Now, in INSERT mode:

The => Inserts the last text entered
I => TAB
S => ScrollLock
H => BackSpace
T => 2 tab’s at the beginning of the line (Two Tabs)
V => Aborts expansion of the next character
J => Enter – line break
M => Enter – line break
L => Redraws the screen
R => Insert content of register [a-z] (See below)
K => Inserts a digraph (See below)
N => the Search word in the current text (Next)
P => Search word in the text the current (Previous)
Y => Copy the character that is above (Yank)

See the special characters:

ga => Shows the code of the letter on the cursor.
:dig => Shows all dígrafos available (table).

Examples: To make a paragraph, use Ctrl+K, -, (“Ctrl”+”K”+” -“+””).
To make a½, use Ctrl+K,1,2 (“Ctrl”+”K”+”1″+”2”).

Working with files and multiple windows

You can open multiple files, for example:

$ vim file1 file2

And you can switch between the windows. See:

:wn => Writes the current and goes to the next.
:wN => Writes the current and goes to the previous.
:args => Shows all the current files.
:qa => Out of all the windows at once.
:all => Open all the files in individual windows.

Key windows key = Ctrl+W
j, down arrow => Move to window below.
k, up-arrow => Move to window above.
the => Only this window, closes all the other (Only).
+, – => Changes the window size.
= => Leaves all the windows with equal sizes.

The registers

“[a-z] => Use the register [a-z] for the next delete, copy or glue.
:reg => Shows the contents of all the registers.
:reg [- z] => Shows the contents of the registers [a-z].

Note: The [a-z] can be: 0-9a-z%#:.-=”

Tags:

m[a-z] => Brand [a-z] to the current cursor position.
`[a-z] => it Will up to the mark [a-z].
` => Goes to the previous position to the last jump (switch).
:marks => Shows the tags that are active.

Doing recording the sequence of commands

q[a-z] => Starts the recording of a sequence in register [a-z].
q[A-Z] => Starts the recording, by adding at the registrar’s [a-z].
q => Stops the recording.
@[a-z] => Executes the sequence from the registrar [a-z] (5 times? 5@a)

Tip: you Can put the @[a-z] within the recording itself q[a-z]! So it runs recursively. Very useful when there is a demand for standards in the recording. makes for all occurrences.

Mappings

:map :r!date => Mapping in command mode.
:imap :r!date => Mapping in insert mode.
:cmap r!date => Mapping in command line mode.
:vmap :r!date => Mapping in visual mode.

Examples:

“html: bold text in the excerpt selected
:vmap d pa # html: bold text in the excerpt selected

“turns on/off autoIndent
:map ,si :set ai!:echo “autoIndent=”&ai

“show white space at the end of the lines
:map – / *$^M

Through the mappings, it is possible to “shorten” command, or abbreviate them. Get to know the syntaxes:

Comment => Cyan
Constant => Purple
Identifier => Cyan
PreProc => dark Blue
Special => Red
Statement => Yellow
String => Purple
Type => Green

All => Black, brown background
Error => White, background red
Ignore => Black, black background! – hides

Using expandtab

But, what does that do? Turns all TABs into spaces. We can turn within its own vim, using the command:

:set expandtab

To disable:

:set noexpandtab

We can also place in the file ~/.vimrc the following line:

set expandtab

The file ~/.vimrc can be used for many settings, and this is one of them. There is also the file ~/.exrc, but not we will go into details.

We can include the output of a command in vim, using:

:r!command

For example:

:r!rpm -q kernel

Would include the following result, inside of your text (this is on my machine):

kernel-2.4.18-3

Miscellaneous tips vim:

:xit => Equal to :wq, but only writes if it has been changed something in the file.

:map N_ARQ ^R=expand(“%:t:r”)^M
Prints in the file the name of the edited file when N_ARQ is typed.

Now, see how to align the text:

:left
:right
:center

And to make a search for 2 words at the same time:
/palavra1\|palavra2

Vim is a text editor with hundreds of options, commands, strings… Then gives to cover everything here. By so, the best way to get to know him the background is using-o.

Now, here’s who helped with the creation of the tips, and where I took so much information:

Magazine of the Linux (Ed. 04 – I Saw, I came and I conquered – Aurélio);
Page Aurélio (www.verde666.org);
Tips-L (expandtab) (www.Dicas-L.unicamp.br);
Tips sent by his friend Uzziel ([email protected])
Various tutorials gathered from the internet.

I thank all those who contributed directly and/or indirectly with the tutorials.

The VI Editor 1