[Techtalk] VI(M) - Changing & Merging 2 lines?

Akkana Peck akkana at shallowsky.com
Mon Sep 1 13:33:27 EST 2003


Subba Rao writes:
> How can I define a keystroke that will,
>     	add a comma at the end of Line1
>     	perform a JOIN of Line1 and Line2
>     	move the cursor to the next record?

You want something like this (in .vimrc).  Note: I haven't tested
this, so tune it as necessary so it will work with your exact file
format.  I hope my explanation will give you enough that you can
do that.

map <leader>c Jxi,<ESC>

Here's the step by step explanation:

map: This tells it it's a key binding.

<leader> makes it so that you'll invoke it by typing \ (backslash)
first, and it won't conflict with existing predefined key bindings.
Omit the <leader> part if you want (but if it conflicts with something
already defined, sometimes the key binding might not work right
or might get overridden by another binding).

c just picks a key to put it on.  So you'll type \c any time you
want to join lines in this way.
You can pick any key, including function keys and other special
keys (with some limitations).  (That's true whether or not you use
<leader>, but I think there are more limitations if you don't use
<leader>.  The vim FAQ has several questions on that but the answers
aren't terribly clear.)

The rest is a sequence of visual-mode vi commands:

J: joins the following line to the current one.  It puts a space
between the two lines and leaves the caret on the space.

x: deletes the space since you don't actually want it.

i: enters insert mode.

,: inserts your comma.

<ESC>: leaves insert mode.

You might need to add an h or l before the x to move forward or backward
one space, or you might need to make similar minor tweaks if you want
the comma in a different place, a space after it, etc.  You get the idea.

I hope this helps get you started!
There's documentation on this stuff in the vim documentation at vim.org,
but it isn't all that well organized and there aren't many examples,
so set aside some time if you want to study it.  The Vim FAQ seems to
be the best place for info.  I haven't found any really good vim
tutorials which cover customization issues like key bindings or colors.

	...Akkana


More information about the Techtalk mailing list