Skip to content

Instantly share code, notes, and snippets.

@Cloudef
Created July 25, 2012 21:09
Show Gist options
  • Select an option

  • Save Cloudef/3178694 to your computer and use it in GitHub Desktop.

Select an option

Save Cloudef/3178694 to your computer and use it in GitHub Desktop.
vimo
######################################
## Open file inside git tree on vim ##
######################################
vimo() {
local match=
local gtdir=
local cwd=$PWD
git ls-files &>/dev/null || return # test if git
gtdir="$(git rev-parse --show-toplevel)"
[[ "$cwd" != "$gtdir" ]] && pushd "$gtdir" &> /dev/null # git root
[[ -n "$@" ]] && { match="$(git ls-files | grep "$@")"; } ||
match="$(git ls-files)"
[[ $(echo "$match" | wc -l) -gt 1 ]] && match="$(echo $match | dmenu -i -l 20 -p "vim")"
match="$gtdir/$match" # convert to absolute
[[ "$cwd" != "$gtdir" ]] && popd &> /dev/null # go back
[[ -f "$match" ]] || return
vim "$match"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment