1

In Vim, oftens I happen to be using some completion that vim provides for me in the Ctrl n/Ctrl p menu. If the completion happens to be from some other file, I know that Vim knows where it comes from as well, since it's able to display the file name that contains the completion for me.

Sometimes, after making use of the completion, I'd like to jump directly to the said file to inspect further. Is this possible?

2
  • 1
    Vim certainly knows a lot of things but it doesn't necessarily expose all of it to the user, even the enterprising one.
    – romainl
    Aug 31 at 12:34
  • The general solution seems to be to install a plugin that adds LSP functionality.
    – Friedrich
    Aug 31 at 13:14

1 Answer 1

0

Vim has no way to know which buffer contains the definition of the term or its utilization.

It looks to me that what you ask is to find where the term has been used in the opened buffers.

You can achieve that with the following custom command

function! Vimgrepall(pattern)
  call setqflist([])
  exe 'bufdo vimgrepadd ' . a:pattern . ' %'
  cnext
endfunction
command! -nargs=1 FindOccurences call Vimgrepall(<f-args>)

From the VimWiki: Search on all opened buffers

If you want to exclude the current buffer from the search you could do:

function! Vimgrepall(pattern)
  let current_buffer = bufnr('%')
  call setqflist([])
  let buffers = filter(range(1, bufnr('$')), 'bufexists(v:val)')
  for buffer in buffers
    if buffer == current_buffer ||bufname(buffer) == '' || !buflisted(buffer)
      continue
    endif
    
    silent! exe 'vimgrepadd ' . a:pattern . ' ' . escape(bufname(buffer), ' ')
  endfor

  cnext
endfunction

command! -nargs=1 FindOccurences call Vimgrepall(<f-args>)
2
  • Thanks for your answer! I've edited the title of the question to clarify that the actual location of the 'definition' for the said completion is not so important to me, as just being able to jump to what I can already select in the completion. With this solution, one would have to sort through the found results to find the (specific) file that contained the completion again, but vim had already been able to show me the sought for file in the completion menu. I feel like there should be a better way. Aug 31 at 12:12
  • 1
    I doubt that Vim extract the completion from a specific file. I believe Vim make the list of all the terms/words and used it for completion. Aug 31 at 12:55

Your Answer

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

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