[ACCEPTED]-.emacs global-set-key and calling interactive function with argument-elisp

Accepted answer
Score: 48

Not minutes after asking the question I 2 figured it out by copy+pasting code. However 1 I have no clue how it works.

(global-set-key (kbd "M-<up>") (lambda () (interactive) (move-line -1)))
Score: 8

global-set-key only takes 2 arguments: the 6 key sequence and the command you want to 5 bind to it. So

(global-set-key (kbd "M-<down>") 'move-line)

works fine. But if you want 4 to use move-line with an argument you need 3 to wrap it in an anonymous (aka lamba) function 2 so that it presents itself to global-set-key 1 as one value.

Score: 3

You can simply ask for the number of lines 2 you want and convert the input string into 1 an integer:

(global-set-key (kbd "M-<up>") 
  (lambda () 
    (move-line (string-to-int (read-string "Lines: ")))))
Score: 2

You might want to check out the "transpose-lines" built-in 1 function.

Score: 2

I found this when I had the same problem, but 7 I ended up solving it in another way.

(global-set-key (kbd "M-<down>") 'move-line)

(global-set-key (kbd "M-<up>") (kbd "C-u -1 M-<down>"))

Definitely 6 not a perfect solution, since M-<down> could be 5 reassigned and C-u -1 might not make sense on 4 it, but since it's just my local init file, it 3 should be no problem.

Also this obvious 2 only works well for keyboard commands that 1 you want to have reversed.

More Related questions