5 ms·
Merge multiple lines in Vim
- jakejake 14y agoI can't imagine a situation where I'd need to join text this way, but it's impressive to see the powerful functions possible with vim. thanks for sharing!
- alecbenzer 14y agoI think the point was to be impressed with the example.
- sophacles 14y agoThe other day I had this exact situation. I didn't wan't to futz around with writing my own script to generate code, and the values I needed had to be hard coded into defines anyway, so I just created a empty line below my defines, then did: :r !$SHELL_COMMAND_TO_PRINT_SOME_VALS to get the values in the buffer below where I had already done the series of #define NAME1 ... #define NAMEN so I just needed to move the values I read into the buffer up to the defines. (using the block select method). Anyway, now that you know about it, you may find yourself using this trick more :) (I usually use it in context of config files and :r !ls than programming, but the above was my most recent. )
- SeoxyS 14y agoI posted my solution on StackOverflow[1], but I like my way better than the top post: Here's how I'd do it (with the cursor on the first line): qama:5y$'a$p:5dd'ajq3@a You need to know two things: - The line number on which the first line of the second group starts (5 in my case), and the number of lines in each group (3 in my example). Here's what's going on: `qa` records everything up to the next `q` into a "buffer" in `a`. `ma` creates a mark on the current line. `:5` goes to the next group. `y$` yanks the rest of the line. `'a` returns to the mark, set earlier. `$p` pastes at the end of the line. `:5` returns to the second group's first line. `dd` deletes it. `'a` returns to the mark. `jq` goes down one line, and stops recording. `3@a` repeats the action for each line (3 in my case) [1]: http://stackoverflow.com/questions/10760326/merge-multiple-lines-in-vim/10762846#10762846 http://stackoverflow.com/questions/10760326/merge-multiple-l...
- mdonahoe 14y agoMacros and block-select are my favorite infrequently used parts of Vim. Every time I have a need for either, I grin.
- ibotty 14y agoyes. and macros are so easy to use. i usually start a macro before i really know how i am supposed to do it. it's just a "do it for this one occurence" but prepended by qa and ended by q. well i will have to move the cursor to the right spot again. but that is good habit anyway.
- tux1968 14y agoUsing a file with the same input of the top answer: /harder<enter>1Gqa''dd''pkJ0jq3@a Edit: notice that those are each 2 single quotes, not "
- mdonahoe 14y agoI assume most people interested enough in vim to click on the comments with will have heard of Vimgolf. http://vimgolf.com/ http://vimgolf.com/ But if not, it is a great way to learn the less obvious tricks that Vim has to offer.
- tux1968 14y agoWas going to submit a solution but it appears you can only sign in with a twitter account? What an odd thing.