Advanced tricks with vim registers
stackoverflow.com
stackoverflow.com
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.)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?
If hidden buffers are enabled, you can open document A, make some changes, then open document B in the same window and you don't have to save the other document first. When you want to go back to it, you just use :b 1
If you use screen, you can think of buffers like windows. You can have multiple windows open but one is active at any moment, or you can split your screen into multiple regions and have multiple windows visible at the same time. In Vim, you can have multiple buffers open. You can interact with them one at a time through a single window or you can split the window and see multiple buffers at the same time.
If you get used to using them like that, then things like the :bufdo command becomes handy for making changes to a bunch of docs at the same time and you can quickly bounce around documents without having to browse to them each time.
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.If I may ask a newbie question, why do you use both tabs and split buffers? It seems like a little over kill, but perhaps it's because it allows you to easily switch between buffers w/o having to :b $file all the time? I haven't gone higher than 4 splits as the window sizes start getting too small.
"kyy - copy a line to register k
"Kyy - append a line to register k
So by repeatedly hitting ^p after a paste you can cycle back quite a ways. Way easier than ":reg, now where did that go?"
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.