VIM 7: How to effectively manage the source code tree from the root of the code base?

I have a multi-file navigation system in a huge code base, and I want to improve / fix the flaw that it currently has:

My shell is preconfigured to open at the root of my code base - allows you to call it Dev /. During synchronization / code creation, I have a script that automatically saves the relative path for all .h and .c files in a single file that cscope will use (lets call it cscope.files).

As soon as I synchronize, 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` 

If I give part of the file name long enough to uniquely identify it, I will immediately open it in vim.

Now the disadvantage of this approach is that when I have already opened one file and moved to another file without exiting vim, the only way to do this is

 :!vif *file2* 

This spawns a new shell and then opens the file in vim running there. As a result, I cannot switch between the two files (using Ctrl- ^). I can not find a solution that:

a) Allows me to open any file from Dev / instantly

b) Allows me to open any other file inside vim (as soon as I opened an existing file) in the same shell so that 2 vim sessions know about each other (I can jump between 2 using Ctrl- ^)

I know this is a long question (as one from Google it :)), but I'm sure the solution is simple and obvious for someone more experienced in vim !!

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

UPDATE: I eventually went to the cscope path after setting up with a shortcut (since using gf on cscope.files still prevented me from switching between the two source files). See VIM 7 and cscope: Using "cscope find f" inside the keyboard display to switch between files for the shortcut.

+4
source share
4 answers

Use vim grep something like this:

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

For example, in this case:

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

you select (visual mode) the template you want to find, and then press F1 . The first file matching the pattern will open, replacing the current buffer * .

* If possible. if the current buffer is saved or set to hidden , etc.

If you want to receive an invitation, use input() :

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

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

Update

... for example, for example:

 function! GetPat() call inputsave() let mypat = input("Enter pattern: ") call inputrestore() return mypat endfunction nnoremap <F1> :exe ':1vim /'.GetPat().'/ cscope.files'<CR>gf 
+3
source

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

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

Additionnaly, it also works for other Vim resources such as buffers, tags, etc.

+3
source

I myself do not use cscope, but it looks like you can use it to search for files, see :help cscope-find .

Otherwise, this may help (e.g. not verified):

 "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>) 
+1
source

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 facilitate the use of cscope from within vim.

+1
source

Source: https://habr.com/ru/post/1344764/


All Articles