0

I would like to bind M-S-up to C-u 7 S-up to move a timestamp a week forward (and down for back). A modification of this answer resulted in a "Symbol is void" error in Emacs.

I tried modifying the define-keys command, but was not successful, as Emacs then stated that "-up" could not follow after "M-S".

How can I configure a keybinding in my ~/.emacs file to execute C-u 7 S-up?

Update

I placed the following, modified from the answer linked above, in the scratch buffer and evaluated it:

(define-key my-map (kbd "C-u 7 S-up") (kbd "M-S-up"))

Emacs returned Symbol's value as variable is void: my-map.

4
  • 4
    You're guessing at the way to describe a key binding. Don't do that. Just like the answer you've linked to advises, ask Emacs with C-h k or C-h c and then use the syntax it shows you in your kbd arguments. – phils May 3 '16 at 13:31
  • C-h k followed by pressing Meta Shift Up returns <M-S-up> is undefined, as expected. – SabreWolfy May 3 '16 at 19:52
  • 3
    define-key expects an existing key map, and it looks like you haven't defined my-map anywhere. I suspect you want to use org-mode-map as you are trying to bind this key for org? You also have the (kbd .. ) arguments backwards for what you've described. Finally, you need the <> chars. Try this: (define-key org-mode-map (kbd "<M-S-up>") (kbd "C-u 7 <S-up>")) – glucas May 3 '16 at 20:31
  • (Unrelated, but I missed it earlier as I as not in Org-mode when I checked: <M-S-up> is already mapped in Org-mode, so it's best to choose a different binding.) – SabreWolfy May 4 '16 at 11:58
2

You can't set a key in a keymap that doesn't exist. In order to use define-key, you need to know the name of the keymap you're trying to modify. To add a keybinding to the global keymap, you can use global-set-key instead:

(global-set-key (kbd "M-S-<up>") (kbd "C-u 7 S-<up>"))

If you want to modify the keybinding only for org-mode, you can use

(eval-after-load "org-mode"
  '(define-key org-mode-map (kbd "M-S-<up>") (kbd "C-u 7 S-<up>")))

or

(defun my-org-hook ()
  (local-set-key (kbd "M-S-<up>") (kbd "C-u 7 S-<up>")))

(add-hook 'org-mode-hook 'my-org-hook)
2

The typical approach would be to bind the key to a command, rather than another key sequence. In your case you want to call org-shiftup with an argument. For example:

(define-key org-mode-map (kbd "<M-S-up>")
   (lambda () (interactive) (org-shiftup 7)))

Alternatively you can bind one key sequence to another as in the example you cited. This works for me:

(define-key org-mode-map (kbd "<M-S-up>") (kbd "C-u 7 <S-up>"))

As @Tyler pointed out, when defining keys for a specific mode you generally want to use a mode hook or eval-after-load so that the key is not defined until the mode's keymap has been defined.

A few general tips:

  • Use C-h k to find that S-up is bound to org-shiftup.
  • Use C-h f define-key to read the doc for define-key. It looks like you swapped the arguments and may not be familiar with keymaps.
  • As noted in the comments, use C-h k to see the appropriate key syntax -- when using up, you need the <>.
  • To really understand key maps and key bindings, refer to the Elisp manual. There is also a great overview on Mastering Emacs.
1
  • Thanks for the answer and all the additional information. – SabreWolfy May 3 '16 at 21:22

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.