Skip to content

Instantly share code, notes, and snippets.

@dfeich
Created March 16, 2017 20:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dfeich/1bcb09b0e2fc5f55fecec8f498862683 to your computer and use it in GitHub Desktop.
Save dfeich/1bcb09b0e2fc5f55fecec8f498862683 to your computer and use it in GitHub Desktop.
Emacs function to open an ansi-term at a local or remote location
(defun dfeich/ansi-terminal (&optional path name)
"Opens an ansi terminal at PATH. If no PATH is given, it uses
the value of `default-directory'. PATH may be a tramp remote path.
The ansi-term buffer is named based on `name' "
(interactive)
(unless path (setq path default-directory))
(unless name (setq name "ansi-term"))
(ansi-term "/bin/bash" name)
(let ((path (replace-regexp-in-string "^file:" "" path))
(cd-str
"fn=%s; if test ! -d $fn; then fn=$(dirname $fn); fi; cd $fn;")
(bufname (concat "*" name "*" )))
(if (tramp-tramp-file-p path)
(let ((tstruct (tramp-dissect-file-name path)))
(cond
((equal (tramp-file-name-method tstruct) "ssh")
(process-send-string bufname (format
(concat "ssh -t %s '"
cd-str
"exec bash'; exec bash; clear\n")
(tramp-file-name-host tstruct)
(tramp-file-name-localname tstruct))))
(t (error "not implemented for method %s"
(tramp-file-name-method tstruct)))))
(process-send-string bufname (format (concat cd-str " exec bash;clear\n")
path)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment