   (interactive)
   (erase-buffer)
   (comint-send-input))
+
+(defun my-shell-rename-buffer ()
+  (interactive)
+  (let ((new-name (read-string "New buffer name: ")))
+    (rename-buffer (format "*%s*" new-name))))
 
 (defvar my-shell-bash-esc-dot-counter 0)
 (defvar my-shell-bash-esc-dot-last-insertion nil)

  (recenter-top-bottom 0))

(defun my-shell (&optional arg)
  "Create a new shell (with prefix arg) or switch to a shell buffer."
  (interactive "P")
  (if arg
      (shell (generate-new-buffer-name "*shell*"))
    (let (shells others)
      (dolist (buf (buffer-list))
        (when (eq (with-current-buffer buf major-mode) 'shell-mode)
          (if (string-match "^[*]shell[*]" (buffer-name buf))
              (push buf shells)
            (push buf others))))
      ;; Sort the shells named "*shell*", "*shell*<1>" by their names.
      (setq shells (sort shells (lambda (x y)
                                  (string-lessp (buffer-name x)
                                                (buffer-name y)))))
      ;; Sort the shells not named "*shell*" etc. by their
      ;; creation time.
      (setq others (sort others (lambda (x y)
                                  (< (with-current-buffer x
                                       my-shell-mode-created-at)
                                     (with-current-buffer y
                                       my-shell-mode-created-at)))))
      (cond ((and (null shells) (null others))
             (shell))
            ((eq major-mode 'shell-mode)
             (let ((cur (current-buffer)))
               (switch-to-buffer (if (string-match "^[*]shell[*]" (buffer-name))
                                     (or (cadr (memq cur shells))
                                         (car others)
                                         (car shells))
                                   (or (cadr (memq cur others))
                                       (car shells)
                                       (car others))))))
            (t (switch-to-buffer (or (car shells)
                                     (car others))))))))

(defvar my-shell-mode-created-at nil)
(make-variable-buffer-local 'my-shell-mode-created-at)

(add-hook 'shell-mode-hook
          (lambda ()
            (setq my-shell-mode-created-at (float-time))
            (linum-mode -1)
            (setq line-number-mode nil
                  column-number-mode nil)

              'comint-next-prompt)
            (define-key shell-mode-map (kbd "C-c e")
              'my-shell-erase-buffer)
            (define-key shell-mode-map (kbd "C-c n")
              'my-shell-rename-buffer)
            (define-key shell-mode-map (kbd "C-l")
              'my-shell-bash-clear-screen)
            (define-key shell-mode-map (kbd "<right>")
