Vim Tips Wiki
(tweak TipProposed template + minor manual clean)
(10 intermediate revisions by 7 users not shown)
Line 1: Line 1:
  +
{{TipNew
{{TipProposed
 
|id=0
+
|id=1668
|previous=0
+
|previous=1667
|next=0
+
|next=1669
 
|created=February 11, 2011
 
|created=February 11, 2011
 
|complexity=basic
 
|complexity=basic
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
By default, Cscope [http://cscope.sourceforge.net/cscope_maps.vim script] adds <tt>cscope.out</tt> from Vim's current directory and from <tt>$CSCOPE_DB</tt>. However, if you start Vim from say <tt>~/proj/src/a/b/c/</tt>, while <tt>cscope.out</tt> is at <tt>~/proj/src/</tt>, that <tt>cscope.out</tt> won't be loaded automatically.
+
By default, Cscope [http://cscope.sourceforge.net/cscope_maps.vim script] adds <code>cscope.out</code> from Vim's current directory and from <code>$CSCOPE_DB</code>. However, if you start Vim from say <code>~/proj/src/a/b/c/</code>, while <code>cscope.out</code> is at <code>~/proj/src/</code>, that <code>cscope.out</code> won't be loaded automatically.
   
For ctags, there is a nice trick: with the command {{tt|1=:set tags=tags;/}} Vim will look for tags file everywhere starting from the current directory up to the root.
+
For ctags, there is a nice trick: with the command <code>:set tags=tags;/</code> Vim will look for tags file everywhere starting from the current directory up to the root.
   
 
This tip provides the same "autoloading" functionality for Cscope. Just add the following to your [[vimrc]]:
 
This tip provides the same "autoloading" functionality for Cscope. Just add the following to your [[vimrc]]:
Line 21: Line 21:
 
if (!empty(db))
 
if (!empty(db))
 
let path = strpart(db, 0, match(db, "/cscope.out$"))
 
let path = strpart(db, 0, match(db, "/cscope.out$"))
set nocsverbose " suppress 'duplicate connection' error
+
set nocscopeverbose " suppress 'duplicate connection' error
 
exe "cs add " . db . " " . path
 
exe "cs add " . db . " " . path
set csverbose
+
set cscopeverbose
 
endif
 
endif
 
endfunction
 
endfunction
Line 33: Line 33:
   
 
==Comments==
 
==Comments==
  +
If somebody use gVim in the windows(like me use Winxp), a modified script for the _vimrc script maybe helpful as follow:
  +
<pre>
  +
function LoadCscope()
  +
if (executable("cscope") && has("cscope"))
  +
let UpperPath = findfile("cscope.out", ".;")
  +
if (!empty(UpperPath))
  +
let path = strpart(UpperPath, 0, match(UpperPath, "cscope.out$") - 1)
  +
if (!empty(path))
  +
let s:CurrentDir = getcwd()
  +
let direct = strpart(s:CurrentDir, 0, 2)
  +
let s:FullPath = direct . path
  +
let s:AFullPath = globpath(s:FullPath, "cscope.out")
  +
let s:CscopeAddString = "cs add " . s:AFullPath . " " . s:FullPath
  +
execute s:CscopeAddString
  +
endif
  +
endif
  +
endif
  +
endfunction
  +
command LoadCscope call LoadCscope()
  +
  +
</pre>
  +
  +
The last statement "Command LoadCscope call LoadCscope()" means that we can use command :LoadCscope to call the function LoadCscope for convinence.

Revision as of 02:29, 15 July 2013

Tip 1668 Printable Monobook Previous Next

created February 11, 2011 · complexity basic · author Dorserg · version 7.0


By default, Cscope script adds cscope.out from Vim's current directory and from $CSCOPE_DB. However, if you start Vim from say ~/proj/src/a/b/c/, while cscope.out is at ~/proj/src/, that cscope.out won't be loaded automatically.

For ctags, there is a nice trick: with the command :set tags=tags;/ Vim will look for tags file everywhere starting from the current directory up to the root.

This tip provides the same "autoloading" functionality for Cscope. Just add the following to your vimrc:

function! LoadCscope()
  let db = findfile("cscope.out", ".;")
  if (!empty(db))
    let path = strpart(db, 0, match(db, "/cscope.out$"))
    set nocscopeverbose " suppress 'duplicate connection' error
    exe "cs add " . db . " " . path
    set cscopeverbose
  endif
endfunction
au BufEnter /* call LoadCscope()

See also

Comments

If somebody use gVim in the windows(like me use Winxp), a modified script for the _vimrc script maybe helpful as follow:

function LoadCscope()
	if (executable("cscope") && has("cscope"))
		let UpperPath = findfile("cscope.out", ".;")
		if (!empty(UpperPath))
			let path = strpart(UpperPath, 0, match(UpperPath, "cscope.out$") - 1)	
			if (!empty(path))
				let s:CurrentDir = getcwd()
				let direct = strpart(s:CurrentDir, 0, 2) 
				let s:FullPath = direct . path
				let s:AFullPath = globpath(s:FullPath, "cscope.out")
				let s:CscopeAddString = "cs add " . s:AFullPath . " " . s:FullPath 
				execute s:CscopeAddString 
			endif
		endif
	endif
endfunction
command LoadCscope call LoadCscope()

The last statement "Command LoadCscope call LoadCscope()" means that we can use command :LoadCscope to call the function LoadCscope for convinence.