Skip to content
Manage ansi-term buffers using projectile
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
Cask
Makefile
README.org
term-manager-indexed-mapping.el
term-manager.el
term-projectile.el

README.org

term-manager http://melpa.org/packages/term-manager-badge.svg https://stable.melpa.org/packages/term-manager-badge.svg

term-projectile http://melpa.org/packages/term-projectile-badge.svg https://stable.melpa.org/packages/term-projectile-badge.svg

About

term-projectile manages terminals in groups based on the current projectile context. With a single keystroke, it will either create a new terminal for the current project, or take you to one that already exists. If more than one terminal exists for your project, it will let you easily cycle through those existing terminals. term-manager is used to support term-projectile, though it can theoretically be used without term-projectile.

Installation

Install from MELPA with M-x package-install term-projectile. See the melpa repository for details about how to set up MELPA if you have not already done so.

Setup

Emacs Configuration

The author (config here) prefers to set up term-projectile using the following (now quite complicated) hydra:

(defun imalison:maybe-symbol-name (arg)
  (if (symbolp arg)
      (symbol-name arg)
    arg))

(defun imalison:concat-symbols (&rest args)
  (intern (mapconcat 'imalison:maybe-symbol-name args "")))

(defvar imalison:term-hydra-original-default-directory)

(defhydra imalison:term-hydra-default-directory
  (:body-pre
   (term-projectile-default-directory-forward-restored))
  "term - default-directory"
  ("f" term-projectile-default-directory-forward-restored)
  ("b" term-projectile-default-directory-backward-restored)
  ("c" term-projectile-default-directory-create-new-restored)
  ("d" term-projectile-default-directory-forward-restored)
  ("g" imalison:term-hydra-global/body-restored :exit t)
  ("p" imalison:term-hydra-projectile/body-restored :exit t))

(defhydra imalison:term-hydra-projectile
  (:body-pre
   (progn
     (term-projectile-forward-restored)))
  "term - projectile"
  ("f" term-projectile-forward-restored)
  ("b" term-projectile-backward-restored)
  ("c" term-projectile-create-new-restored)
  ("d" imalison:term-hydra-default-directory/body-restored :exit t)
  ("g" imalison:term-hydra-global/body-restored :exit t)
  ("p" term-projectile-forward-restored))


(defhydra imalison:term-hydra-global
  (:body-pre
   (progn (setq imalison:term-hydra-original-default-directory
                default-directory)
          (term-projectile-global-forward-restored)))
  "term - global"
  ("f" term-projectile-global-forward-restored)
  ("b" term-projectile-global-backward-restored)
  ("c" term-projectile-global-create-new-restored)
  ("d" imalison:term-hydra-default-directory/body-restored :exit t)
  ("g" term-projectile-global-forward-restored)
  ("p" imalison:term-hydra-projectile/body-restored :exit t))

(mapcar (lambda (term-projectile-function)
          (defalias (imalison:concat-symbols term-projectile-function '-restored)
            (lambda (&rest args)
              (interactive)
              (let ((default-directory imalison:term-hydra-original-default-directory))
                (apply term-projectile-function args)))))
        '(term-projectile-default-directory-forward
          term-projectile-default-directory-backward
          term-projectile-default-directory-create-new
          term-projectile-forward
          term-projectile-backward
          term-projectile-create-new
          term-projectile-global-forward
          term-projectile-global-backward
          term-projectile-global-create-new
          imalison:term-hydra-global/body
          imalison:term-hydra-projectile/body
          imalison:term-hydra-default-directory/body))

When term-projectile-forward is executed in a projectile project where no terminal currently exists, a new ansi-term instance will be created. If one already exists, pressing that key binding will take the user to the existing ansi-term instance for that project.

Terminal Support

To get automatic directory changing You will need to add something like the following to your shell’s configuration file, along with the snippet from one of the headings below that corresponds to your shell of choice.

function environment_variable_exists {
    eval "value=\"\${$1+x}\""
    [ ! -z $value ]
}

function emacs_ansi_term_support {
    echo -e "\033AnSiTu" "$LOGNAME" # $LOGNAME is more portable than using whoami.
    echo -e "\033AnSiTc" "$(pwd)"
    if [ $(uname) = "SunOS" ]; then
        # The -f option does something else on SunOS and is not needed anyway.
        hostname_options="";
    else
        hostname_options="-f";
    fi
    # XXX/TODO: This disables correct setting of the current directory
    # when in an sshed shell when inside of emacs
    # echo -e "\033AnSiTh" "$(hostname $hostname_options)" # Using the -f option can #
    # cause problems on some OSes.
}

zsh

if environment_variable_exists INSIDE_EMACS; then
    if [[ $INSIDE_EMACS == *"term"* ]]
    then
        add-zsh-hook precmd emacs_ansi_term_support
    fi
fi

bash

if environment_variable_exists INSIDE_EMACS; then
    if [[ $INSIDE_EMACS == *"term"* ]]
    then
        PROMPT_COMMAND='emacs_ansi_term_support'
    fi
fi
You can’t perform that action at this time.