开发者

VIM 7: How to navigate the code source tree from the root of the code base, in an efficient manner?

开发者 https://www.devze.com 2023-02-18 19:19 出处:网络
I have a system of navigating the code across multiple files in a huge code base, and I want to improve/fix a drawback that it currently has :

I have a system of navigating the code across multiple files in a huge code base, and I want to improve/fix a drawback that it currently has :

My shell is pre-configured to open at the root of my code base - lets call it Dev/. During syncing/code building, I have a script which automatically stores the relative path of all .h and .c files in a single file to be used by cscope 开发者_Go百科(lets call it cscope.files).

Once I sync, this file is updated - and then I can open any file I want in vim using the following command from Dev/ :

vif "part of file name",

where

vif:     aliased to vi `grep !:1 cscope.files`

Provided I give a part of the filename long enough to uniquely identify it, I can immediately open it in vim.

Now, the drawback to this approach is, when I've already opened one file, and jump to another file without exiting vim, the only way I can do so is

:!vif *file2*

This spawns a new shell and then opens the file in a vim launched there. As a result, I can't switch between the two files (using Ctrl-^). I'm unable to come up with a solution that :

a) Lets me open any file from Dev/ instantly

b) Lets me open any other file inside vim (once I've opened an existing file) in the same shell, so that the 2 vim sessions are aware of each other (I can hop between the 2 using Ctrl-^)

I know this is a long question (how does one google this :) ), but I'm betting the solution is simple and obvious to someone more proficient in vim !!

Let me know if any part of the question is fuzzy, and I'll clarify it...

UPDATE: I ultimately went the cscope way, after customizing using a shortcut (as using 'gf' on cscope.files still prevented me from toggling between 2 source files). See VIM 7 and cscope: Using "cscope find f" inside a keyboard mapping for switching between files for the shortcut.


Use vim's grep in something like this:

:map <F1> :vim <pattern> cscope.files<CR>gf

For example, with this:

vnoremap <F1> "ry:exe ':1vim /'.@r.'/ cscope.files'<CR>gf

you select (visual mode) the pattern you'd like to search for and then press F1. The first file that matches the pattern will open, replacing the current buffer*.

* If this is possible. i.e if current buffer is saved or if hidden is set etc.

If you prefer to get a prompt, use input():

nnoremap <F1> :exe ':1vim /'.input("Enter pattern: ").'/ cscope.files'<CR>

[ but then you have to manually gf because input() consumes the remaining characters of the map. To avoid this, you can use inputsave() and inputrestore() ]

update

... for example like this:

function! GetPat()
  call inputsave()
  let mypat = input("Enter pattern: ")
  call inputrestore()
  return mypat
endfunction

nnoremap <F1> :exe ':1vim /'.GetPat().'/ cscope.files'<CR>gf


I think that the Vim Fuzzy Finder plugin is well adapted to your use case.

As the name implies, using the plugin, you can find files using a fuzzy text search.

Additionnaly, it also works for other Vim ressources like buffers, tags, etc.


I don't use cscope myself, but it seems that you can use it to find files, see :help cscope-find.

Otherwise, something like (not tested) this could help:

"Custom function
function! MyFunc(pat)
  " Get files list
  let filelist = readfile('path/to/cscope.files')
  " Filter non matching item out and see if only one item is left
  if len(filter(filelist, 'v:var =~? '.a:pat)) == 1
    " edit file
    exec 'edit '.filelist[0]
  else
    " Report back
    echom 'More than one match:'
    for file in filelist
      echom file
    endfor
  endif
endfunction
" Custom command
command! -bar -nargs=1 MyCom call MyFunc(<args>)


Also try using the built-in cscope integration:

:cs find f stdio.h

Cscope tag: stdio.h
   #   line  filename / context / line
   1      1  /usr/include/stdio.h <<<unknown>>>
   2      1  /usr/include/bits/stdio.h <<<unknown>>>
Type number and <Enter> (empty cancels):

See :help cscope-suggestions for some mappings that may make it easier to use cscope from within vim.

0

精彩评论

暂无评论...
验证码 换一张
取 消