Skip to content

Instantly share code, notes, and snippets.

@hcs42
Created October 12, 2016 15:47
Show Gist options
  • Save hcs42/acb406f5ef65e89690f34e30ff20650c to your computer and use it in GitHub Desktop.
Save hcs42/acb406f5ef65e89690f34e30ff20650c to your computer and use it in GitHub Desktop.
FindInBuffers
" Usage:
"
" :FindInBuffers "regex"
" Buffer index, buffer name
" 0 file1
" 7 file2
command! -nargs=1 FindInBuffers call FindInBuffers(<args>)
function! FindInBuffers(pattern)
let last = bufnr('$')
let buf_index = 0
let found = 0
echo "Buffer index, buffer name"
while buf_index <= last
if buflisted(buf_index) && bufloaded(buf_index)
let buf_lines = getbufline(buf_index, 1, '$')
let buf_contains = 0
for line in buf_lines
if line =~ a:pattern
let buf_contains = 1
break
endif
endfor
if buf_contains
echo buf_index . ' ' . bufname(buf_index)
endif
let found = 1
endif
let buf_index = buf_index + 1
endwhile
if found == 0
echo "No match."
endif
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment