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, hypen} 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 2016-12-18"
  (interactive)
  ;; this function sets a property 「'state」. Possible values are 0 to length of -charArray.
  (let (-p1 -p2)
    (if (and (not (null *begin)) (not (null *end)))
        (progn (setq -p1 *begin -p2 *end))
      (if (use-region-p)
          (setq -p1 (region-beginning) -p2 (region-end))
        (if (nth 3 (syntax-ppss))
            (save-excursion
              (skip-chars-backward "^\"")
              (setq -p1 (point))
              (skip-chars-forward "^\"")
              (setq -p2 (point)))
          (let (
                (-skipChars
                 (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))
           (-nowState
            (if (equal last-command this-command )
                (get 'xah-cycle-hyphen-underscore-space 'state)
              0 ))
           (-changeTo (elt -charArray -nowState)))
      (save-excursion
        (save-restriction
          (narrow-to-region -p1 -p2)
          (goto-char (point-min))
          (while
              (search-forward-regexp
               (elt -charArray (% (+ -nowState 2) -length))
               ;; (concat
               ;;  (elt -charArray (% (+ -nowState 1) -length))
               ;;  "\\|"
               ;;  (elt -charArray (% (+ -nowState 2) -length)))
               (point-max)
               "NOERROR")
            (replace-match -changeTo "FIXEDCASE" "LITERAL"))))
      (when (or (string= -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 2015-08-18"
  (interactive "r")
  (save-excursion
    (save-restriction
      (narrow-to-region begin end)
      (goto-char (point-min))
      (while
          (search-forward-regexp "_" (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 from Space to Hyphen/Underscore

Text Transform Topic

  1. Emacs: Toggle Letter Case
  2. Emacs: Change to Title Case
  3. Emacs: Upcase Sentences
  4. Emacs: Cycle Replace Space Hyphen Underscore
  5. Emacs: Remove Accent Marks
  6. Emacs: Escape Quotes Command
  7. Emacs: Quote Lines
Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from Best Keyboard for Emacs