Vim Tips Wiki
No edit summary
 
m (Reformat, categorize)
Line 4: Line 4:
 
|title=Add a newline after given pattern(s)
 
|title=Add a newline after given pattern(s)
 
|created=March 5, 2004 23:58
 
|created=March 5, 2004 23:58
|complexity=intermediate
+
|complexity=basic
 
|author=parv
 
|author=parv
 
|version=6.0
 
|version=6.0
 
|rating=4/1
 
|rating=4/1
 
|text=
 
|text=
After having gone numb when trying to de parse HTML source code w/
+
After having gone numb when trying to de parse HTML source code with very long lines, I created the following function, thus macro and command. It takes a list of one or more patterns/strings, and adds a newline after each. (Wrapping/Indentation is controlled by your own
 
settings).
   
very long lines, i created the following function, thus macro and
 
   
  +
<pre>
command. It takes a list of one or more patterns/strings, and adds a
 
 
"Intentionally left incomplete to be complete as needed
 
nnoremap ,nl :NewLine
   
 
"Add line breaks in after given strings/regex
newline after each. (Wrapping/Indentation is controlled by your own
 
 
com! -nargs=+ -range -bar NewLine <line1>,<line2>call AddNewLine(<f-args>)
   
 
function! AddNewLine(...) range
settings.)
 
 
let str_no = 1
   
 
while str_no <= a:0
 
exec 'let var = a:' . str_no
   
 
" ` (backquote) is used as delimiters for s///, which is hard
 
" to distinguish but also is much rarer than delimiters.
  +
"
 
" And, "No Match found" messages are suppressed (s///e)
  +
"
 
" (The "exec..." is one long line.)
 
exec a:firstline . "," . a:lastline . 's`\(' . var . '\)\($\)\@!`\1\r`ge'
   
 
let str_no = str_no +1
&lt;code&gt;
 
 
endwhile
 
" Intentionally left incomplete to be complete as needed
 
 
nnoremap ,nl :NewLine
 
 
 
 
" Add line breaks in after given strings/regex
 
 
com! -nargs=+ -range -bar NewLine &lt;line1&gt;,&lt;line2&gt;call AddNewLine(&lt;f-args&gt;)
 
 
 
 
function! AddNewLine(...) range
 
 
let str_no = 1
 
 
 
 
while str_no &lt;= a:0
 
 
exec 'let var = a:' . str_no
 
 
 
 
" ` (backquote) is used as delimiters for s///, which is hard
 
 
" to distinguish but also is much rarer than delimiters.
 
 
"
 
 
" And, "No Match found" messages are suppressed (s///e)
 
 
"
 
 
" (The "exec..." is one long line.)
 
 
exec a:firstline . "," . a:lastline . 's`\(' . var . '\)\($\)\@!`\1\r`ge'
 
 
 
 
let str_no = str_no +1
 
 
endwhile
 
 
 
 
unlet! var
 
 
unlet str_no
 
 
endfunction
 
 
&lt;/code&gt;
 
   
 
unlet! var
 
unlet str_no
 
endfunction
  +
</pre>
   
   
  +
''Note:'' The substitution is done on a pattern which is NOT ALREADY AT THE END of the line. The default range is limited to current line; specify other range just like any other Vim command.
   
 
}}
 
}}
   
 
== Comments ==
 
== Comments ==
 
Why not just use the 's'ubstitution command and ^V^M (ctrl-v, ctrl-m) (or ^Q^M)?
I forgot to mention that default range is limited to current line;
 
specify other range just like any other Vim command. And, that "com"
 
line is also one long line.
 
 
(Argh! Missed that "to be complete"...)
 
 
 
'''Anonymous'''
 
, March 6, 2004 0:09
 
----
 
Why not just use the 's'ubstitution command and ^V^M (ctrl-v, ctrl-m) (or ^Q^M)?
 
 
To put a new line after each &lt;br /&gt; tag in your html try something like
 
   
 
To put a new line after each &lt;br /&gt; tag in your html try something like:
  +
<pre>
 
:%s/&lt;br \/&gt;/&lt;br \/&gt;^V^M/g
 
:%s/&lt;br \/&gt;/&lt;br \/&gt;^V^M/g
  +
</pre>
   
 
andy47--AT--halfcooked.com
 
andy47--AT--halfcooked.com
 
, March 6, 2004 2:51
 
, March 6, 2004 2:51
 
----
 
----
( In OP i forgot to explicitly mention that the the substitution is done
 
on a pattern which is NOT ALREADY AT THE END of the line. In
 
addition, it should have been labeled Basic ... Only if i could
 
rewind time ... )
 
 
 
To Andy47,
 
To Andy47,
   
  +
