Skip to content

Instantly share code, notes, and snippets.

@dov
Created October 25, 2023 13:08
Show Gist options
  • Save dov/a02254789ef1d73f05d916f7a2608753 to your computer and use it in GitHub Desktop.
Save dov/a02254789ef1d73f05d916f7a2608753 to your computer and use it in GitHub Desktop.
Toggle between backslashes and forward slashes
(defun toggle-backslash-line ()
"Toggle all forward slashes to backslashes for the current line."
(interactive)
(save-excursion
(if (use-region-p)
(setq myBoundaries (cons (region-beginning) (region-end)))
(setq myBoundaries (bounds-of-thing-at-point 'line)))
(save-restriction
(goto-char (car myBoundaries))
(narrow-to-region (car myBoundaries) (cdr myBoundaries))
(if (search-forward "/" nil t)
(progn
(goto-char (car myBoundaries))
(while (search-forward "/" nil t) (replace-match "\\\\" 'literal)))
(progn
(goto-char (car myBoundaries))
(while (search-forward "\\" nil t) (replace-match "/")))
)
))
(if (use-region-p)
(progn
(set-mark (car myBoundaries))
(goto-char (cdr myBoundaries)))))
(global-set-key [(control x) (control ?\\)] 'toggle-backslash-line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment