Emacs: dired Rename File, Space to Hyphen or Lowline 📜

By Xah Lee. Date: . Last updated: .
(defun xah-dired-rename-space-to-lowline ()
  "In dired, rename current or marked files by replacing space to lowline _.
If not in `dired', do nothing.

2025-10-28 was-named-xah-dired-rename-space-to-underscore.

URL `http://xahlee.info/emacs/emacs/elisp_dired_rename_space_to_underscore.html'
Created: 2016-10-04
Version: 2025-10-28"
  (interactive)
  (require 'dired-aux)
  (if (eq major-mode 'dired-mode)
      (let ((xmarkedFiles (dired-get-marked-files)))
        (mapc (lambda (x)
                (let ((xfname (file-name-nondirectory x)))
                  (when (string-match " " xfname)
                    (dired-rename-file
                     x
                     (file-name-concat (file-name-directory x) (string-replace " " "_" xfname)) nil))))
              xmarkedFiles)
        ;; (dired-next-line 1)
        (dired-revert))
    (user-error "%s: Not in dired" real-this-command)))
(defun xah-dired-rename-space-to-hyphen ()
  "In dired, rename current or marked files by replacing space to hyphen -.
If not in `dired', do nothing.

URL `http://xahlee.info/emacs/emacs/elisp_dired_rename_space_to_underscore.html'
Created: 2016-10-04
Version: 2024-01-18"
  (interactive)
  (require 'dired-aux)
  (if (eq major-mode 'dired-mode)
      (progn
        (mapc (lambda (x)
                (when (string-match " " x )
                  (dired-rename-file x (string-replace " " "-" x) nil)))
              (dired-get-marked-files ))
        (dired-revert))
    (user-error "%s: Not in dired" real-this-command)))

Cycle Hyphen Lowline Characters