For a bunch of reasons, I recently decided to stop using Vim emulation in Emacs, and switch to a different modal keybinding system. But that’s a topic for another post.
One of the things that Vim does pretty well is bind case change to the tilde key (“~”). Briefly, if the point is on an upper-case character, tilde will make it lower case. If the point is on a lower-case character, tilde will make it upper-case. After a case change, the point advances one character.
I got used to this behavior, and wanted something similar in base Emacs. It’s easy enough to code it up in Emacs lisp, and bind the resulting defun to a key. Here is the elisp to emulate Vim’s ~ in Emacs.:
(defun jth-change-case ()
"Function to emulate Vim's ~ key in Emacs."
(interactive)
(let ((char-at-point (char-after)))
(delete-char 1)
(cond
((equal (upcase char-at-point) char-at-point)
(insert (downcase char-at-point)))
((equal (downcase char-at-point) char-at-point)
(insert (upcase char-at-point)))
(t nil))))
If the point is not on an upper or lower-case character, nothing happens other than the point moving forward by one char.