m (Reformat, categorize) |
mNo edit summary |
||
Line 9: | Line 9: | ||
|rating=4/1 |
|rating=4/1 |
||
|text= |
|text= |
||
− | 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 |
+ | 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). |
− | settings). |
||
− | |||
− | |||
<pre> |
<pre> |
||
"Intentionally left incomplete to be complete as needed |
"Intentionally left incomplete to be complete as needed |
||
Line 69: | Line 66: | ||
, March 6, 2004 12:33 |
, March 6, 2004 12:33 |
||
---- |
---- |
||
− | [[Category: |
+ | [[Category:Searching]] |
Revision as of 13:45, 27 September 2007
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