Registers


In most editors you get a single cut-n-paste buffer. When you use the cut or copy command, you lose whatever is in the buffer. As a result you end up zipping back and forth in a file, cutting from one place, and pasting in another. If you are lucky you can split the window and go back and forth between tiles, but it's a lot of manual labor and an exercise in hand-eye coordination as you seek, cursor, mark, cut, seek, cursor, paste your way to authoring nirvana.


It probably took a couple of minutes to get sick of that.

In VIM, they have a different answer. Sadly they have different terminology, too. Instead of editing buffers, we have "registers". Same concept, different term (the word "buffer" means something else in VIM). You can find a source in your code, yank/copy to many different registers, move to the destination site in your code, and then paste the various register contents. It's all one move.

The registers are (from the VIM documents, available via :help registers):

" (literally, the quote character)The unnamed or default register
a-z,A-Zthe lowercase letters and the uppercase letters
+The system default register (the normal cut/paste one)
*Select/drop registers
_The black hole -- essentially /dev/null, used to avoid wiping out register " (the unnamed register)


There are also a few other special-purpose registers which I leave for your exploration in the help system, such as the small delete register and the numbered ones. You don't really need to know these.

To tell the difference between the command y and register y, VIM expects you to prefix the register name with a double-quote character. Therefore, y is the yank command, and "y is the y register. If you type "y, VIM will wait for you to complete the standard pattern with perhaps the optional repeat count, a command, and a movement command (if required).

Examples of increasing power/complexity:
dddelete the current line into the default, unnamed register ("" or quote-quote)
"adddelete the current line into register a
"xy$Yank from the current character to the end of the line into register x
"byyYank the current line into register b
"c24ddLiterally Into register c, 24 times delete the current line. That's complex to read, maybe it's easier to just say delete the next 24 lines into the c register


I'm sure that "c24dd seems a little crazy, but think how you would do the same work if you were using notepad or the like. This is 6 keystrokes, and only one of the shifted, and you never had to leave the home row to grab a mouse.

It would be an extremely efficient way to cut 24 lines into a named register if you happened to know that you had 24 lines.

 If you didn't know that, the work of counting the lines would more than make up for the convenience.

That makes this a pretty academic example, and opens the door to visual marking of text for copy/cut, etc.

No comments:

Post a Comment