I just want to transform

for(int i = 1; i <= N; i++) if(ord[i] == -1) dfs(i, 0, i);


for(int i = 1; i <= N; i++) {
    if(ord[i] == -1) dfs(i, 0, i);

as little key I should press as possible.

I found the plugin 'surround' but It seems like it has some functions(like 'yss{') that surrounds entire text line with brackets but not the block I want to surround. And if it is possible, i have to add some new lines at some position, so i think it will be so uncomfortable.

And I also found some original vim commands, but I was too long and seems like taking too much time.

I'll be so glad if you guys tell me which plugin i should use. Thank you!


The surround plugin does actually support this: If your cursor is in the i of the if, pressing yS${ or ySf;{ will have the desired effect.

In general, substituting an uppercase S for the lowercase s in most of the plugin's shortcuts will do a linewise operation instead of the default characterwise one.

  • Wow! Thank you so much. You are so genius – david Hwang Oct 1 '17 at 23:46

I'm quite surprised surround plugin doesn't handle this. If it's not there by default, I'm quite certain though that it can be configured to behave this way.

Any way, in lh-brackets, with the default settings, you can select (in visual mode) from if to the semi-colon and hit your leader key (backspace by default) followed by {.enter image description here

In the screencast, <space> is my leader key, and ctrl+alt+4 relates to { on my French keyboard.

But honestly, if this is some C++ code, you'd better configure clang-format to your liking and run it on the current line that you would select. This way braces would also be added around the true path.

  • 2
    Besides lh-brackets and surround (S in visual mode, or with more advanced config), the plugin splitjoin.vim is designed to handle this exact case (in many languages) – Mass Oct 1 '17 at 20:04
  • After i install and add 'LucHermitte/lh-vim-lib' and 'LucHermitte/lh-brackets' when i paste code 'for(int i = 1; i <= N; i++) if(ord[i] == -1) dfs(i, 0, i);' from my clipboard, It became 'for(int i = 1); i <= N; i++) if(ord[i] == -1) dfs(i, 0, i);' but if i commentize those plugins, there are no problems. What should i do? – david Hwang Oct 1 '17 at 23:16
  • @davidHwang That's odd. Whether I paste in normal mode, or in insert mode (with mouse middle button, or with i_CTRL-R_+/p) I don't observe anything like that. What you observe looks more like if you have typed the text manually -- I've configured the semi colon to close all opened parenthesis, and sometimes its behaviour can become quite annoying even with the abbreviations/Snippets I define in lh-cpp. How do you paste? And what :set tells you? Note: it'll may be best to continue this discussion either by mail or though an issue that you could open. – Luc Hermitte Oct 2 '17 at 0:29
  • 1
    @LucHermitte while trying to help out @davidHwang with his other question I was able to reproduce the issue. I was pasting from the clipboard with <C-V> while in insert mode (the ubuntu bash terminal equivalent of <C-v>. I believe invoking such a paste does indeed behave as if it were pasting each char manually. Ie if I am in normal mode the text would have only pasted nt i = 1); i <= N; i++) if(ord[i] == -1) dfs(i, 0, i); as it wouldnt be in insert mode till an i is pasted – ljden Oct 2 '17 at 7:23
  • @ljden. Thanks for the explanation. I'll have to improve context analysis in ; mapping. – Luc Hermitte Oct 2 '17 at 8:50

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.