noVIMber: This ain’t your momma’s undo

Like most modern applications, VIM has an undo/redo feature.  As you’re
typing along, you might realize that you’ve made a mistake and you want
to undo it.  This is easy.  In command mode, type:

u

and VIM will undo your latest changes.  If you decided that you really
did mean to make those changes, type:

<CTRL>+r

or

:redo

And the last change will be restored.

But VIM doesn’t just store a linear stack of changes.  Often, the
changes you make will cause branches in the document’s change history,
and VIM can help you navigate these changes.  If you type:

:undolist

You’ll see a not-very-user-friendly list of information in three
columns.  The first column is the change number, which is the branch’s
ID; the second is the number of changes that occurred in that branch,
and the third is the time the branch was created.

The branch IDs are sequential, so if you remember the order in which
changes were made, you can type:

:undo <ID>

to jump back to a specific branch.  But because the time stamps are
included for each branch, you can also use the commands

:earlier

and

:later

:earlier and :later take an argument of minutes, seconds, or hours.  So
if you made a change ten minutes ago and you want to revert to it, you
can type:

:earlier 10m

And from there,

:later 5m

You can also use the following commands to scroll backwards and forwards
through all change branches:

:g-
:g+

Happy VIMming!

- David Roth

noVIMber: Flexible and powerful VIM macros

This is one of the most flexible and powerful features of VIM: macros.

On day 8 I mentioned registers. To recap: VIM provides 26 registers
named a-z which can hold arbitrary text. This text can include VIM
commands. When you are in command mode, typing q followed by the name
of a register (a-z) will put you in to macro recording mode. Once this
starts, every keystroke you type will be recorded into the register that
you named.

When working with large blocks of structured text, this is insanely
productive. When you’re done typing the keystrokes that you need, hit
to go back in to command mode and hit q again. Now, all your
keystrokes are in the register and ready for playback.

Type @+ to play back the keystrokes. VIM will play back
exactly the commands you typed in while recording the macro, so if you
need the cursor to drop to the next line, or if you need to search
forward for some text, make sure that you end the macro with the
appropriate keystrokes.

Since the macro is recorded to a register, the same rules of register
use apply – if you want to append keystrokes to an existing macro that
was recorded in register a, you can add more commands by typing qA
(capital names append to registers).

Go crazy, and happy VIMming.

- David Roth

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

Personal Paparazzi: Managing Social Media work/life bleed-over

Techintwenty.com interviewed me on the topic of managing a work/life balance especially in context of using Social Media in private life and work life. Main takeaway from me: participating in Social Media is opt-in and by doing so we become our own personal paparazzi, exposing ourselves in our private lives.

A special thanks to @epodcaster and @LuisSandovalJr for having me on the program.

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

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