Skip to content

Instantly share code, notes, and snippets.

@sebasmonia
Forked from dov/toggle-backslash.el
Last active November 7, 2023 16:25
Show Gist options
  • Save sebasmonia/d0d0286dbf7537cc6a849d6604ccdec6 to your computer and use it in GitHub Desktop.
Save sebasmonia/d0d0286dbf7537cc6a849d6604ccdec6 to your computer and use it in GitHub Desktop.
Toggle between backslashes and forward slashes
(defun toggle-backslash-dwim (&optional start end)
"Toggle slashes-backslashes for the active region.
If there's no active region START and END, it operates on the
current line."
(interactive "r")
(save-excursion
(unless (use-region-p)
(cl-destructuring-bind (a . b)
(bounds-of-thing-at-point 'line)
(setf start a
end b)))
(with-restriction start end
(goto-char start)
(if (save-excursion (search-forward "/" nil t))
(while (search-forward "/" nil t) (replace-match "\\\\" 'literal))
(while (search-forward "\\" nil t) (replace-match "/"))))))
(global-set-key (kbd "C-c C-/") #'toggle-backslash-dwim)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment