
Advanced tricks with vim registers - nyellin
http://stackoverflow.com/questions/1497958/how-to-use-vim-registers/1498026#1498026
======
mcantor
\- _How do delete without clobbering the "clipboard" register:_

Delete to the "black hole register", "_". For example:

    
    
      "_dd
    

\- _How to enter the solution to a quick math equation while in insert mode:_

Use the "expression register", "=". You can even use variables. For example:

    
    
      ^r=5325*30<ENTER>
    

\- _How to edit a macro once you've recorded it:_

Paste, edit, and re-yank its register. For example, hitting "qq" will record
to register "q". If you want to edit it, you can do _"qp_ , make your changes,
then visually select it and do _"qy_.

\- _How to access prior contents in the clipboard:_

Use registers 0 - 9. For example, _"0p_ will paste whatever was in the
clipboard register _before_ you yanked whatever's in there now.

BONUS ROUND:

\- _In command mode, how to insert the word currently under the cursor:_

    
    
      ^r^w
    

(That's Ctrl-r, Ctrl-w)

\- _Insert a register's_ literal _contents:_ For example, if you have a _^m_
(newline) character in the _a_ register, but you want to input the _^m_
character instead of an actual newline while in insert mode:

    
    
      ^r^ra
    

\- _Insert the full path of the filename currently under the cursor:_

    
    
      ^r^f
    

(This only works if it's in your 'path' variable, like with _gf_.)

------
Pewpewarrows
The two "advanced" topics that I've never bothered to learn, despite my years
of using vim, are registers and buffers. Thanks to the answers on SO and the
comments here, I think I have a pretty good grasp of registers (the being able
to paste yanked text after a delete would have saved me so much god-damn
time).

So all that leaves is buffers. I've traditionally been using tabs and splits
for all my editing multitasking, with NERDTree and Command-T to open new files
quickly. Is there anything to be gained from me learning/using buffers at this
point? I don't know what benefits they offer, and I've never had any problems
with my current tab & split setup.

Any buffer evangelists out there that care to chime in?

~~~
mcantor
Surprise! You have been using buffers all this time. A "buffer" is just "a
file in memory"; if you have 1 split on your screen, with a different file in
each split, then you are viewing 2 buffers.

The two things which will probably give you the biggest bang for your buck
are:

    
    
      :ls
    

Which lists all buffers currently in memory, and:

    
    
      :b
    

Which supports tab completion, allowing you to instantly jump to any file with
just a few keystrokes. You'll also probably enjoy Ctrl + 6 / Ctrl + ^, which
swaps between two buffers.

You'll need to do

    
    
      :set hidden
    

before switching between buffers in a single window. Don't worry, though; if
you try to quit without saving a modified buffer, or use _:bd_ to delete a
buffer without saving it, vim will yell at you.

~~~
yuvipanda
<leader>b is a good replacement for both :ls and :b if you're using Command-T
(like grandparent is). I was surprised when I discovered that feature, since
it isn't quite as advertised/popular as the file opening capabilities of
Command-T

~~~
Pewpewarrows
Oh wow, that's an excellent find. That immediately made playing around with
buffers much easier.

~~~
yuvipanda
Indeed! I was using BufferExplorer and accidentally 'discovered' <leader>b. 30
minutes later, BufferExplorer was dead :)

------
Symmetry
A great trick I didn't know about was the 0 register. I've lost tracked of the
number of times I've copied some text, and then deleted something by mistake
not realizing I'd overwritten the default register. I could still have pasted
what I wanted with "0p, though.

~~~
nyellin
You can also use "2p to paste the second to last deletion.

------
nyellin
My favorite trick is using capital-letters to append to registers:

"kyy - copy a line to register k

"Kyy - append a line to register k

------
herTTTz
I use YankRing (<http://www.vim.org/scripts/script.php?script_id=1234>) which
takes care of the problem of remembering yank history. Just map :YRShow to a
key and you are good to go.

~~~
guns
Give regbuf a try too: simpler implementation and features a buffer for
previewing and editing registers.

[1]: <https://github.com/tyru/regbuf.vim>

~~~
kjell
To me it wasn't obvious, but with yankring after you hit p you can paste from
older registers with ^p. Yankring then replaces "0 with "1. Another ^p
replaces "1 with "2.

So by repeatedly hitting ^p after a paste you can cycle back quite a ways. Way
easier than ":reg, now where did that go?"

------
sixtofour
Vim is the tech equivalent of "line movies." A line movie is one where guys
(it's usually guys) throw lines back and forth at each other, usually in the
current context. Princes Bride and Real Men are line movies. I love line
movies.

Vim lets geeks throw "vim tricks" back and forth at each other. I love Vim,
and I love vim tricks.

Good SO thread, and good HN thread. I never tire of Vim threads.

------
jjcm
If you guys are interesting in learning vim more, checkout vimcasts.org. Drew
Neil does some really nice walkthroughs that are extremely helpful for
beginner and intermediate vim users.

