Skip to content

Instantly share code, notes, and snippets.

@andreasjansson
Created January 12, 2015 18:01
Show Gist options
  • Select an option

  • Save andreasjansson/405128cf65effc1430f9 to your computer and use it in GitHub Desktop.

Select an option

Save andreasjansson/405128cf65effc1430f9 to your computer and use it in GitHub Desktop.
(require 'cl)
(defun new-shell ()
"Create new shells with consecutive names *shell-1*, *shell-2*, etc.
Remove any stale shells (shells without a live process)"
(interactive)
(let* ((regex "^\\*shell-\\([0-9]+\\)\\*$")
(shell-buffers (loop for buffer in (buffer-list)
if (string-match regex (buffer-name buffer))
collect buffer))
(max-buffer-num) (active-shell-buffers) (new-num))
(loop for buffer in shell-buffers
if (not (get-buffer-process buffer))
do (kill-buffer buffer))
(setq active-shell-buffers (loop for buffer in shell-buffers
if (buffer-live-p buffer)
collect buffer))
(setq max-buffer-num (loop for buffer in active-shell-buffers
for buffer-name = (buffer-name buffer)
for buffer-num-s = (progn
(string-match regex buffer-name)
(match-string 1 buffer-name))
for buffer-num = (string-to-number buffer-num-s)
maximize buffer-num))
(setq new-num (1+ (or max-buffer-num 0)))
(shell)
(rename-buffer (format "*shell-%d*" new-num))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment