News for hackers. Stuff that matters.
Re:Advanced tricks with vim registers by mcantor on Wednesday August 10, 2011 @05:13PM (#2870225)
- 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.)
Re:Advanced tricks with vim registers by Pewpewarrows on Wednesday August 10, 2011 @06:43PM (#2870531)
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?

Re:Advanced tricks with vim registers by mcantor on Wednesday August 10, 2011 @07:26PM (#2870670)
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.
Re:Advanced tricks with vim registers by yuvipanda on Wednesday August 10, 2011 @09:50PM (#2871000)
<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
Re:Advanced tricks with vim registers by Pewpewarrows on Wednesday August 10, 2011 @10:08PM (#2871035)
Oh wow, that's an excellent find. That immediately made playing around with buffers much easier.
Re:Advanced tricks with vim registers by yuvipanda on Wednesday August 10, 2011 @11:22PM (#2871208)
Indeed! I was using BufferExplorer and accidentally 'discovered' <leader>b. 30 minutes later, BufferExplorer was dead :)
Re:Advanced tricks with vim registers by pyre on Wednesday August 10, 2011 @10:26PM (#2871076)
BufferExplorer is a good plugin to have around. <leader>be will give you a listing of all of your buffers and allow you to do things like hit 'd' to delete the buffer under the cursor.
Re:Advanced tricks with vim registers by DEinspanjer on Wednesday August 10, 2011 @06:56PM (#2870570)
Hidden buffers are the only important thing to me. I don't know if you might already have that turned on or not.

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.

Re:Advanced tricks with vim registers by ajtaylor on Thursday August 11, 2011 @02:55AM (#2871785)
I've only recently (last week!) started using split buffers with vi, and it is a major game changer for me! I've read about NERDTree before, so I'm moving it up on my TODO list.

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.

Re:Advanced tricks with vim registers by justanotheratom on Thursday August 11, 2011 @03:33AM (#2871878)
I think what you are referring to as buffer is actually called a 'window' in vim. It is my understanding that a Tab can have Windows (created with veritical and horizontal splits), and Windows have buffers. You can switch buffers in the same Window, something I find very confusing.
Re:Advanced tricks with vim registers by donaq on Wednesday August 10, 2011 @09:56PM (#2871011)
I'm a recent buffer convert. Now I use them in combination with splits because there was this module I had to modify for work that involved >8 files. My monitor wasn't big enough to comfortably view 8 splits, so I learned how to use buffers. Also, you can use buffers and splits over ssh, but not tabs.
Re:Advanced tricks with vim registers by derobert on Thursday August 11, 2011 @02:52PM (#2874218)
You should be able to use graphical tabs over ssh with either X11 forwarding (ssh -X user@host) or by using vim's remote file syntax: gvim sftp://user@host/path/to/file (see :help netrw)
Re:Advanced tricks with vim registers by donaq on Thursday August 11, 2011 @09:17PM (#2875561)
I should have been clearer, perhaps. I meant with servers that don't usually run X. However, that remote file syntax thing I did not know about. Thanks!
Re:Advanced tricks with vim registers by derobert on Friday August 19, 2011 @02:14AM (#2902439)
The servers don't need to run X. X runs on your machine. The servers will need to have the X11 client libraries installed, but no X server.
Re:Advanced tricks with vim registers by subleq on Thursday August 11, 2011 @11:47AM (#2873343)
Why can't you use tabs over ssh? `gt`, `gT`, and `<n>gt` cycle forward through tabs, backwards, and go to the `n`th tab.
Re:Advanced tricks with vim registers by donaq on Thursday August 11, 2011 @09:24PM (#2875574)
Ooh, I did NOT know that tabbing works in a non-GUI environment! Once again, Hacker News has humbled me. Thanks!
Re:Advanced tricks with vim registers by Symmetry on Wednesday August 10, 2011 @05:09PM (#2870207)
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.
Re:Advanced tricks with vim registers by nyellin on Wednesday August 10, 2011 @05:17PM (#2870239)
You can also use "2p to paste the second to last deletion.
Re:Advanced tricks with vim registers by Davertron on Thursday August 11, 2011 @10:43AM (#2873067)
I didn't know about that trick either. I've been using YankRing (http://www.vim.org/scripts/script.php?script_id=1234) which would get me around the same problem. Basically, you hit p to paste, and if it's not what you want, you keep hitting C-p (control+p) and it will move backwards through time and keep pasting. Once you paste what you want, you're done! Awesome plugin that I can't live without.
Re:Advanced tricks with vim registers by nyellin on Wednesday August 10, 2011 @04:28PM (#2870032)
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

Re:Advanced tricks with vim registers by herTTTz on Wednesday August 10, 2011 @05:30PM (#2870289)
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.
Re:Advanced tricks with vim registers by guns on Wednesday August 10, 2011 @05:40PM (#2870335)
Give regbuf a try too: simpler implementation and features a buffer for previewing and editing registers.

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

Re:Advanced tricks with vim registers by kjell on Wednesday August 10, 2011 @11:28PM (#2871224)
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?"

Re:Advanced tricks with vim registers by sixtofour on Thursday August 11, 2011 @09:04AM (#2872595)
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.

Re:Advanced tricks with vim registers by jjcm on Wednesday August 10, 2011 @06:22PM (#2870478)
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.