3 ms·
; Open a terminal M-x ansi-term ; Rename the buffer (escaping shell mode first) C-c C-j M-x rename-buffer ; Shell responds to keyboard again C-c C-k ; Spl
by merlincorey 14y ago
; Open a terminal
M-x ansi-term
; Rename the buffer (escaping shell mode first)
C-c C-j M-x rename-buffer
; Shell responds to keyboard again
C-c C-k
; Split window in half horizontally
C-x 2
-----
I stole this (possibly from Steve Yegge or another source) long ago to somewhat automate the process of renaming terminal buffers:
(global-set-key (kbd "<f2>") 'visit-ansi-term)
;; Terminal awesomeness
(require 'term)
(defun visit-ansi-term ()
"If the current buffer is:
1) a running ansi-term named *ansi-term*, rename it.
2) a stopped ansi-term, kill it and create a new one.
3) a non ansi-term, go to an already running ansi-term
or start a new one while killing a defunt one"
(interactive)
(let ((is-term (string= "term-mode" major-mode))
(is-running (term-check-proc (buffer-name)))
(term-cmd "/usr/local/bin/zsh")
(anon-term (get-buffer "*ansi-term*")))
(if is-term
(if is-running
(if (string= "*ansi-term*" (buffer-name))
(call-interactively 'rename-buffer)
(if anon-term
(switch-to-buffer "*ansi-term*")
(ansi-term term-cmd)))
(kill-buffer (buffer-name))
(ansi-term term-cmd))
(if anon-term
(if (term-check-proc "*ansi-term*")
(switch-to-buffer "*ansi-term*")
(kill-buffer "*ansi-term*")
(ansi-term term-cmd))
(ansi-term term-cmd)))))
- abecedarius 14y agoHey, I found that code hard to follow, with the if-then-elses nested 4 deep. A refactor, pretty much untested since I don't use ansi-term: (defun visit-ansi-term () "If the current buffer is: 1) a running ansi-term named *ansi-term*, rename it. 2) a stopped ansi-term, kill it and create a new one. 3) a non ansi-term, go to an already running ansi-term or start a new one while killing a defunct one." (interactive) (let ((is-term (string= "term-mode" major-mode)) (is-ansi-term (string= "*ansi-term*" (buffer-name))) (is-running (term-check-proc (buffer-name))) (anon-term (get-buffer "*ansi-term*"))) (cond ((and is-term is-running is-ansi-term) (call-interactively 'rename-buffer)) ((and anon-term (if is-term (and is-running (not is-ansi-term)) (term-check-proc "*ansi-term*"))) (switch-to-buffer "*ansi-term*")) (t (cond ((and is-term (not is-running)) (kill-buffer (buffer-name))) ((and anon-term (not (term-check-proc "*ansi-term*"))) (kill-buffer "*ansi-term*"))) (ansi-term "/usr/local/bin/zsh")))))