Showing posts with label Capital-G Command. Show all posts
Showing posts with label Capital-G Command. Show all posts

Tuesday, May 27, 2014

Jumping to the Top and Bottom of a Vim Document

Today I learned something new. I learned a new way to jump to the top or bottom of a Vim document.

I've long known how to jump to the top using the following ex command:

:0

That's a colon followed by a zero. The colon is there to prompt you for an ex command. The zero is the ex command itself. It is line zero, a pseudo line that is before all other lines. Since line zero is a pseudo line, the following command does exactly the same thing:

:1

So whether it's a colon followed by a zero or it's a colon followed by the number one, the result is the same: You are immediately jumped to line 1.

That's my old way of doing things. Today I learned a new way to jump to line 1:

gg

Yes, it's that simple. Two lower-case g's in a row (gg) take you to the very first line in the file. Here's how you go to the last line in the file:

G

So, in vim normal mode, a capital-G takes you to the last line in the file and a couple of lower-case g's typed one after the other takes you to the first line in the file. For years, out of force of habit, I used the following command to go to the bottom of a file:

:$

The dollar sign represents the last line in the file. For example, the following ex command tells you how many lines there are in the file by reporting the last line number to you:

:$=

Here's how I put it all together in my head now:

  1. Preceding my command with a colon makes it an ex command
  2. Immediately following the colon with a number addresses a line number in the file
  3. If it is a bare-naked number with nothing else following it, the number is used to jump you to that line number rather than address that line number
  4. Oddly enough, there is a pseudo line number, called line number 0
  5. For years I've typed :0 to go to line number 1. This is purely out of habit
  6. To jump to line number 1 in ex, I could have typed a colon followed by the number 1 instead. Whether I jump using colon followed by a zero or colon followed by the number 1, it is the same thing, for all intents and purposes. Both take me to line 1 immediately.
  7. Now I have a better way to do things. I now jump to the first line in the file with gg and jump to the last line in the file with G. Both these commands can be executed without ever leave vim's normal mode.

There's another way I can put this all together in my head. I now have 3 different ways to jump to line 12:

  1. :12
  2. 12G
  3. 12gg

In other words, the capital G defaults to the last line in the file and the lowercase gg defaults to the first line in the file. So, another way to look at it is in terms of defaults.

One of the reasons I started this blog is so that I would think about what I'm doing. I do things the same way over and over again out of force of habit.

It seems that everything remains the same until I decide to do something different. I aim to check in on myself a bit more often these days to make sure I know exactly what it is I'm up to.

Ed Abbott

Friday, October 7, 2011

The Vim Capital-G Command

Admittedly the title for this
article is kind of lame. Why call
it the Capital-G Command.
Surely it would make more sense
to name the command by what it
does rather than how you do it.

I've decided to call it the Vim
Capital-G Command
because this
is how I remember it. While it
makes more sense to call it the
Goto Line Number Command,
for some reason that's not what I
first chose as a search string
when I went looking for more
information on this command.

Here's the first article on the
Capital-G Command that I came
across:

The Vim
Goto Line Number Command


The article does a nice job of
explaining the command. I particularly
like the fact that the author points
out that a single Capital-G will instantly
take you to the last line of the file.

The reason I'm somewhat in love with
the Vim Capital-G Command is that I've
always used the ex editor command
for jumping to a certain line number.

For example, if I wanted to go to the
last line in a file, I would typically
type this:

:$

That's two keystrokes: colon followed by
a dollar sign. The colon accesses all the
legacy ex commands and then the dollar
sign signifies the last line in the file.

The reason I'm still using this legacy
ex command is because I've been
able to get away with it for so many
years.

It isn't until I started making heavy use
of highlighting commands (Capital-V, for
example) that using ex to go to a specific
line has become a liability.

There's no convenient way to highlight text
and then drop into an ex command
that I know of. Therefore, I really needed
to start using a pure vi command to
go to specific lines.

I've always been aware of the Capital-G
Command
. I just never use it. Old habits
die hard.

This becomes cumbersome when highlighting an
entire file for cross-application copy and
paste. For example, I'll copy and paste
text files from Vim to the OpenOffice.org
text editor for formatting and printing.

Copying the entire file from Vim is so much
easier when I use the following commands:

  1. Bring the file up in Vim
  2. Make sure the cursor is on the first
    line of the file (don't worry, it will be)
  3. Type Control-V to highlight the first line
  4. Type Capital-G to highlight all lines
    (in other words, go to the last line)
  5. Type double-quote, plus-sign, lowercase-y
    to push all highlighted text into the clip board
  6. Switch applications from Vim to the OpenOffice.org
    text editor
  7. Paste the text from the clip board by typing
    Control-V

I hate to admit it, but prior to getting a little
bit smarter about this, I'd been scrolling to the
bottom of the file rather than using the Capital-G
Command
.

The lesson? No matter how well the way you've
been doing things for years works, there is always
a better way.

Another lesson is it pays handsome dividends to update
your habits once in a while.

Sometimes what used to be a good habit can erode into
a bad habit over time.

Ed Abbott