Vim Tips Wiki
No edit summary
 
(Change <tt> to <code>, perhaps also minor tweak.)
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=1141
 
|id=1141
  +
|previous=1134
|title=command PP: print lines like
 
  +
|next=1142
|created=February 20, 2006 1:14
+
|created=2006
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Yakov Lerner
 
|author=Yakov Lerner
 
|version=6.0
 
|version=6.0
 
|rating=10/4
 
|rating=10/4
  +
|category1=Searching
|text=
 
  +
|category2=
This command (PP) prints lines (like :p or :&#35;) with search pattern highlighted. I use g//p
 
 
quite often, and I was missing the highlighting of search pattern. To test this command, try
 
 
something like:
 
 
:g/a/PP
 
 
If you supply optional argument &#35; (PP &#35;) then line numbers are also printed a-la :&#35;
 
 
 
 
"-------------------------------------------------------------------------------------------------
 
 
" command PP: print lines like :p or :&#35; but with with current search pattern highlighted
 
 
command! -nargs=? -range -bar PP :call PrintWithSearchHighlighted(&lt;line1&gt;,&lt;line2&gt;,&lt;q-args&gt;)
 
 
function! PrintWithSearchHighlighted(line1,line2,arg)
 
 
let line=a:line1
 
 
while line &lt;= a:line2
 
 
echo ""
 
 
if a:arg =~ "&#35;"
 
 
echohl LineNr
 
 
echo strpart(" ",0,7-strlen(line)).line."\t"
 
 
echohl None
 
 
endif
 
 
let l=getline(line)
 
 
let index=0
 
 
while 1
 
 
let b=match(l,@/,index)
 
 
if b==-1 |
 
 
echon strpart(l,index)
 
 
break
 
 
endif
 
 
let e=matchend(l,@/,index) |
 
 
 
 
echon strpart(l,index,b-index)
 
 
echohl Search
 
 
echon strpart(l,b,e-b)
 
 
echohl None
 
 
let index = e
 
 
endw
 
 
let line=line+1
 
 
endw
 
 
endfu
 
 
 
 
}}
 
}}
  +
