noVIMber: Adding a little color to your day with VIM

VIM supports syntax highlighting of a lot of different types of files,
from C code to Apache configs. To make sure you’re using the syntax
highlighting, execute the following command:

:syntax on

If the highlighting doesn’t appear, you may need to tell VIM what kind
of file it is you’re editing:

:se filetype=apache

Typing “:filetype” and hitting <ENTER> will tell you what type VIM
thinks the file is currently.

All this is done automatically on most installations of VIM. So what if
you log in to a server and the syntax highlighting is working, but you
can’t read it because of the colors?

Change the colorscheme. Type “:colorscheme ” (note the space) and hit
tab. VIM will cycle through the names of all the colorschemes currently
installed. I kind of like ‘desert’ on most systems. Experiment and find
one that works for you.

Happy VIMing!

- David Roth

noVIMber: Using markers in VIM

“m” marks the spot.

If you find your self jumping back and forth in different locations in a
file, you might find using marks to be a performance booster.
When you have the cursor in a location that you plan to return to, drop
a mark by typing:

ma

That creates a mark named ‘a’ under the cursor.  Just like with
registers, there are 26 named marks available (a-z).  There are also 26
marks (A-Z) which work across files.  To go to a mark once you’ve placed
it, type:

'a

to jump to the line where the mark was placed, or:

`a

to jump to the line and column where the mark was placed.  If you’ve
placed a mark with an uppercase name, then VIM will jump you to the mark
even if it’s in a file that is not currently open.

There is also a special default mark named “.”.  It holds the location
of the last edit made in the current file.  You can use “‘.” or “`.” to
jump to the last line edited or the last specific location edited.

Happy VIMming.

-David Roth

noVIMber: Doing more with less in VIM

One of the central philosophies of VIM is to get more done with fewer
keystrokes.  There are features built into VIM such as templates,
macros, and completion that are designed to minimize the typing you have
to do when working in structured files.  Because when you think about
it, there are only so many keystrokes you are going to be able to type
in your lifespan.  By saving these precious keystrokes, VIM can
literally prolong your life.

Or something.

Basic completion in VIM works by searching through the file that you
currently have open and matching words.  Let’s say you’re editing an
Apache httpd.conf and you’re setting up a new virtual host.

<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/html
</VirtualHost>

In a default httpd.conf, the word “VirtualHost” already appears, so you
can save yourself some keystrokes by using <CTRL>+p to autocomplete the
word VirtualHost when you start typing it:

<Virt<CTRL>p

So you’re typing the string “<Virt” and then, while still in insert
mode, the CTRL key followed by p.  <CTRL>+p searches backwards in the
file for the Previous match to what you’ve started typing.  <CTRL>+n
searches for the Next match to what you’ve started typing.  Unless
you’re working on a really huge file that doesn’t contain a lot of
instances of the word you’re searching for, these two commands are
analagous because the searches will wrap.

But <CTRL>+p and <CTRL>+n only match a single word – that is, a string
of characters bounded by whitespace or other non-alphanumeric
characters.  Let’s get turbo-lazy and complete entire lines instead.

<V<CTRL>x<CTRL>l

<CTRL>+x puts you in a special autocomplete mode.  The next set of
keystrokes tells VIM just how you want to autocomplete what you’ve
started.  <CTRL>+l completes whole lines.  And in an httpd.conf file
using name-based virtual hosting, there’s a really good chance that
there’s already a VirtualHost definition that you can use.

Another handy autocomplete sequence is <CTRL>+x <CTRL>+f, which
autocompletes file names in your current directory.  If you have a
dictionary file, you can tell VIM about it with:

:se dictionary=/path/to/dictionary

and then use <CTRL>+x <CTRL>+k to autocomplete based on words in that file.

Type less and happy VIMming!

-David

noVIMber: Finding the cursor in VIM

