When writing prose (or comments or documentation in markdown), it is often useful to toggle between lower case ("word") and Capital Case ("Word"). While you could lower case a word with guiw
and Capital Case it with bgUl
, those really aren't convenient keychords to type. And even if remapped, it's two keymap you have to remember, even though it is really only one action (toggling between lower and Capital case).
This is one of the rare cases where the ~
command to toggle the case of the character under the cursor comes in handy: paired with b
, the keystroke combination b~
does the job. But by making a few addition, we can further improve this:
- We add in a
l
motion to ensure the binding still works when on the first letter of a word, since~b
would otherwise affect to the previous word. - Wrapping the combination in
mz
/`z
keeps the cursor position (and also allows repeatability, since using~b
twice would move to the previous word). - Next we also add in
lgue
, which ensures that all but the first character of the word are lowercased. This allows us to fix typos like "WOrd" as well.
When we then create a mapping for the final macro, mzlblgueh~`z
, we have one keybinding that:
- lowercases a word that is Capitalcased (or UPPERCASED),
- Capitalcases a word if it is lowercased,
- and simultaneously also fixes some typos.
1vim.keymap.set("n", "<leader>u", "mzlblgueh~`z")
For changing between variable casing conventions, e.g. to turn a SNAKE_CASE variable into camelCase, there is the fantastic vim-absolish.