Showing the current directory in Emacs’ mode line

May 23, 2008 – 3:28 pm

Today I got tired of always looking up, where all these little files named “_show.rhtml”, “_list.rhtml” and their ilk are living, and patched the emacs mode line to include the last element of the current buffer’s directory. http://www.emacswiki.org/cgi-bin/wiki/ModeLineDirtrack describes something very similar, but it repeats the whole mode line definition of mode-line-format, which might break with the next version of Emacs. It’s much cleaner to add to a variable that is used in the mode line:

(setq-default mode-line-buffer-identification
              (cons
               '(:eval (replace-regexp-in-string "^.*/\\(.*\\)/" "\\1/" default-directory))
               mode-line-buffer-identification))
  1. 5 Responses to “Showing the current directory in Emacs’ mode line”

  2. But I am using vim. Please provide this patch for vim. :)

    By ccm on May 29, 2008

  3. You can always use viper-mode. 8-)

    By admin on May 29, 2008

  4. My combined solution to keep propertization and restrict directory name to file buffers:

    (defun find-file-mode-line ()
    (setq mode-line-buffer-identification
    ‘(:eval (propertized-buffer-identification
    (concat (replace-regexp-in-string “^.*/\\(.*\\)/” “\\1/” default-directory) “%12b”)))))
    (add-hook ‘find-file-hook ‘find-file-mode-line)

    By maf on Dec 24, 2008

  5. In the mean time I found an even better solution using the uniquify library.

    (require 'uniquify)
     
    (setq uniquify-buffer-name-style 'reverse)
    (setq uniquify-separator "|")
    (setq uniquify-after-kill-buffer-p t)
    (setq uniquify-ignore-buffers-re "^\\*")

    By admin on Mar 24, 2009

  6. Thanks for your ‘uniquify’ suggestion. This is the best improvement since I learned about ido-mode.

    By Axel Bregnsbo on May 29, 2010

Post a Comment