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

How to Open a New Buffer Without Saving the Old One

In Vim, there are many ways to accomplish the same thing. The other day I was trying to open a new buffer without having to save the old one.

I finally figured out how to do it:

:b! {new_file}

The The b! forces the new buffer without saving the old one. At the same time, the old buffer remains in the buffer list. If you save changes to the new buffer, you can go back to the old one by rewinding the buffer list like this:

:rew

The reason the file name is enclosed in curly braces is because the name inside the curly braces is an actual file name and not the name of a buffer. Without the curly braces, the file name becomes a buffer name.

Obviously, there are many other ways to accomplish the same thing. This is just one way to hide the current buffer from view, without modifying it, so that a new buffer can be viewed.

There are so many ways to do essentially the same thing in Vim that I get lost in it all. I'm always looking for a better way.

Ed Abbott

Friday, January 31, 2014

How Do You Remove Hard Returns Inside of Paragraphs?

When I write, I tend to place hard returns inside my paragraphs. I use Vim as my writing composition tool. Later, I need to remove these same hard returns when I transfer my writing to a word processor in order to format and print my document.

How do you remove hard returns inside a paragraph? How do you get rid of some newlines, but not all newlines? How do you leave your paragraph breaks unscathed in a simple text file, yet, at the same time, remove all newlines that are internal to that paragraph?

I remove newlines internal to a paragraph with a regular expression. I might use more than one regular expression to get the job done.

Here's a list of commands I might use and have used in the past:

qa
/.\{50\}\n.../
J
zz
q
@a
@@

Some notes about the above commands:

  1. The qa command starts to record a macro.
  2. The forward slash is a search. To be extra explicit, you can also place a trailing forward slash on the search string, but do not have to.
  3. The dot, or period, represents any character. In regular expressions, a dot is any character.
  4. The number 50 is a multiplier. It multiplies the previous character which is the dot.
  5. The entire expression, .\{50\} translates as any 50 characters.
  6. The entire expression /.\{50\}\n.../ translates to search for a line that is at least 50 characters long followed by a line that is at least 3 characters long
  7. The \n is a newline or hard return. In other words, the \n is the character we get rid of when we remove hard returns from our paragraphs
  8. The Capital-J is what we use to get rid of newlines. Capital-J is a command. It means join this line with the next line.
  9. The zz command completely redraws the screen so that the 2 lines that have been joined together are now in the center of the screen. I want to be able to see my work in order to check it. Each time I join 2 lines together, I want to check to see that this is truly what I intended.

There are many many variations on the above series of commands. The point? Find a regular expression that identifies hard returns in the middle of paragraphs and it becomes easier to remove them.

To make this task even easier, turn the whole hard return removal operation into a macro. Macros are one of my favorite vim features.

Ed Abbott

Wednesday, June 26, 2013

Line Deletetion With Confirmation

This morning, I was looking for a way to delete a line and confirm the deletion at the same time. I'm familiar with this command:

:g/pattern/d

In the above command, all lines containing the text pattern (regular expression) are deleted. However, that's not what I wanted. I wanted to be able to make changes similar to the search and substitution command which allows you to confirm substitutions like this:

:g/search pattern/subtitution pattern/gc

I then came across this web page:

Confirm Line Delete

Much better! From this web page I was able to figure out that the following command will confirm all deletions of lines containing the word banana:

:g/banana/s/^.*$\n//c

Here are the working parts of the above command:

  1. The colon gets you into ex commands, a set of commands that are line-oriented.
  2. The g prefix addresses each and ever line in your file. The g prefix is global, in other words.
  3. The first set of slashes that contain banana is the search pattern. This identifies lines that are to be addressed, which really means lines that are to be acted upon.
  4. The s command is the substitution command. Typically, the substitution command has s/// as its most basic syntax.
  5. The basic idea of the substitution command is that you clobber something with something else. You can literally clobber something with something else by typing s/something/something else/.
  6. In the example command above, ^.*$\n is being clobbered by absolutely nothing. In other words, ^.*$\n is being deleted in its entirety.
  7. The pattern ^.*$\n means the whole line plus the newline at the end of the line. To understand better, read about regular expressions. Regular expressions are a whole topic on to themselves.
  8. The final part of the command is the confirmation suffix which is represented by lowercase c. A simple y or n is used to confirm yes or no. No means the line in the file will not be deleted.

As is so often the case in vim, it is the combination of commands that lends vim its power.

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

Saturday, December 18, 2010

Inserting Unicode Characters
and UTF-8 Characters Into Vim

 
I was wondering how to insert a
unicode character into Vim the
other day. Here's what I found.

First off, you have to know how
to enter a regular ASCII character
into Vim using its number on the
ASCII table. Here's how you do
that:

  1. Enter insert mode
  2. Type ctrl-v
  3. Type the decimal equivalent
    for the ASCII character
  4. To actually see the ASCII
    character, type the
    ESC key

