Function: rename-uniquely

rename-uniquely is an interactive and byte-compiled function defined in files.el.gz.

Signature

(rename-uniquely)

Documentation

Rename current buffer to a similar name not already taken.

This function is useful for creating multiple shell process buffers or multiple mail buffers, etc.

Note that some commands, in particular those based on compilation-mode
(compile, grep, etc.) will reuse the current buffer if it has the
appropriate mode even if it has been renamed. So as well as renaming the buffer, you also need to switch buffers before running another instance of such commands.

View in manual

Probably introduced at or before Emacs version 19.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/files.el.gz
(defun rename-uniquely ()
  "Rename current buffer to a similar name not already taken.
This function is useful for creating multiple shell process buffers
or multiple mail buffers, etc.

Note that some commands, in particular those based on `compilation-mode'
\(`compile', `grep', etc.) will reuse the current buffer if it has the
appropriate mode even if it has been renamed.  So as well as renaming
the buffer, you also need to switch buffers before running another
instance of such commands."
  (interactive)
  (save-match-data
    (let ((base-name (buffer-name)))
      (and (string-match "<[0-9]+>\\'" base-name)
	   (not (and buffer-file-name
		     (string= base-name
			      (file-name-nondirectory buffer-file-name))))
	   ;; If the existing buffer name has a <NNN>,
	   ;; which isn't part of the file name (if any),
	   ;; then get rid of that.
	   (setq base-name (substring base-name 0 (match-beginning 0))))
      (rename-buffer (generate-new-buffer-name base-name))
      (force-mode-line-update))))