Saturday, February 5, 2011

Delete lines matching hits

Hi there!

I make abundant use of a wonderful function I found at

http://vim.wikia.com/wiki/Copy_the_search_results_into_clipboard

to copy all matching lines, whole lines, containing hits.

I wonder if there's a way to do the same but instead of copying them
to the clipboard, delete them.

I very much appreciate your attention,

Eduard Fabra
http://www.google.com/profiles/edfabra

PS. The exact script I'm using is this:

" Use /pattern to search for something, then
" :call CopyMatchingLines()
" to copy all lines containing hits (whole lines).
" The pattern may extend over multiple lines.
" The 'normal! $' attempts to avoid copying the same line more than
once.
" BUG: For some patterns, it could miss a second hit?
function! CopyMatchingLines()
let posinit = getpos(".")
call cursor(1, 1)
let cnt = 0
let hits = []
let snum = search(@/, 'cW')
while snum > 0
let enum = search(@/, 'ceW')
call extend(hits, getline(snum, enum))
let cnt += 1
normal! $
let snum = search(@/, 'W')
endwhile
if cnt > 0
let @+ = join(hits, "\n") . "\n"
endif
call cursor(posinit[1], posinit[2])
echomsg cnt 'lines (or blocks) were appended to the clipboard.'
endfunction

--
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

No comments: