How to capitalize english string in Emacs

How to capitalize english string in Emacs

Maybe if you temporarily add to the current word constituent syntax table:

(modify-syntax-entry ? w)

(capitalize cant)
=> Cant

I have M-c bound to capitalize-word, and it works to capitalize cant correctly.

How to capitalize english string in Emacs

Current answers are fine, but if you work with strings in code, you can use s string manipulation library. s-capitalize capitalizes the first word in a sentence.

ELISP> (s-capitalize cant win the war on drugs in a prison, where the hell you gonna win it?)
Cant win the war on drugs in a prison, where the hell you gonna win it?
ELISP> (s-join   (-map s-capitalize (s-split   cant win the war on drugs in a prison, where the hell you gonna win it?)))
Cant Win The War On Drugs In A Prison, Where The Hell You Gonna Win It?

s-titleize capitalizes every word in a string, but its a simple wrapper around built-in capitalize, therefore Karl Voigtlands workaround applies.

ELISP> (s-titleize Girl, you cant even think of calling this shit a war.)
Girl, You CanT Even Think Of Calling This Shit A War.
ELISP> (progn (modify-syntax-entry ? w) (s-titleize Girl, you cant even think of calling this shit a war.))
Girl, You Cant Even Think Of Calling This Shit A War.

Leave a Reply

Your email address will not be published.