2

What's the quickest (as far as anyone knows) way to change this:

int main()
{

into this:

int main() {

provided the cursor is on the {?

  • 4
    kJ? (and some more text is here because comments need it :) ) – grochmal Apr 5 '17 at 1:18
  • @grochmal That looks like an answer, to me! – Rich Apr 5 '17 at 8:35
1

It actually took me a little while to figure this out on my own, too: dh

Now, I use a macro for "merging" lines like this: let @m=' 0i ^[0wd0dhi ^[' This will take the current line, with the cursor positioned anywhere on that line and merge it with the previous line, while inserting a space, and leave you back in NORMAL mode. Note: the ^[ are escapes gotten by typing ctrl-r<Esc> which is control-r then the esc key. To be sure you've done it correctly, those two characters will behave as one if you try and move the cursor over them.

Put the above line in your ~/.vimrc or ~/.vimrc.local and then either restart vim or do :source ~/.vimrc

| improve this answer | |
  • 1
    dh will result in int main(){, not int main() {, and it only works if you've added h to your whichwrap setting. – Rich Apr 5 '17 at 8:35
  • Previous comment reads more confrontational than I meant it to! Just thought they were caveats to your otherwise nifty technique that might be worth mentioning. – Rich Apr 5 '17 at 8:50
  • Thanks. I wasn't aware of the whichwrap setting. Learning something new every day. – GH05T Apr 5 '17 at 13:01
1

Besides that I think that opening braces on a separate line is the better style, you can do

:g/^\s*{\s*$/-1j

to append all occurences in the whole file of opening braces in a separate line to the previous line.

If this is your coding style (for some no good reason), you can add this to your .vimrc to be executed automatically when opening source code files.

| improve this answer | |
1

you can use the following command in normal mode if cursor is at bracket.

gqk

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.