Some Random Thoughts..
Just a place to put some random thoughts..

I have been using for some years now and it's a great tool that increases my productivity. It helps me take my mind off stupid things like rpm switches, hostname etc. Just press tab and it will present you with a list of relevent choices. Great.

Another thing that I often do is "grep". I am known for that. I am comfortable with it and many times use it instead of ctags. One thing annoys me (or used to annoy me) is I have to remember C variable names to do a grep on it. I wanted to write a tab completion, using programmable bash completion feature, that searches into tag files and complete variable names in grep.

For example, in the current directory, you have a .c file with "int mynameisrumplestiltskin;". Now you want to grep through all the files for "mynameisrumplestiltskin". You should be able to do that with "grep myn[TAB]" and it should complete.

Today I found some time to do this. Turned out to be very easy. Just put following code in you bashrc file. Go ahead give it a try.


_grep()
{
local cur exp file
COMPREPLY=()

exp=${COMP_WORDS[1]}
file=${COMP_WORDS[2]}
if [ $COMP_CWORD -eq 1 ]; then
COMPREPLY=( $(ls *.c *.h 2>/dev/null | ctags --filter=yes | awk '{print $1}' | grep ^$exp) )
fi
if [ $COMP_CWORD -gt 1 ]; then
COMPREPLY=( $( compgen -f -- "$file" ) $( compgen -d -- "$file" ) )
fi
}
complete -F _grep grep

Did it work?


Productivity
I have been using for some years now and it's a great tool that increases my productivity. It helps me take my mind off stupid things like rpm switches, hostname etc. Just press tab and it will present you with a list of relevent choices. Great.

Another thing that I often do is "grep". I am known for that. I am comfortable with it and many times use it instead of ctags. One thing annoys me (or used to annoy me) is I have to remember C variable names to do a grep on it. I wanted to write a tab completion, using programmable bash completion feature, that searches into tag files and complete variable names in grep.

For example, in the current directory, you have a .c file with "int mynameisrumplestiltskin;". Now you want to grep through all the files for "mynameisrumplestiltskin". You should be able to do that with "grep myn[TAB]" and it should complete.

Today I found some time to do this. Turned out to be very easy. Just put following code in you bashrc file. Go ahead give it a try.


_grep()
{
local cur exp file
COMPREPLY=()

exp=${COMP_WORDS[1]}
file=${COMP_WORDS[2]}
if [ $COMP_CWORD -eq 1 ]; then
COMPREPLY=( $(ls *.c *.h 2>/dev/null | ctags --filter=yes | awk '{print $1}' | grep ^$exp) )
fi
if [ $COMP_CWORD -gt 1 ]; then
COMPREPLY=( $( compgen -f -- "$file" ) $( compgen -d -- "$file" ) )
fi
}
complete -F _grep grep

Did it work?



posted by rumplestiltskin @ 4:18 pm 0 comments

0 Comments:


Post a Comment