On Sunday, November 3, 2013 11:55:18 PM UTC-6, [email protected] wrote:
> Hi,
> 
> I want to format all the lines like "if(CONDITION)    return false;"
> 
> to:  "if(CONDITION)
> 
>                                  return false;"
> 
> 
> 
> ie) I want to separate  the 'if(CONSITION) statement' and 'the code
> 
> executed if that condition is true' into two seperate lines.
> 
> 
> 
> How to achieve this using vim.
> 

A very simple approach would be technically incorrect in some cases but may 
work for your needs:

  :%s#if\s*(\_[^)]*)\zs\s*\(.*;\)#\r\1

This will separate any single-condition if statements from code on the same 
line. You will then need to re-indent. You could do this with gg=G for the 
whole file, or be smarter and only reindent the line after any "if" statement 
with:

  :g#\<if\>#+norm! ==

If your actual code is more complex than that (including nested parentheses), 
then there is no good way to match arbitrary levels of nested parentheses using 
regular expression find+replace.

For this one method would be to record a macro to search for an if statement 
and fix it, then go to the beginning of the file and apply it as many times as 
needed. You can do "as many times as needed" in a way that stops when done by 
setting 'nowrapscan' and running the macro an excessive number of times. For 
example:

qa/\<if\>.*;<Enter>f(%lr<Enter>==q
:set nowrapscan
gg
9999999@a
:set wrapscan " optional

Unlike using a substitute command, this uses the % command to move to the 
matching parenthesis, instead of attempting to write a regex to count 
parentheses.

-- 
-- 
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to