I wanted to have a git openlast command which would look at the last commit, get a list of added or changed files, filter them to only the text files, and finally open them in an editor.

So far I've done the following:

git show --stat HEAD
read -p "Open in Vim tabs? (Y/n)" -n 1 -r
if [[ -z $REPLY || $REPLY =~ [Yy] ]]; then
  vim -p $(git diff --diff-filter=AM --ignore-submodules --name-only HEAD^)

The down fall is if I add or change a binary file in the previous commit then it will be opened by the editor (Vim in this case). Is there a way to take the list outputted by the git diff command and remove binary files?

2 Answers 2


You can pipe to xargs and use grep -Il "" to filter out binary files:

git diff --diff-filter=AM --ignore-submodules --name-only HEAD^ | \
  xargs grep -Il ""

Example git openfiles command

git show --stat HEAD
files=($(git diff --diff-filter=AM --ignore-submodules --name-only HEAD^ | xargs grep -Il ""))
read -p "Open ${#files[@]} files in Vim tabs? (Y/n)" -n 1 -r
if [[ -z $REPLY || $REPLY =~ [Yy] ]]; then
  exec vim -p ${files[@]}
  exit 1

If have file extensions, can use those with Gawk to choose what you want in the list. Example has '.txt' and '.jpeg' extensions. Regular expression is used to edit output from git.

git show --stat HEAD
commit ec07d8306e9e61894d18e6f8d6ea1e5d650c0712
Author: root <root@somebox>
Date:   Thu Dec 17 17:19:30 2015 -0500

test project commit

add email addresses.jpeg           | Bin 0 -> 127688 bytes
allpermissions.jpeg                | Bin 0 -> 126620 bytes
error.jpeg                         | Bin 0 -> 227469 bytes
sonic_boom.jpeg                    | Bin 0 -> 112958 bytes
test1.jpeg                         | Bin 0 -> 96857 bytes
test1.txt                          |   1 +
test2.txt                          |   1 +
top_cipher_test_results.jpeg       | Bin 0 -> 149293 bytes

Gawk removal of binary .jpeg extension files.

git show --stat HEAD|awk '{ if($1 ~ /.jpeg/)print $1}'

Inclusion of both the .txt and the .jpeg if desired:

git show --stat HEAD|awk '{ if($1 ~ /.jpeg|.txt/)print $1}'
  • Filtering by file extension is useful. Thank you.
    – Sukima
    Dec 17, 2015 at 22:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .