4

One of the things that I love in Vim, was the faster way of insert double quotes (saves me hitting the shift key"

 inoremap '' ""<Left>

So when I press the single quote, and type further, I get the quote and the text, which I close with the closing quote, like 'foo bar' But when I double tap the quote, I get double quotes instead, then I get "foo bar". Evil doesn't support if the next character after the single quote is not included in the configuration. I tried various ways to figure out how I could get this feature.

Any suggestions how I could configure this feature in Elisp? (I'm using Evil mode by the way, if that helps).

3 Answers 3

5

Sounds like a task for the KeyChord library. Presumably something like this:

(key-chord-define-global "''" (lambda () (interactive) (insert "\"")))

or maybe:

(key-chord-define evil-insert-state-map "''" (lambda () (interactive) (insert "\"")))
2
  • Thanks for your answer! The keychord feature is interesting, however it's known to have some bugs, compared with Evil. I appreciate your answer, and it's working fine here.
    – ReneFroger
    Commented Apr 28, 2015 at 21:19
  • 1
    By the way, I made it as: (key-chord-define-global "''" (lambda () (interactive) (insert "\"\"") (backward-char 1))) and to escape from it: (key-chord-define evil-insert-state-map "jj" '(lambda () (interactive) (forward-char 1) (insert " ") ))
    – ReneFroger
    Commented Apr 28, 2015 at 21:28
5

You will want to bind the ' key to a function that performs different activities the first and the second time it is called. This is done by checking the variable last-command:

(defun insert-single-quote ()
  (interactive)
  (cond
    ((and (eq last-command 'insert-single-quote) (eql (char-before) ?'))
     (delete-backward-char 1) (insert "\""))
    (t (insert "'"))))

(global-set-key "'" #'insert-single-quote)
1
  • 1
    In the event a user uses this solution and wants to actually insert consecutive ' chars, they can do so by successive C-q '. Commented Apr 29, 2015 at 1:01
1

you may be interested in my new package to swap keys when entering text; it allows swapping quotes (avoiding the shift key): https://github.com/wbolster/evil-swap-keys

for automatic closing quotes, electric-pair-mode may be what you're looking for: it will insert the closing quote automatically, and you can type the quoting character yourself to step over the (already inserted) closing quote, which means you can avoid cursor keys completely and still have automatic matching pairs.

i know this is not an exact answer to your question, but it addresses the underlying problem in a (at least for me) very acceptable way.

1
  • Thanks for sharing, looks interesting! Will try out soon, I have your package already added it to emacs.zeef.com and /r/emacs on reddit, by the way.
    – ReneFroger
    Commented Aug 25, 2016 at 22:53

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.