Say, for example, you wish to
enter a capital-A. Here's how
you would do it following the
above steps:

  1. Type i for insert
  2. Type ctrl-v to enter
    the ASCII code for a capital-A
  3. Type 65 to represent the
    letter A
  4. Hit the ESC key to end
    the insertion of text

Of course it is easier to type a
capital-A simply by hitting the
A-key while in insert mode. I'm
taking the long way around the barn
here in order to explain things.

What does this have to do with Unicode?
For Unicode characters, you take an extra
step. After typing ctrl-v, you follow
the ctrl-v with a Lowercase-u.

Here are the steps again, slightly adjusted
for Unicode:

  1. Enter insert mode
  2. Type ctrl-v u
  3. Type the hexidecimal equivalent
    for the Unicode character
  4. To see what effect the
    insertion of a Unicode character
    has had on your document, type
    the ESC key. Hitting
    ESC ends text insertion.

Here's how to enter a Capital-A
character in Unicode.

  1. Enter insert mode
  2. Type ctrl-v u
    to insert a Unicode character
  3. Type 0041 which
    is the 2-byte hexadecimal equivalent
    for a capital-A
  4. To actually see the Capital-A,
    type the ESC key. Hitting
    ESC ends text insertion.

What does this have to do with
UTF-8? So far, I've only mentioned
Unicode. UTF-8 is a specific
implemenation of Unicode. Wikipdedia
describes the relationship:

Unicode

Remember these distinctions when entering
ASCII versus entering Unicode in Vim:

  1. ASCII is one byte
  2. Unicode is 2 bytes
  3. ASCII is expressed in
    decimal notation
  4. Unicode is expressed in
    hexadecimal notation

If you are entering an ASCII
character in Vim, you will use
one byte of decimal notation.
If you are entering a Unicode
character in Vim, you will use
2 bytes of hexadecimal notation.
To the best of my knowledge, the
notation (decimal or hexadecimal
) is hard-wired. Please post if
I'm wrong and I'll correct myself.

One more thing I find it helpful
to know is what kind of encoding
Vim is using. Here's how I find
out:

:set encoding

This also seems to work:

:set enc

The answer that comes back in
my current session of Vim is:

encoding=utf-8

Seeing UTF-8 on my screen
makes me feel secure in the knowledge
that I'll be able to enter Unicode
into Vim.

There are many uses for a flexible
tool. That's the lesson I learn
over and over again when using Vim.

Ed Abbott

Friday, August 20, 2010

Conversion Between
DOS, Unix, and Mac Text Files

 
Vim is a real champ when it comes to
converting from one text file format
to another. Historically, the big
3 text ile formats have been:

  • DOS
  • Mac
  • Unix

Here's the corresponding commands
you can use to do conversions to
the above formats:

  • :set ff=dos
  • :set ff=mac
  • :set ff=unix

Alternatively, you can spell
everything out with the following
commands:

  • :set fileformat=dos
  • :set fileformat=mac
  • :set fileformat=unix

When Vim reads the file into
its buffer, it determines the
format. To query what Vim has
determined, use the following
command:

:set ff

Alternatively, you can spell your
query out with:

:set fileformat

That's all it takes to do a text
file conversion in Vim.

Well, not quite all. Of course
you have to save the file to make
the conversion complete. Here's
one way to save the file:

ZZ

Here's another way to save the file:

:wq

Ed Abbott

Thursday, March 4, 2010

Vim and Mac to Unix and CR to LF

 
How do you translate a CR
to a LF? How to your translate
a Classic Mac text file to Unix?

Here's a simple command that will
do this:

:%s/\r/\r/g

Here's where I learned how to do
this:

Change end-of-line format for dos-mac-unix

Ed Abbott

Insert an ASCII Character With Vim

 
Vim is a handy tool for inserting
an ASCII character into a file. Of
course, most characters are easily
entered by just simply typing them.

For example, Capital-A is
entered into a file by typing a
Capital-A while in Insert Mode.
It's easy to enter a number, a character
of the alphabet, or punctuation.

In life, it is often helpful to have
more than one way to get there. If
a landslide has blocked one road, you
can use another.

In Vim, the alternative way to insert
an ASCII character is to type CNTRL-V
while in insert mode. This will cause you
to be prompted for the ASCII code for
that character.

You terminate the prompt but either typing
something other than a number or by hitting
the ESC key. After you've terminated the
prompt, the character, whose ASCII code you
just typed, appears.

Ed Abbott

Tuesday, December 1, 2009

Splitting Vim Into Windows (Viewports)

OK. This is a new blog.

Here's a wonderful article
about splitting vim into
horizontal and vertical windows:

Split Vim Viewport

Here's the command I use to
split vim into two equal horizontal
windows:

:sp

Here's the command I use to
go up a window, moving from
the bottom window to the top
window:

cntrl-w k

Here's the command I use to
go down a window, moving from
the top window to the bottom
window:

cntrl-w j

Here's the command I use to get
rid of the extra window:

:q

Enjoy!


Ed Abbott