Skip to content

Instantly share code, notes, and snippets.

@rafmagana
Last active December 10, 2015 00:19
Show Gist options
  • Save rafmagana/4350600 to your computer and use it in GitHub Desktop.
Save rafmagana/4350600 to your computer and use it in GitHub Desktop.
Script to open modified, cached/staged or untracked files in a git repo with macvim.
#!/usr/bin/env bash
function git_edit_files
{
# Init
local type=$1
# Commands
local ls_files='git ls-files'
local diff='git diff --cached --name-only'
# Messages
local usage='Usage: git-edit-files [-m|--modified|-u|--untracked|-c|--cached]'
local were_not="There weren't any"
local files_to_edit='files to edit...'
local untracked_not_found="$were_not untracked $files_to_edit"
local modified_not_found="$were_not modified $files_to_edit"
local cached_not_found="$were_not cached/staged $files_to_edit"
case $type in
-m|--modified)
local files=$($ls_files --modified)
git_edit_files_open "$files" "$modified_not_found"
;;
-u|--untracked)
local files=$($ls_files . --exclude-standard --others)
git_edit_files_open "$files" "$untracked_not_found"
;;
-c|--cached)
local files=$($diff)
git_edit_files_open "$files" "$cached_not_found"
;;
-h|--help)
echo $usage
;;
*)
$FUNCNAME -m
;;
esac
}
function git_edit_files_open()
{
local files=$1
local message=$2
if [ -n "$files" ]; then
mvim -o $files -c 'NERDTree' > /dev/null 2>&1
else
echo $message
fi
}

Why?

I use MacVim, I use mksession, so I know I can create a my_session.vim file and later I can load the session with :load my_session.vim, but I don't like to save/load sessions, I just want to open my modified files in git repos.

Making it work

Make it executable

chmod +x /path/to/git-edit.sh

Add it to your ~/.bashrc

echo ". /path/to/git-edit.sh" >> ~/.bashrc

Add an alias to your ~/.gitconfig

[alias]
  edit = !bash -ic 'git_edit_files \"$@\"' -

or to path/to/your/project/.git/config

cd path/to/your/project
git config alias.edit '!bash -ic '\''git_edit_files "$@"'\'' -'

and last but not least, and just to make it even easier, let's add some alias to your ~/.bashrc or ~/.bash_profile:

alias gedm='git edit --modified'
alias gedc='git edit --cached'
alias gedu='git edit --untracked'

Usage

Every file will be opened in its own MacVim window and split horizontally, and NERDTree will be opened too, so once the script and aliases are all setup:

$ git edit     # open modified files
$ git edit -m  # open modified files
$ git edit -c  # open cached/staged files, those you have "git add'ed"
$ git edit -u  # open untracked files

with bash aliases:

$ gedm  # open modified files
$ gedc  # open cached/staged files, those you have "git add'ed"
$ gedu  # open untracked files

Customizing the script

The script doesn't gives you too much flexibility on the options we pass to MacVim, so you'll have to customize the script if you want to open the files in its own tab or with vertical splitting, you can do it in git-edit.sh#46:

mvim -o $files -c 'NERDTree' > /dev/null 2>&1

To open in tabs:

mvim -p ...

To open in windows but split vertically:

mvim -O ...

If you don't want NERDTree, get rid of it by removing:

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