StevieD
StevieD

Reputation: 7433

How to edit file with vim without typing path (similar to autojump)?

A few months back, I installed a utility on my mac so that instead of typing something like this:

vim /type/path/to/the/file

I could just type:

v file

9 times out of 10 it would guess the right file based on the past history, similar to the way autojump works. And instead of typing in vim I can just type the letter v.

I can't remember how I set this up though. It still works on my mac but I don't see anything in my .bash_profile that shows how I did that.

I'm trying to get this to work on my linux box.

Upvotes: 0

Views: 135

Answers (2)

Chris Hill
Chris Hill

Reputation: 235

This can be found here

https://github.com/rupa/v/blob/master/v

it should work in Linux too. It is a bash script that uses the viminfo history file to fill in partial strings.

It can be installed on macOS with brew install v

Upvotes: 2

StevieD
StevieD

Reputation: 7433

Ah! I found the command with which. Here is the magical script. I can't determine where I got it.

 #!/usr/bin/env bash

 [ "$vim" ] || vim=vim
 [ $viminfo ] || viminfo=~/.viminfo

 usage="$(basename $0) [-a] [-l] [-[0-9]] [--debug] [--help] [regexes]"

 [ $1 ] || list=1

 fnd=()
 for x; do case $x in
     -a) deleted=1;;
     -l) list=1;;
     -[1-9]) edit=${x:1}; shift;;
     --help) echo $usage; exit;;
     --debug) vim=echo;;
     --) shift; fnd+=("$@"); break;;
     *) fnd+=("$x");;
 esac; shift; done
 set -- "${fnd[@]}"

 [ -f "$1" ] && {
     $vim "$1"
     exit
 }

 while IFS=" " read line; do
     [ "${line:0:1}" = ">" ] || continue
     fl=${line:2}
     [ -f "${fl/\~/$HOME/}" -o "$deleted" ] || continue
     match=1
     for x; do
         [[ "$fl" =~ $x ]] || match=
     done
     [ "$match" ] || continue
     i=$((i+1))
     files[$i]="$fl"
 done < "$viminfo"

 if [ "$edit" ]; then
     resp=${files[$edit]}
 elif [ "$i" = 1 -o "$list" = "" ]; then
     resp=${files[1]}
 elif [ "$i" ]; then
     while [ $i -gt 0 ]; do
          echo -e "$i\t${files[$i]}"
          i=$((i-1))
     done
     read -p '> ' CHOICE
     resp=${files[$CHOICE]}
 fi

 [ "$resp" ] || exit
 $vim "${resp/\~/$HOME}"

Upvotes: 0

Related Questions