Vim is a text editor for Unix/Linux, based up the vi
editor which is standard with most Unix
and Linux installations.
I found Vim easier to learn than Emacs, and to
have every feature or customization I needed to
edit perl and HTML code. It can be run
a terminal window, but it also has it's own graphical
interface (called Gvim, in the screenshot below and to the right).
Vim allows for macros, regular
expression search and replace,
and has it's own command language.
This page has tips and techniques
I've found useful and which
might benefit other beginning to intermediate
users. The Vim homepage (see link below) also
has a section for user tips.
Vim modes
When beginning with vi or Vim, it's first necessary to
learn Vim's modes: text insertion mode, "normal" mode,
and command-line (ex) mode.
normal mode
Note in the graphic at right that
the cursor is a solid block. Any keys pressed
while in normal mode will be interpreted as
commands, rather than being inserted into the document. |
 |
insert mode
From normal mode, the key 'i' (for insert) will switch you
to insert mode. Note in the graphic at the right that
the cursor is now a vertical bar. Any keys pressed will be inserted into
the document. Pressing the Escape key will take you out of insert mode.
|
 |
ex mode
From normal mode, pressing the colon ':' will move you into
the command-line or "ex" mode. Notice in the graphic on the right
that the cursor is now at the bottom of the Vim window, and
any keys pressed are entered on this command line. |
 |
Some Vim Commands
| gf |
Jump to filename under the cursor |
| <Control-g> |
display current file name in command window |
| :g /^$ d |
delete all blank lines from a file |
| :%s/foo/bar/g |
global substitution of 'bar' for 'foo' |
| :let @/=@* |
set search string(register @/) to last highlighted
text(register @*). |
There are some examples of Vim regular expressions
on my regexp page.
Macros
Repeating commands "manually" is never necessary in Vim.
Even complex combinations of commands can be done for you
by Vim automatically. This can be done by using Vim's macro support.
Pressing 'q' followed by any alphanumeric key (while in normal mode)
will begin recording all commands your type. They are saved
in the register matching the alphanumeric key you used.
Pressing 'q' a second time stops recording.
The recorded commands can be executed en mass by
and pressing '@' followed by the register letter/number
you used.
For example, the names below can be moved from the
end of the list to the end of the lines above them by
using a macro.
First surname:
Second surname:
Third surname:
Samuelson
Thoennes
Ruona
The macro would need to find the next name in the file,
delete it, move to the next available line above,
and paste the name. The Vim keystrokes (commands)
to accomplish this, saving them as a macro, would be:
qa/^\w\+$<Return>v$jdgg/:$<Return>pq
An explanation of the above commands follows:
| qa | begin recording a macro, saving it to register 'a' |
| /^\w\+$(Return) | search for a line with just one word on it |
| v | enter visual selection mode |
| $ | select to the end of the line |
| j | move one space to the left |
| d | delete the selected text |
| gg | jump to the top of the document |
| /$(Return) | find a colon at the end of a line |
| p | paste the last deleted text |
| q | stop recording the macro |
Preceding any command (in normal mode) with a number
causes the command to be repeated that many times.
So once the macro above was created, it can be used on
a file a hundred times or more with "100@a".
Editing database tables with Vim
Vim can be used for editing text that's stored in a database
using any of the three following techniques,
as well as others.
Editing an entire database table
If you want to edit an entire database table,
you can dump it to a textfile using mysqldump:
mysqldump database_name table_name > table.sql
The resulting textfile will contain all the proper SQL statements
for the creation of the table, and the insertion of all it's data.
You can edit this textfile with Vim, changing just those
fields you wish to change. You can run ispell on it to
spellcheck it, for example.
To return the revised table to the database, the original
table must be dropped (deleted. Make sure you have the database
backed up before dropping a significant amount of data,
such as an entire table). You can then pipe the revised
table back into the database from the command line:
mysql database_name < table.sql
Editing selected fields from a database table
You can revise only selected fields from a database table
by using a "select" statement to get the fields you want,
and piping the result into a textfile. This can be done
using the '-e' switch with mysql, followed by your select statement in quotes:
mysql database_name -e "select id,title from photos
where id between 1 and 20 order by id;" > textfile.sql
textfile.sql can then edited in Vim.
It will look something like the below, with the field names on
the top line followd by field values seperated by tabs.
title id
Shops on 14th Avenue 1
14th Avenue Southeast and 4th Street 2
14th Avenue Southeast 3
236 Bedford Street Southeast 4
43rd and Upton 5
You can run ispell to spellcheck this text, or
otherwise revise it in Vim. Your modified text
cannot be piped back into MySQL
until it has been formated into proper SQL statements.
Since we're modified fields that already exist,
we need "update" statements. This is why it's necessary
to dump an id number or other key; for use with
the update statement.
Vim's global search and replace can handle formatting
the above text into proper update statements.
First get rid of the field names at the top of the
file(which read "title id").
The following Vim substitutions should make
the necessary changes.
| :% s/"/\\"/g |
escape any quotes contained within your text,
so " becomes \" |
| :% s/^/update table set field="/ |
puts the beginning of the update statement in front of each line |
| :% s//" where id=/ |
puts the where clause between the first fields and second,
which is the id number |
| :% s/$/;/ |
puts the ';' at the end of each line |
The above text will look like the below after
these substitutions have been applied to it:
update table set field="Shops on 14th Avenue" where id=1;
update table set field="14th Avenue Southeast and 4th Street" where id=2;
update table set field="14th Avenue Southeast" where id=3;
update table set field="236 Bedford Street Southeast" where id=4;
update table set field="43rd and Upton" where id=5;
This file can then be piped back into MySQL,
with the following command, and the revised text will
replace the original text:
mysql database_name < textfile.sql
Editing webpages 'live' from within a database
I've created a vim script that will fetch a webpage
(or other text) from a database for editing with a single Vim
command. It can be re-saved with a second command.
The script is customized to my own database fields,
but could be modified to work with your own table.
It requires Vim with perl support, perl, and the perl DBI module
to be installed. The link below will display the script as a
textfile. It can be executed from within vim with
":source /path/to/pages.vim.txt".
pages.vim.txt
Vim credits
Vim was created by Bram Moolenaar and others. It is open source.
If you find it useful, you are encouraged to make a donation to the
International Child
Care Fund(ICCF) for orphans in Uganda.
|