Vim Tips Wiki
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
Vim Tips Wiki
1,649
pages
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
Editing
List lines with current search pattern highlighted
Back to page
Edit
Edit source
View history
Talk (0)
Edit Page
List lines with current search pattern highlighted
We recommend that you
log in
before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your
Watchlist
.
Creating an account
is quick and free.
The edit appears to have already been undone.
Anti-spam check. Do
not
fill this in!
{{review}} {{TipImported |id=1141 |previous=1134 |next=1142 |created=2006 |complexity=intermediate |author=Yakov Lerner |version=6.0 |rating=10/4 |category1=Searching |category2= }} 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. <pre> " 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 </pre> ==Comments== Just a small issue, it chokes on: <pre> :g/^/PP </pre> ---- 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. 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): <pre> nmap [I :execute 'g/\<' . expand( '<cword>' ) . '\>/PP #'<CR> </pre> 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: <pre> "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) </pre> 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 "number :g/^\s*\d\{,4}/PP "trailing spaces :g/\s*$/PP "dash line :g/-*/PP </pre> ---- <pre> ... let e=matchend(l,@/,index) + if e == b + let e = e+1 + endif ... </pre> 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: <pre> - echon strpart(l." ",b,e-b) + echon strpart(l." ",b,e-b) </pre> will display '$' correctly too. ----
Summary:
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Navigation
(
view source
)
Template:Review
(
view source
)
Template:TipImported
(
view source
)
Follow on IG
TikTok
Join Fan Lab