This command (<code>PP</code>) prints lines (like <code>:p</code> or <code>:#</code>) with the search pattern highlighted. I use <code>g//p</code> quite often, and I was missing the highlighting of search pattern. To test this command, try something like:
  +
<pre>
 
:g/a/PP
  +
</pre>
   
 
If you supply the optional argument <code>#</code> (<code>PP #</code>) then line numbers are also printed.
== Comments ==
 
  +
<pre>
Good idea, Yakov. Just a small issue, it chokes on:
 
 
" command PP: print lines like :p or :# but with with current search pattern highlighted
 
command! -nargs=? -range -bar PP :call PrintWithSearchHighlighted(<line1>,<line2>,&lt;q-args>)
 
function! PrintWithSearchHighlighted(line1,line2,arg)
 
let line=a:line1
 
while line <= a:line2
 
echo ""
 
if a:arg =~ "#"
 
echohl LineNr
 
echo strpart(" ",0,7-strlen(line)).line."\t"
 
echohl None
  +
endif
 
let l=getline(line)
 
let index=0
 
while 1
 
let b=match(l,@/,index)
 
if b==-1 |
 
echon strpart(l,index)
  +
break
  +
endif
  +
let e=matchend(l,@/,index) |
 
echon strpart(l,index,b-index)
 
echohl Search
 
echon strpart(l,b,e-b)
 
echohl None
 
let index = e
  +
endw
 
let line=line+1
  +
endw
  +
endfunction
  +
</pre>
   
 
==Comments==
 
Just a small issue, it chokes on:
  +
<pre>
 
:g/^/PP
 
:g/^/PP
  +
</pre>
   
Gerald Lai
 
, February 23, 2006 18:50
 
 
----
 
----
A small addition:
+
A small addition:
  +
<pre>
 
nmap [I :execute 'SS g/\<' . expand( '<cword>' ) . '\>/PP #'<CR>
  +
</pre>
   
 
Changes the behaviour of the internal <code>[I</code> to highlight the text now.
nmap [I :execute 'SS g/\&lt;' . expand( '&lt;cword&gt;' ) . '\&gt;/PP &#35;'&lt;CR&gt;
 
   
 
I noticed that it breaks on ^ and $ (if they're alone), also, but that doesn't really happen that often.
Changes the behaviour of the internal [I to highlight the text now.
 
   
I noticed that it breaks on ^ and $ (if they're alone), also, but that doesn't really happen that often -- I don't think I've ever searched for ^ or $ -- that's just like looking at the main edit window, to me, cuz it returns every line :)
 
 
salmanhalim--AT--hotmail.com
 
, February 25, 2006 12:21
 
 
----
 
----
I think you meant (without SS) :)
+
I think you meant (without SS):
  +
<pre>
nmap [I :execute 'g/\&lt;' . expand( '&lt;cword&gt;' ) . '\&gt;/PP &#35;'&lt;CR&gt;
+
nmap [I :execute 'g/\<' . expand( '<cword>' ) . '\>/PP #'<CR>
  +
</pre>
   
Yes, [I with highlighting is a great idea. Now the only thing lacking is the jump numbers at the start of each line that are right-aligned.
+
Yes, <code>[I</code> with highlighting is a great idea. Now the only thing lacking is the jump numbers at the start of each line that are right-aligned.
   
About choking on ^ and $ anchors, it actually poses minor practical problems. Searching for ^ or $ alone is useful to indicate the whole line. In the case for ranges:
+
About choking on ^ and $ anchors, it actually poses minor practical problems. Searching for ^ or $ alone is useful to indicate the whole line. In the case for ranges:
   
  +
<pre>
"print lines 3 to 34, including empty lines
+
"print lines 3 to 34, including empty lines
:3,34g/^/p
+
:3,34g/^/p
"how would we do this without using ^ or $?
+
"how would we do this without using ^ or $?
:3,34g/\_./PP
+
:3,34g/\_./PP
"is almost the same but not quite (at EOF)
+
"is almost the same but not quite (at EOF)
  +
</pre>
   
Anything that can possibly evaluate to matching nothing but anchors ^ or $ or void will choke it too. But these aren't practical:
+
Anything that can possibly evaluate to matching nothing but anchors ^ or $ or void will choke it too. But these aren't practical:
   
  +
<pre>
:g/^\s*\(function\)\=/PP
+
:g/^\s*\(function\)\=/PP
"number
+
"number
:g/^\s*\d\{,4}/PP
+
:g/^\s*\d\{,4}/PP
"trailing spaces
+
"trailing spaces
:g/\s*$/PP
+
:g/\s*$/PP
"dash line
+
"dash line
 
:g/-*/PP
 
:g/-*/PP
  +
</pre>
   
Gerald Lai
 
, February 25, 2006 15:14
 
 
----
 
----
  +
<pre>
...
+
...
let e=matchend(l,--AT--/,index)
 
 
let e=matchend(l,@/,index)
+ if e == b
 
+ let e = e+1
+
+ if e == b
 
+ let e = e+1
+ endif
 
 
+ endif
...
+
...
  +
</pre>
   
should solve problem with zero length regexps - they are displayed as in vim - next character is highlighted.
+
should solve problem with zero length regexps - they are displayed as in Vim - next character is highlighted. (there is still another problem with match function - so '^', '\<' aren't matched correctly)
(there is still another problem with match function - so '^', '\&lt;' aren't matched correctly)
 
   
Marian Csontos
 
, March 2, 2006 3:50
 
 
----
 
----
and this:
+
and this:
  +
<pre>
- echon strpart(l." ",b,e-b)
 
+ echon strpart(l." ",b,e-b)
+
- echon strpart(l." ",b,e-b)
 
+ echon strpart(l." ",b,e-b)
will display '$' correctly too
 
  +
</pre>
  +
 
will display '$' correctly too.
   
M.C.
 
, March 2, 2006 3:53
 
 
----
 
----
<!-- parsed by vimtips.py in 0.692112 seconds-->
 

Latest revision as of 06:09, 13 July 2012

Tip 1141 Printable Monobook Previous Next

created 2006 · complexity intermediate · author Yakov Lerner · version 6.0


This command (PP) prints lines (like :p or :#) with the search pattern highlighted. I use g//p quite often, and I was missing the highlighting of search pattern. To test this command, try something like:

:g/a/PP

If you supply the optional argument # (PP #) then line numbers are also printed.

" command PP: print lines like :p or :# but with with current search pattern highlighted
command! -nargs=? -range -bar PP :call PrintWithSearchHighlighted(<line1>,<line2>,<q-args>)
function! PrintWithSearchHighlighted(line1,line2,arg)
  let line=a:line1
  while line <= a:line2
    echo ""
    if a:arg =~ "#"
      echohl LineNr
      echo strpart(" ",0,7-strlen(line)).line."\t"
      echohl None
    endif
    let l=getline(line)
    let index=0
    while 1
      let b=match(l,@/,index)
      if b==-1 |
        echon strpart(l,index)
        break
      endif
      let e=matchend(l,@/,index) |
      echon strpart(l,index,b-index)
      echohl Search
      echon strpart(l,b,e-b)
      echohl None
      let index = e
    endw
    let line=line+1
  endw
endfunction

Comments[]

Just a small issue, it chokes on:

:g/^/PP

A small addition:

nmap [I :execute 'SS g/\<' . expand( '<cword>' ) . '\>/PP #'<CR>

Changes the behaviour of the internal [I to highlight the text now.

I noticed that it breaks on ^ and $ (if they're alone), also, but that doesn't really happen that often.


I think you meant (without SS):

nmap [I :execute 'g/\<' . expand( '<cword>' ) . '\>/PP #'<CR>

Yes, [I with highlighting is a great idea. Now the only thing lacking is the jump numbers at the start of each line that are right-aligned.

About choking on ^ and $ anchors, it actually poses minor practical problems. Searching for ^ or $ alone is useful to indicate the whole line. In the case for ranges:

"print lines 3 to 34, including empty lines
:3,34g/^/p
"how would we do this without using ^ or $?
:3,34g/\_./PP
"is almost the same but not quite (at EOF)

Anything that can possibly evaluate to matching nothing but anchors ^ or $ or void will choke it too. But these aren't practical:

:g/^\s*\(function\)\=/PP
"number
:g/^\s*\d\{,4}/PP
"trailing spaces
:g/\s*$/PP
"dash line
:g/-*/PP

...
 let e=matchend(l,@/,index)
+ if e == b
+ let e = e+1
+ endif
...

should solve problem with zero length regexps - they are displayed as in Vim - next character is highlighted. (there is still another problem with match function - so '^', '\<' aren't matched correctly)


and this:

- echon strpart(l." ",b,e-b)
+ echon strpart(l." ",b,e-b)

will display '$' correctly too.