I wrote the function mainly because to get the effect of giving OR'd pattern, in LHS of s///, simply separated by spaces w/o having to escape the darned "|"; also, to not having to remember to put "\($\)\--AT--!" -- zero width negative look ahead assertion for end-of-line -- on each invocation of s///. \--AT--! is used to limit the number of useless blank lines.
I wrote the function mainly because to get the effect of giving OR'd
 
pattern, in LHS of s///, simply separated by spaces w/o having to
 
escape the darned "|"; also, to not having to remember to put
 
"\($\)\--AT--!" -- zero width negative look ahead assertion for end-of-line
 
-- on each invocation of s///. \--AT--! is used to limit the number of
 
useless blank lines.
 
 
As to using ^V^M, instead of \r i suppose, ... I tried using \&lt;CR&gt; ,
 
&lt;CR&gt; , ^V^M and ^M in a normal macro, which was not including a new
 
line. (I had not tried ^Q^M.) Instead, search for 'g' was being
 
initiated, given macro's RHS was ":%s/\(&gt;\)\($\)\--AT--!/\1^M/g" (where ^M
 
is one of \&lt;CR&gt; , &lt;CR&gt; , ^V^M, ^M).
 
 
The same excat macro works as desired when manually executed. After
 
wasting time w/ above four character sequences, i used \r.
 
   
  +
As to using ^V^M, instead of \r I suppose, ... I tried using \&lt;CR&gt; , &lt;CR&gt; , ^V^M and ^M in a normal macro, which was not including a new line. (I had not tried ^Q^M.) Instead, search for 'g' was being initiated, given macro's RHS was ":%s/\(&gt;\)\($\)\--AT--!/\1^M/g" (where ^M is one of \&lt;CR&gt; , &lt;CR&gt; , ^V^M, ^M).
   
 
The same exact macro works as desired when manually executed. After wasting time w/ above four character sequences, I used \r.
- Parv
 
   
 
parv
 
parv
 
, March 6, 2004 12:33
 
, March 6, 2004 12:33
 
----
 
----
  +
[[Category::Search]]
<!-- parsed by vimtips.py in 0.756181 seconds-->
 

Revision as of 13:43, 27 September 2007

Previous TipNext Tip

Tip: #671 - Add a newline after given patterns

Created: March 5, 2004 23:58 Complexity: basic Author: parv Version: 6.0 Karma: 4/1 Imported from: Tip#671

After having gone numb when trying to de parse HTML source code with very long lines, I created the following function, thus macro and command. It takes a list of one or more patterns/strings, and adds a newline after each. (Wrapping/Indentation is controlled by your own settings).


"Intentionally left incomplete to be complete as needed
nnoremap ,nl :NewLine

"Add line breaks in after given strings/regex
com! -nargs=+ -range -bar NewLine <line1>,<line2>call AddNewLine(<f-args>)

function! AddNewLine(...) range
let str_no = 1

while str_no <= a:0
    exec 'let var = a:' . str_no

    " ` (backquote) is used as delimiters for s///, which is hard
    "  to distinguish but also is much rarer than delimiters.
    "
    "  And, "No Match found" messages are suppressed (s///e)
    "
    "  (The "exec..." is one long line.)
    exec a:firstline . "," . a:lastline . 's`\(' . var . '\)\($\)\@!`\1\r`ge'

    let str_no = str_no +1
endwhile

unlet! var
unlet str_no
endfunction 


Note: The substitution is done on a pattern which is NOT ALREADY AT THE END of the line. The default range is limited to current line; specify other range just like any other Vim command.

Comments

Why not just use the 's'ubstitution command and ^V^M (ctrl-v, ctrl-m) (or ^Q^M)?

To put a new line after each <br /> tag in your html try something like:

:%s/<br \/>/<br \/>^V^M/g

andy47--AT--halfcooked.com , March 6, 2004 2:51


To Andy47,

I wrote the function mainly because to get the effect of giving OR'd pattern, in LHS of s///, simply separated by spaces w/o having to escape the darned "|"; also, to not having to remember to put "\($\)\--AT--!" -- zero width negative look ahead assertion for end-of-line -- on each invocation of s///. \--AT--! is used to limit the number of useless blank lines.

As to using ^V^M, instead of \r I suppose, ... I tried using \<CR> , <CR> , ^V^M and ^M in a normal macro, which was not including a new line. (I had not tried ^Q^M.) Instead, search for 'g' was being initiated, given macro's RHS was ":%s/\(>\)\($\)\--AT--!/\1^M/g" (where ^M is one of \<CR> , <CR> , ^V^M, ^M).

The same exact macro works as desired when manually executed. After wasting time w/ above four character sequences, I used \r.

parv , March 6, 2004 12:33


[[Category::Search]]