Launch Magit from the command line

Last update:

I frequently want to open a Magit buffer on the current git repo when navigation directories from the command line.

With the following script saved in your local bin directory, type magit to open a new Emacs buffer with Magit status on the git repo of your current working directory. It creates a new Emacs frame if in graphical mode, or displays the buffer in the terminal otherwise. Very convenient.

Requires Emacs to run as server.

#!/bin/bash

set -euo pipefail

if ! emacsclient -a false --eval nil --quiet >/dev/null 2>&1; then
    echo "Error: Emacs not running." >&2
    exit 1
fi

IS_GRAPHICAL="$(emacsclient -a false --eval "(display-graphic-p)" --quiet)"
ARGS=

if [ "$IS_GRAPHICAL" = "t" ]; then
    ARGS="-n"
fi

exec emacsclient $ARGS -c --eval "(progn (magit-status) (delete-other-windows))"
Code Snippet 1: ~/usr/bin/magit

See all posts in the archive.

Comments

Comments were disabled in March 2022. Since this page was created earlier, there may have been previous comments which are now inaccessible. Sorry.