reddit / emacs
Capitalize word just typed
BobKoss · 2022-01-25 18:48
I mappe the caps lock key on my keyboard to control. Is there a function that will capitalize the word I just typed? I currently hold down the shift key with one hand and use one finger on the other hand to type a word.
crawsphi · 2022-01-25 18:54
`M-b` for `backward-word` and then `M-c` for `capitalize-word` (which moves forward again) might be one option.
Or `C-- 1 M-c` (that's a negative numeric arg, hold Control, hit `-`, then `1`) depending on which you find easier to type.
vonfuckingneumann · 2022-01-25 19:03
Or M-u for upcase-word, to capitalize all letters of the word. I think that is what OP is looking for - otherwise there'd be no reason to mention caps lock, it's not better than shift to capitalize the beginning of the word.
crawsphi · 2022-01-25 19:06
Good thinking!
vonfuckingneumann · 2022-01-25 19:08
While we're at it there are some more advanced options that can do cool things and might save OP the M-b. Just realized this. https://github.com/akicho8/string-inflection
vonfuckingneumann · 2022-01-25 19:10
The other answer is good, but here's a possibly faster one: use [string-inflection](https://github.com/akicho8/string-inflection). That way you can do it for the word you just typed without going back via M-b. I have `string-inflection-all-cycle` bound to C-c i. Advantage is that you can get "ALLCAPS" or "Capitalized" by just repeating `string-inflection-all-cycle`. (I actually do what the docs suggest and customize it according to major mode, but that's a detail.)
With that, ALLCAPS is just: type word -> C-c i. If I actually wanted just the first letter capitalized or something, just C-c i again.
ieure · 2022-01-25 20:21
You don't have to supply the `1` argument, just `M-- M-u`.
BayesMind · 2022-01-25 22:25
`M-c` capitalizes first letter of next word
`M-u` does all caps
`M-l` does all lowercase, if you want it
thetemp_ · 2022-01-26 00:50
I've replaced the default bindings with their built-in "do what I mean" versions.
(define-key global-map ?\M-c 'capitalize-dwim)
(define-key global-map ?\M-u 'upcase-dwim)
(define-key global-map ?\M-l 'downcase-dwim)
Works like the defaults, except that if region is active, it'll work on the region instead of the next word.
Makes it easy to upcase a line after selecting it. Or you can do something like `C-M-- C-M-SPC C-M-SPC C-M-SPC M-u` to capitalize just the last 3 words*, for example.
*-technically, the last 3 sexps.
crawsphi · 2022-01-26 01:34
Oh, good point, thanks!
karthink · 2022-01-26 06:47
> Or you can do something like C-M-- C-M-SPC C-M-SPC C-M-SPC M-u to capitalize just the last 3 words*, for example.
`M-- M-3 M-u`?
thetemp_ · 2022-01-26 07:42
Nice. I get satisfaction from slap-bassing the space bar, but it's just personal preference.
EDIT: It'd be nice if we could just do "M-- M-u M-u M-u" until we've upcased the number of words we want. Seems a bit odd that repeating M-u doesn't preserve the negative argument, while repeating C-M-SPC does.
oantolin · 2022-01-26 08:02
You don't need the 1 in the second method: `C-- M-c` works fine too. Even better you can use `M-- M-c` without any awkward modifier switching.
BobKoss · 2022-01-26 09:22
My question wasn't precise enough. I want the entire word capitalized, like for e.g., a #define in C/C++.
BobKoss · 2022-01-26 09:25
M-u only seems to capitalize the character I'm on, or it jumps to a new headline in org-mode. Perhaps this doesn't work for me because I'm using doom emacs? I thought this was a general enough topic to ask in the emacs forum. My apologies.
BobKoss · 2022-01-26 09:28
That looks handy. Thank you.
BobKoss · 2022-01-26 09:31
I like the default so one binding works for me, and I just want the case for the entire word to be capitalized. The other choices I'd just do as I was typing. It's the constantly holding the shift key that was driving me crazy.
Thank you.
xenodium · 2022-01-26 11:52
Forever ago I adopted Oleh’s `capitalize-word-toggle` from http://oremacs.com/2014/12/25/ode-to-toggle bound to `C-c c`. Well-internalised in muscle memory by now.
ps. Toggle functions in general are awesome (fewer bindings to remember)
BobKoss · 2022-01-26 12:35
Sweet and to the point.
_viz_ · 2022-01-27 02:58
M-x where-is RET upcase-word RET. But I think doom will bind upcase-dwim instead: M-x where-is RET upcase-dwim RET.