4

I use strings like "include/lib-one", "include/lib-two", "src/lib-three" all the time.

Is there an easy way to set shortcuts for these strings, e.g. as C-c 1, C-c 2 and so on?

1
  • There's abbrev or yasnippet. But auto-completion for your specific language would be better.
    – abo-abo
    Dec 20, 2014 at 10:09

2 Answers 2

8

You can also use define-key or global-set-key like this:

(global-set-key (kbd "C-c a") "include/lib-one")

I've used C-c a instead of C-c 1 because the former is a reserved-for-end-users sequence, whereas the latter is reserved for major modes. See C-hig(elisp) Key Binding Conventions RET

1
  • Also of note: this method executes the keyboard macro defined by the given string. So you can have things like "\C-j" in there, and it would be as if you hit C-j.
    – Malabarba
    Jan 18, 2015 at 11:57
3

You can use the insert function to insert strings at point. You can define keybinding to an invocation of this function. For example,

(define-key your-mode-map (kbd "C-c 1") (lambda () (insert "include/lib-one")))
3
  • thanks! One more question: what is your-mode-map?
    – Beginner
    Dec 20, 2014 at 10:17
  • It is the name of the mode where you want this keybinding. For example, if you want this in c++-mode only, you will replace your-mode-map by c++-mode-map. Alternatively, if you want it to be defined globally, use global-set-key.
    – Pradhan
    Dec 20, 2014 at 10:19
  • I've tried adding similar keybindings to my .emacs file, but they are never taken into account when I launch emacs. Is there something that I'm missing?
    – Dai
    Oct 19, 2017 at 14:54

Your Answer

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

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