Skip to content

Instantly share code, notes, and snippets.

@YumaInaura
Last active August 29, 2018 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YumaInaura/ed6963ad1b0898299628b1291e2e71f4 to your computer and use it in GitHub Desktop.
Save YumaInaura/ed6963ad1b0898299628b1291e2e71f4 to your computer and use it in GitHub Desktop.
Git — git grep and edit on Vim or default EDITOR ( use peco incremental search )

Git — git grep and edit with default EDITOR ( e.g Vim )( use peco incremental search )

Images

  • Do git grep include string "echo"
  • Select file line number 12 matched

image

  • Open file with vim line number 12

image

Points

Example

#!/usr/bin/env bash -eu

# Usage
#
# $ command [grep-keyword] --some-grep-option

readonly editor=${EDITOR:-vim}

if [ ! $(which peco) ]; then
  echo You need peco please install
  exit
fi

readonly git_grep_result=$(git grep --line-number $@)

if [ -z "$git_grep_result" ]; then
  echo No match with git grep
  exit
fi

readonly git_grep_select=$(echo "$git_grep_result" | peco --query="$1")

if [ -z "$git_grep_select" ]; then
  exit
fi

readonly filepath=$(echo "$git_grep_select" | awk -F':' '{ print $1 }')

readonly line_number=$(echo "$git_grep_select" | awk -F':' '{ print $2 }')

if [ ! -z "$filepath" ]; then
  if [[ "$editor" =~ vim ]] && [ ! -z "$line_number" ]; then
    eval "$editor" +"$line_number" "$filepath"
  else
    eval "$editor" "$filepath"
  fi
fi

gitim/git-grep-edit at acdbbf9bcbfd92e10d62be3f92bff5e6e5b0366e · YumaInaura/gitim

Versions

  • git version 2.8.1
  • peco version v0.5.3 (built with go1.10)

Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment