6

I would like the period at the end of a sentence to be added automatically when two spaces are typed (similar to how typing works in iPhones).

I use two spaces between sentences, so the transformation I would like to happen would look like this:

b l a h SPC SPC  -->  b l a h . SPC SPC

I plan to use this along with the auto-capitalize mode.

Many thanks!

10

Give this a go:

(defun freaky-space ()
  (interactive)
  (cond ((looking-back "\\(?:^\\|\\.\\)  +")
         (insert " "))
        ((eq this-command
             last-command)
         (backward-delete-char 1)
         (insert ".  "))
        (t
         (insert " "))))

(define-key text-mode-map " " 'freaky-space)
  • Great. One nice thing about this solution is that it takes into account the context (e.g., only insert a period if there's no period already there). – scaramouche Nov 28 '14 at 5:52
7

Keychords would let you do this. You would also want SPC SPC to be translated to . SPC only when you type them fast enough. That's exactly what keychords would do. After installing key-chord you can define something like this:

(key-chord-define-local (kbd "SPC SPC") (lambda () (insert ". ")))
  • I tried exactly that with key-chord. It makes it so that typing a space between words is very slow. I guess it's waiting to see if you'll type a second space. I turned it off. – incandescentman Jun 26 '15 at 7:32
  • 1
    @incandescentman Yes, you are right. Using a one-key keychord on a common key does have this issue. You could try tweaking key-chord-one-key-delay to your liking. – Pradhan Jun 26 '15 at 16:05
  • The fastest I could get it to recognize discrete key presses was (setq key-chord-one-key-delay 0.11), which still creates a long lag when typing a single space. – incandescentman Jul 9 '15 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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