2

I know how to convert a string to upper case, to lower case, or to title case.

But the operation I do most frequently writing English text is to fix up capitalization so a sentence or group of sentences is in title case. That is, the first letter is capitalized and usually nothing else is, like the sentence you’re reading right now.

Is there an Emacs command or package to reformat a sentence in sentence case?

2 Answers 2

5

(I assume you meant text in a buffer, and not a string (as the original question said).

M-e M-c (press and hold Meta while hitting e, then c.

The M-e moves forward-sentence. The M-c does capitalize-word.

See the Emacs manual, nodes:

You can of course write a simple command that does that, and bind that to a single key. But is it worth it?

(defun sentence-case (&optional n)
  "..."
  (interactive "p")
  (dotimes (ii n)
    (forward-sentence)
    (capitalize-word 1)))

(You can first make it go back to the beginning of the current sentence, with backward-sentence, if you want. And you can make other changes, e.g. to act on the sentences in the region. And you can reverse the order, to first capitalize then move to the sentence end. It all depends on what you really want.)

1
  • Thanks! If I understand right, this will capitalize the first word of the next sentence from point. Correct? But in that case, I still would need to lowercase all the remaining words in that sentence in order to recapitalize the entire sentence correctly. I was hoping there might already by a function which recapitalized all of the current sentence.
    – algal
    Commented Feb 6, 2020 at 3:10
1

This is a rough start but seems to work:

(defun recapitalize-sentence ()
  "Recapitalizes current sentence as a sentence.                                                                                   

Relies on the function sentence-end to find the end of a                                                                           
sentence, which by default relies on two spaces before starting                                                                    
the next sentence."
  (interactive)
  (save-excursion
    (let ((next-sentence-start (progn (forward-sentence) (point))))
      (backward-sentence)
      (capitalize-word 1)
      (while (< (point) next-sentence-start)
        (downcase-word 1)))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.