I’ve been using a bash function for launching vim for quite a while. I can do things like vij edit.jsp and it would find the edit.jsp and launch vim. The original version looked like this:

vij ()
{
find . -name "$1" -exec vim {} \;
}

The problem would occur when there were more than one file named edit.jsp. Running find . -name edit.jsp yielded four files:

find . -name edit.jsp
./code/webapp/WEB-INF/pages/activationkeys/edit.jsp
./code/webapp/WEB-INF/pages/errata/edit.jsp
./code/webapp/WEB-INF/pages/channel/manage/edit.jsp
./code/webapp/WEB-INF/pages/systems/probes/edit.jsp

vij would exec vim for each of the files found. This is not what I wanted, I only wanted to edit the third one. So with a little hacking I updated it to prompt me if it found more than one, so I could choose the one I wanted.

vij edit.jsp

Multiple matches found...
1: ./code/webapp/WEB-INF/pages/activationkeys/edit.jsp
2: ./code/webapp/WEB-INF/pages/errata/edit.jsp
3: ./code/webapp/WEB-INF/pages/kickstart/wizard/profile/advanced/edit.jsp
4: ./code/webapp/WEB-INF/pages/channel/manage/edit.jsp
5: ./code/webapp/WEB-INF/pages/systems/probes/edit.jsp
q: Quit

? 

Simply add this to your $HOME/.bashrc and you’ll be all set.

vij ()
{
    dafiles=$(find . -type f -name "$1")
    matches=$(echo $dafiles | gawk '{print NF}')

    case "$matches" in
        0)
           echo "No matches found"
           show=""
           ;;
        1)
           show=$dafiles
           ;;
        *)
           echo
           echo "Multiple matches found..."
           i=1
           for option in $dafiles
           do
              echo "$i: $option"
              i=`expr $i + 1`
           done
           echo "q: Quit"
           echo
           read -p "? " ans
           if [ "q" == "$ans" ]; then
              show=""
           else
              show=$(echo $dafiles | gawk '{print $"'"$ans"'"}')
           fi
           ;;
    esac

    if [ "" != "$show" ]; then
       vim $show
    fi
}

5 thoughts on “vij

  1. How about that:


    vij()
    {
    vim `find . -name "$1" -print`
    }

    This will open all the files and you can use :n and :prev to navigate forward and backwards.

  2. This is awesome. I’ve had the exact same script as your original version for years (except for emacs and I called it “femacs”) but I never looked into fixing the multiple file issue. I’d just watch as they all popped up. I have to give this a try.

  3. When I initially commented I seem to have clicked on the -Notify me when new comments are added- checkbox and now every
    time a comment is added I receive 4 emails with the same
    comment. Is there a way you are able to remove me from that service?
    Kudos!

  4. Wonderful goods from you, man. I have bear in mind your stuff prior to and you are
    simply too fantastic. I actually like what you have bought here, certainly like what you’re stating and the way in which during which you are saying it.
    You make it enjoyable and you still take care of to keep it smart.

    I can’t wait to learn far more from you. That is really
    a terrific website.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s