VIM’s syntax highlighting can be very cool, but sometimes all the colors
on the screen can make it difficult to track where your cursor actually
is. Usually this isn’t a big deal – just wiggle the cursor around with
a bunch of jkjkjk keystrokes and you can find it – but VIM’s got a
couple of settings that make it easy to home in on exactly where your
cursor is.

The first is cursorline. With cursorline turned on, VIM will
highlight the row of the file where your cursor is.

:se cursorline

To turn it off, type:

:se nocursorline

If that’s not precise enough, you can get a bull’s-eye view of the
cursor by also enabling cursorcolumn:

:se cursorcolumn

(To turn it off, use :se nocursorcolumn)

With both cursorline and cursorcolumn turned on, you have a cross-hair
view of the cursor. How cool is that? This may not be the most
practical feature in the world, but it looks kind of groovy.

Happy VIMming!

-David Roth

noVIMber: Fun with registers in VIM

VIM provides several registers that can make editing a lot easier.  A
register is analagous to a clipboard, except that instead of just having
one, you’ve got several.  There are twenty-six “normal” registers, each
named after a letter of the alphabet, and a few “special” registers
which might get dealt with in a later tip of the day.

The registers can hold an arbitrary amount of text for as long as you
need.  To put text into a register, you use the standard “y” (yank), “x”
(cut), or “d” (delete) commands, except that you name the register
you’re going to use before executing the command.

For example, to yank the current line into register a, type:

"a yy

The “a keystrokes let VIM know that you’re referring to a specific
register.  The yy keystrokes yank the current line into that register.

After executing that command, type:

:reg

to view the contents of the current registers.  You’ll probably see a
lot of stuff in several registers, but the one you’re concerned with is
“a.  You should see the text of the line you yanked there.

After getting text into a register, you can paste it back in the same
manner, using the name of the register followed by the “p” (paste after)
or “P” (paste before) commands:

"a p

So VIM basically gives you twenty-six clipboards to use for shuffling
your text around.  This can be very useful for editing complex documents
quickly.

When you use the yank, cut, or delete commands with a register, VIM will
replace the contents of that register with the text effected by the
command unless you use the upper-case name of the register.  If you say:

"A yy

then VIM will append the yanked text to the existing register rather
than replace it.  Keep this in mind – “a and “A are NOT different
registers; they’re the same, it’s just that the way you refer to the
register changes they way your editing commands behave.

Happy VIMming!

noVIMber: Navigating code – including HTML/XML

You can match enclosures like (,),{,},[ and ] in VIM by positioning the
cursor on the enclosure and typing %.  It’s very handy for navigating
around code.  But by default, VIM doesn’t know how to match more complex
enclosures like HTML tags.

Luckily, there’s a script called matchit.vim that takes care of that.
Go to http://www.vim.org/scripts/script.php?script_id=39 for the
download link and installation instructions.  Now when you’re editing
HTML or XML files, you can jump to the matching tag with %.

Note that for this to work, VIM must already know what the file type is.
If the file doesn’t have an .xml or .html extension, type:

:se filetype=html

or

:se filetype=xml

to let VIM know that you’re working in an HTML/XML file.

Happy VIMming!

-David

noVIMber: Syntax highlighting with VIM revisited

Because of the syntax files mentioned in yesterday’s tip, VIM can not
only apply syntax highlighting but it can also autoformat your code.

Consider the following JavaScript snippet, yanked from flickr.com:

fixMaxWidth = function(el) {
try {
el.runtimeStyle.behavior = 'none';
var mw = fixMaxWidth_getWidth(el);
var nmw = parseInt(mw,10) || 10000;
var cW = parseInt(el.offsetWidth);
var cH = parseInt(el.offsetHeight);
var ratio = (cH/cW);
if (el.offsetWidth>nmw) {
el.style.width = (nmw+'px');
if (!isNaN(cH) && cH) {
el.style.height = (Math.ceil(nmw*ratio)+'px');
}
}
} catch(e) {
// oh well
}
}

