Emacs: Cycle Replace Space Hyphen Underscore

By Xah Lee. Date: . Last updated: .

Here's a command that cycles {hypen, underscore, space} for word under cursor. If there's text selection, work on the region.

(defun xah-cycle-hyphen-underscore-space ( &optional @begin @end )
  "Cycle {underscore, space, hyphen} chars in selection or inside quote/bracket or line.
When called repeatedly, this command cycles the {“_”, “-”, “ ”} characters, in that order.

The region to work on is by this order:
 ① if there's active region (text selection), use that.
 ② If cursor is string quote or any type of bracket, and is within current line, work on that region.
 ③ else, work on current line.

URL `http://ergoemacs.org/emacs/elisp_change_space-hyphen_underscore.html'
Version 2019-02-12"
  ;; this function sets a property 「'state」. Possible values are 0 to length of $charArray.
  (let ($p1 $p2)
    (if (and @begin @end)
        (progn (setq $p1 @begin $p2 @end))
      (if (use-region-p)
          (setq $p1 (region-beginning) $p2 (region-end))
        (if (nth 3 (syntax-ppss))
              (skip-chars-backward "^\"")
              (setq $p1 (point))
              (skip-chars-forward "^\"")
              (setq $p2 (point)))
          (let (
                 (if (boundp 'xah-brackets)
                     (concat "^\"" xah-brackets)
            (skip-chars-backward $skipChars (line-beginning-position))
            (setq $p1 (point))
            (skip-chars-forward $skipChars (line-end-position))
            (setq $p2 (point))
            (set-mark $p1)))))
    (let* (
           ($charArray ["_" "-" " "])
           ($length (length $charArray))
           ($regionWasActive-p (region-active-p))
            (if (eq last-command this-command)
                (get 'xah-cycle-hyphen-underscore-space 'state)
              0 ))
           ($changeTo (elt $charArray $nowState)))
          (narrow-to-region $p1 $p2)
          (goto-char (point-min))
               (elt $charArray (% (+ $nowState 2) $length))
               ;; (concat
               ;;  (elt $charArray (% (+ $nowState 1) $length))
               ;;  "\\|"
               ;;  (elt $charArray (% (+ $nowState 2) $length)))
            (replace-match $changeTo "FIXEDCASE" "LITERAL"))))
      (when (or (string-equal $changeTo " ") $regionWasActive-p)
        (goto-char $p2)
        (set-mark $p1)
        (setq deactivate-mark nil))
      (put 'xah-cycle-hyphen-underscore-space 'state (% (+ $nowState 1) $length)))))

Give it a key. For example, Ctrl+-. [see Emacs: How to Define Keys]

If you just want it to toggle between _ and -, just shorten the vector in the code.

Hard Coded Underscore to Space

Here's a dumb command that just replaces underscore to space, in region.

(defun xah-underscore-to-space-region (@begin @end)
  "Change underscore char to space.
URL `http://ergoemacs.org/emacs/elisp_change_space-hyphen_underscore.html'
Version 2017-01-11"
  (interactive "r")
      (narrow-to-region @begin @end)
      (goto-char (point-min))
          (re-search-forward "_" (point-max) "NOERROR")
        (replace-match " " "FIXEDCASE" "LITERAL")))))

If you want a dumb command that replaces hyphen to space, hyphen to underscore, or underscore to hyphen, just modify the above code.

Dired Rename File from Space to Hyphen or Underscore

Emacs: Rename File, Space to Hyphen/Lowline

If you have a question, put $5 at patreon and message me on xah discord.
Or support me by Buy Xah Emacs Tutorial

Emacs Tutorial

Emacs Init

Emacs Keys


ELisp Examples

ELisp Write Major Mode

ELisp Examples

Xah Commands

Text Transform Under Cursor

Commands Do thing-at-point

Command to Insert Things

Script Examples