0

Let's say I entered a folder with many files and I opened them with vim *. I know that if I want to list all the files inside the buffer I can use the command :ls, and then I can navigate those files by seeing their id number, like using :b1 or :b5 to open a specific file.

I also know that I can use the command :vimgrep /expression/ ## to add specific lines of those files on the quickfix list and navigate through them very quickly.

However, I'd like to filter and list files by name just like I'm capable to do on the terminal... Something like :ls | grep '.*txt'. Is it possible to do? This last command doesn't work. Can I use grep with :ls inside Vim or is there an alternative command to achieve this same goal?

0

1 Answer 1

4

Easy answer: use tab completion with :buffers.

  1. Type :b (there's a space at the end)
  2. Enter any part of a buffer name.
  3. Press tab.

If the pattern matches a single buffer you'll be switched to that buffer immediately. Otherwise, you'll see the matches in the standard tab completion way, i.e. all the matching buffers will be shown and you tab through to the one you want to open.

If you want something a bit more sophisticated that allows search with regular expressions and includes a nice menu when there are multiple matches then take a look at this function and command from https://vim.fandom.com/wiki/Easier_buffer_switching

function! BufSel(pattern)
  let bufcount = bufnr("$")
  let currbufnr = 1
  let nummatches = 0
  let firstmatchingbufnr = 0
  while currbufnr <= bufcount
    if(bufexists(currbufnr))
      let currbufname = bufname(currbufnr)
      if(match(currbufname, a:pattern) > -1)
        echo currbufnr . ": ". bufname(currbufnr)
        let nummatches += 1
        let firstmatchingbufnr = currbufnr
      endif
    endif
    let currbufnr = currbufnr + 1
  endwhile
  if(nummatches == 1)
    execute ":buffer ". firstmatchingbufnr
  elseif(nummatches > 1)
    let desiredbufnr = input("Enter buffer number: ")
    if(strlen(desiredbufnr) != 0)
      execute ":buffer ". desiredbufnr
    endif
  else
    echo "No matching buffers"
  endif
endfunction

"Bind the BufSel() function to a user-command
command! -nargs=1 Bs :call BufSel("<args>")

This will...

jump to the matching buffer if only one match is found, or if there are many matches it will print a list of the matching buffers in the command-line area, and allow you to select one of the matching buffers by buffer number.

1
  • 1
    Thanks! The first solution works for me... In the end, it was a small detail that I was missing. The script looks promising but in my case, I prefer something that doesn't require configuration changes that I can accomplish on any computer with Vim installed. So I'm sticking with the b, space and tab strategy.
    – raylight
    Commented May 2, 2021 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.