It’s not especially complicated, but it can be made more readable with a
couple of keystrokes.

Open up an empty file named test.js in VIM and paste in that code. Then
type the following:

gg
VG

That will go to the first line (gg), turn on block highlighting (V), and
go to the last line (G). The entire file is now highlighted.

Now just type:

=

That’s all. ‘=’ applies formatting to the selected block of text based
on the current file’s type. Unfortunately, VIM can’t do this trick with
HTML files out of the box. More on that later. Here’s the result:

VIM's auto-formatting result

VIM's auto-formatting result

Happy VIMming!

-David

noVIMber: Syntax highlighting with VIM

VIM can recognize files by their type and apply syntax highlighting. On
most RedHat systems, if you type:

vim /etc/httpd/conf/httpd.conf

it will open up the httpd.conf file with syntax highlighting
automatically. Comments will be light blue, key words will be kind of
teal, plain text will be black, quote-enclosed strings will be red,
etc., etc. Syntax highlighting can make syntax errors pop out
immediately and it also makes code a whole lot more readable.

If syntax highlighting isn’t working, two things may be wrong. First,
syntax highlighting may just be turned off. You can control it with:

:syntax on
:syntax off

If syntax highlighting is turned on but isn’t appearing correctly, VIM
may not know what the type of the current file is. You can tell it the
file type explicitly with:

:se filetype=<type>

Common types are apache, php, mysql, python, perl, c, c++, etc., etc.

You can see what types are available on your local system by looking in
the syntax directory under the VIM installation directory. On my Ubuntu
system the full path is /usr/share/vim/vim72/syntax. Each file in this
directory defines the syntax of a specific type of file.

If you start a new file without an extension, you’ll have to manually
set the file type before VIM can apply syntax highlighting.

Happy VIMing!

- David Roth

noVIMber: Opening URLs with VIM

VIM knows how to open files by URL, not just on the local system:

vim http://rackspace.com
vim ftp://ftp.microsoft.com/Softlib/README.TXT
vim scp://example.com/.plan

Opening a file over HTTP will download the web page and save a local
copy in your temp directory as a read-only copy. Opening it via FTP
will prompt you for a username and password – in the example above, use
‘ftp’ or ‘anonymous’ and your e-mail address as the password. Again,
VIM will download the file to your temp directory, but with an FTP
connection, if you edit the file and try to write it, VIM will attempt
to write the file back to the remote server. If you’ve got write access
to the remote file this can be very useful. SCP works the same way as
FTP and can be handy if you’ve got key-based authentication set up with
a remote server.

If you’ve already got a VIM session running, you can open a remote file
by typing:

:e http://rackspace.com

Happy VIMing!

- David Roth

noVIMber: VIM by any other name

VIM goes by many names.

You can invoke VIM on most systems with either “vi” or “vim”.  VIM’s
name means “VI iMproved”, and it is a clone of the original VI editor
created by Bill Joy.  On most machines, “vi” is an alias for VIM and if
you invoke vi, it will start VIM in vi compatibility mode.  This isn’t
true on Rackspace’s default RHEL 5 kicks – both VI and VIM are installed
side-by-side.

vi compatibility mode disables a lot of VIM’s features, most notably
syntax highlighting.  This is why I always type “vim” when starting the
editor.

There is another alias for VIM that is extremely useful: vimdiff.  If
you need to compare to two (or more) versions of a file, type:

vimdiff <file1> <file2> <...>

It will open up a special instance of VIM with a vertical split for each
file.  A special syntax highlighting mode will be invoked to display
differences between the files, and most of the matching content of each
file will be suppressed.  It’s a much more readable version of viewing
diffs than, say, “diff -u”.

As with tabbed viewing, you can quit completely out of a vimdiff session
with:

:qa

vimdiff is not read-only, so you can edit the multiple splits if you
like and then save them with:

:w

Happy VIMming!

-David Roth

Bad Behavior has blocked 206 access attempts in the last 7 days.