JohnBeckett (talk | contribs) (Format comment.) |
|||
Line 64: | Line 64: | ||
I've added a [[Using_ZSH_completion_with_ctags_and_Vim | similar tip for ZSH]]. |
I've added a [[Using_ZSH_completion_with_ctags_and_Vim | similar tip for ZSH]]. |
||
+ | ---- |
||
− | |||
− | ----------------- |
||
− | |||
I was very excited to see this, but I was very annoyed that it expected a tags file in the current directory. I keep a tags file at the root of each project, and vim -t is smart enough to find the correct tags file, so I wanted bash completion to be just as smart. |
I was very excited to see this, but I was very annoyed that it expected a tags file in the current directory. I keep a tags file at the root of each project, and vim -t is smart enough to find the correct tags file, so I wanted bash completion to be just as smart. |
||
− | So, I modified the file a bit. It will now look for a tags file in the current directory, and then keep looking in parent directories until it finds one. |
+ | So, I modified the file a bit. It will now look for a tags file in the current directory, and then keep looking in parent directories until it finds one. (note: changed `pwd` to $PWD) |
− | |||
− | (note: changed `pwd` to $PWD) |
||
<pre> |
<pre> |
||
Line 108: | Line 104: | ||
complete -F _vim_ctags -f -X "${excludelist}" vi vim gvim rvim view rview rgvim rgview gview |
complete -F _vim_ctags -f -X "${excludelist}" vi vim gvim rvim view rview rgvim rgview gview |
||
</pre> |
</pre> |
||
− | |||
---- |
---- |
||
− | |||
Thanks for posting this. |
Thanks for posting this. |
||
Here is how I modified the completion function with tags-file search for my own .bashrc. I basically rewrote most of it, partly just for stylistic preference, but also to make some improvements: |
Here is how I modified the completion function with tags-file search for my own .bashrc. I basically rewrote most of it, partly just for stylistic preference, but also to make some improvements: |
||
⚫ | |||
⚫ | |||
+ | <pre> |
||
⚫ | |||
− | |||
⚫ | |||
− | |||
− | <code> |
||
_vim_ctags() { |
_vim_ctags() { |
||
local cur prev |
local cur prev |
||
Line 137: | Line 129: | ||
COMPREPLY=( $(grep -o "^$cur[^ ]*" "$tagsdir/tags" ) ) |
COMPREPLY=( $(grep -o "^$cur[^ ]*" "$tagsdir/tags" ) ) |
||
} |
} |
||
− | </ |
+ | </pre> |
I haven't done extensive testing on it, but it works for me so far. |
I haven't done extensive testing on it, but it works for me so far. |
||
− | Note that there is an invisible tab character inside the grep pattern. |
+ | Note that there is an invisible tab character inside the grep pattern. --February 22, 2013 |
Revision as of 23:38, 23 February 2013
created 2008 · complexity basic · author Seanhodges · version 7.0
Add the following to your ~/.bash_completion
file (create it if it does not exist):
_vim_ctags() { local cur prev COMPREPLY=() cur="${COMP_WORDS[COMP_CWORD]}" prev="${COMP_WORDS[COMP_CWORD-1]}" case "${prev}" in -t) # Avoid the complaint message when no tags file exists if [ ! -r ./tags ] then return fi # Escape slashes to avoid confusing awk cur=${cur////\\/} COMPREPLY=( $(compgen -W "`awk -v ORS=" " "/^${cur}/ { print \\$1 }" tags`" ) ) ;; *) # Perform usual completion mode ;; esac } # Files matching this pattern are excluded excludelist='*.@(o|O|so|SO|so.!(conf)|SO.!(CONF)|a|A|rpm|RPM|deb|DEB|gif|GIF|jp?(e)g|JP?(E)G|mp3|MP3|mp?(e)g|MP?(E)G|avi|AVI|asf|ASF|ogg|OGG|class|CLASS)' complete -F _vim_ctags -f -X "${excludelist}" vi vim gvim rvim view rview rgvim rgview gview
Once you restart your bash session (or create a new one) you can type:
~$ vim -t MyC<tab key>
and it will auto-complete the tag the same way it does for files and directories:
MyClass MyClassFactory ~$ vim -t MyC
I find this really useful when I'm jumping into a quick bug fix.
Comments
I've added a similar tip for ZSH.
I was very excited to see this, but I was very annoyed that it expected a tags file in the current directory. I keep a tags file at the root of each project, and vim -t is smart enough to find the correct tags file, so I wanted bash completion to be just as smart.
So, I modified the file a bit. It will now look for a tags file in the current directory, and then keep looking in parent directories until it finds one. (note: changed `pwd` to $PWD)
_vim_ctags() { local cur prev COMPREPLY=() cur="${COMP_WORDS[COMP_CWORD]}" prev="${COMP_WORDS[COMP_CWORD-1]}" case "${prev}" in -t) while [ "${PWD}" != "/" ]; do if [ -r ./tags ]; then # Escape slashes to avoid confusing awk cur=${cur////\\/} COMPREPLY=( $(compgen -W "`awk -v ORS=" " "/^${cur}/ { print \\$1 }" tags`" ) ) return fi cd .. done return ;; *) # Perform usual completion mode ;; esac } # Files matching this pattern are excluded excludelist='*.@(o|O|so|SO|so.!(conf)|SO.!(CONF)|a|A|rpm|RPM|deb|DEB|gif|GIF|jp?(e)g|JP?(E)G|mp3|MP3|mp?(e)g|MP?(E)G|avi|AVI|asf|ASF|ogg|OGG|class|CLASS)' complete -F _vim_ctags -f -X "${excludelist}" vi vim gvim rvim view rview rgvim rgview gview
Thanks for posting this.
Here is how I modified the completion function with tags-file search for my own .bashrc. I basically rewrote most of it, partly just for stylistic preference, but also to make some improvements:
- the upward directory search doesn't change the current directory, which is typically not a good idea in a shell function
- it uses grep instead of awk. grep is most likely faster, and the invocation is simpler
_vim_ctags() { local cur prev cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} [[ $prev = -t ]] || return local tagsdir=$PWD while [[ "$tagsdir" && ! -f "$tagsdir/tags" ]]; do tagsdir=${tagsdir%/*} done [[ -f "$tagsdir/tags" ]] || return COMPREPLY=( $(grep -o "^$cur[^ ]*" "$tagsdir/tags" ) ) }
I haven't done extensive testing on it, but it works for me so far.
Note that there is an invisible tab character inside the grep pattern. --February 22, 2013