I want to map C-f C-b as moving forward and backward by a fixed amount of lines in a file.
I did this:
(global-set-key (kbd "C-f") 'next-line)
(global-set-key (kbd "C-b") 'previous-line)
but I don't know how to specify an argument before the next-line command. I guess I should use digit-argument but I am unable to write the command in a correct way.
You've changed your question to be about how to bind directly to key sequences
This binds C-c l to C-u 5 C-n
(global-set-key (kbd "C-c l") (kbd "C-u 5 C-n"))
One of the possible alternatives would be define a new function:
(defun my-next-line ()
(interactive)
(next-line 5))
(global-set-key (kbd "C-f") 'my-next-line)
Otherwise, if it is just something you can accomplish with the keyboard you might want to use
M-x name-last-kbd-macro
and save it in your .emacs file
M-x insert-kbd-macro
and have emacs implement the function for you.
It will just get the name you gave in your call to name-last-kbd-macro
Related
I'm trying to bind indent-to-column 100 to C-TAB (or even just indent-to-column so I could then enter the column number I want), but what I tried isn't working:
This gives me an error when I open emacs:
(global-set-key (kbd "<C-tab>") 'indent-to-column 100)
This seems to have no effect:
(global-set-key (kbd "<C-tab>") 'indent-to-column)
What am I doing wrong?
Your second expression should work (in that it should bind indent-to-column to C-TAB, but you still have to provide the argument). You can check this using the following key sequence to see what C-TAB is bound to:
C-hkC-TAB
If you want to provide the argument as well, you can use this kind of construct:
(global-set-key (kbd "<C-tab>")
(lambda ()
(interactive)
(indent-to-column 100)))
Try this:
(global-set-key [(control tab)] 'indent-to-column)
I want to bind C-x-insert to a command. This works:
(global-set-key [\C-insert] 'my-func)
But this doesn't:
(global-set-key [\C-x-insert] 'my-func)
C-hcC-xinsert tells me
C-x <insert> is undefined
Which tells me how Emacs refers to that sequence, which in turn means that I can pass the string "C-x <insert>" into the kbd function, and it will Just Work.
(global-set-key (kbd "C-x <insert>") 'my-func)
kbd is your friend.
This seems to work:
(define-key ctl-x-map [insert] 'beginning-of-line)
I'm trying to create a combination keybinding.
Here's an example:
(define-key my-minor-mode-map (kbd "x f") "\C-x\C-f")
(edit: Thanks you Stefan for pointing out the space between \C-x and \C-f.)
This however takes me to a random file and describe-key says this:
Macro: C-x C-f
Keyboard macro.
So I'm not really sure what that means.
It seems that trying to bind s to C-s doesn't work either (As well as other interactive commands like C-r and M-x).
This does work:
(define-key my-minor-mode-map (kbd "x f") "\M-f")
So basically I want to be able to run C-x C-f (find-file) without having to type 'find-file as a function itself.
In other words; I don't want this:
(define-key my-minor-mode-map (kbd "x f") 'find-file)
I hope someone could help me out with this. My emacs knowledge is very limited.
Thanks in advance.
Complete code:
(defvar my-minor-mode-map (make-keymap) "my-minor-mode keymap")
(define-key my-minor-mode-map (kbd "x f") "\C-x\C-f")
(define-minor-mode my-minor-mode
"My minor-mode"
t "My minor mode" 'my-minor-mode-map)
(defun my-minibuffer-setup-hook ()
(my-minor-mode 0))
"My minor-mode"
Edit:
What would even be better is if I could do this:
(define-key my-minor-mode-map (kbd "x") "\C-x")
(define-key my-minor-mode-map (kbd "f") "\C-f")
And then if I would type "x f" that it would exectue "\C-x C-f" aka find file.
That way I wouldn't have to write out every possible key combination.
"\C-x \C-f" has 3 elements: C-x, SPC, and C-f. You probably did not mean for that space to be there.
I'm not entirely certain what you believe should be happening here, but I suspect what you actually want is:
(define-key my-minor-mode-map (kbd "x f") (key-binding (kbd "C-x C-f")))
which is the same thing as the code you said you didn't want to use:
(define-key my-minor-mode-map (kbd "x f") 'find-file)
except that it obtains the function dynamically, based on the key binding.
p.s. It's also slightly odd that you're using a mixture of kbd and non-kbd syntax in the same form.
I want to call a function from some elisp code as if I had called it interactively with a prefix argument. Specifically, I want to call grep with a prefix.
The closest I've gotten to making it work is using execute-extended-command, but that still requires that I type in the command I want to call with a prefix...
;; calls command with a prefix, but I have to type the command to be called...
(global-set-key (kbd "C-c m g")
(lambda () (interactive)
(execute-extended-command t)))
The documentation says that execute-extended-command uses command-execute to execute the command read from the minibuffer, but I haven't been able to make it work:
;; doesn't call with prefix...
(global-set-key (kbd "C-c m g")
(lambda () (interactive)
(command-execute 'grep t [t] t)))
Is there any way to call a function with a prefix yet non-interactively?
If I'm understanding you right, you're trying to make a keybinding that will act like you typed C-u M-x grep <ENTER>. Try this:
(global-set-key (kbd "C-c m g")
(lambda () (interactive)
(setq current-prefix-arg '(4)) ; C-u
(call-interactively 'grep)))
Although I would probably make a named function for this:
(defun grep-with-prefix-arg ()
(interactive)
(setq current-prefix-arg '(4)) ; C-u
(call-interactively 'grep))
(global-set-key (kbd "C-c m g") 'grep-with-prefix-arg)
Or you could just use a keyboard macro
(global-set-key (kbd "s-l") (kbd "C-u C-SPC"))
In this example, the key combination "s-l" (s ("super") is the "windows logo" key on a PC keyboard) will go up the mark ring, just like you if typed "C-u C-SPC".
I'm trying to put in a feature that I miss from Eclipse, where Alt+[Up/Down] transposes the lines up or down, but can not for the life of me figure out how to assign to these keys properly. I am using it in -nw mode (so just in a shell window), and typically run in a screen session.
Using a global key binding, I can get it to work with letter combinations, like (kbd "M-m"), but every combination I have tried for the arrow keys just gives me a message that doesn't make sense, I always get:
"ESC <up> is undefined"
What I have tried:
(global-set-key (kbd "M-<up>") 'transpose-line-up)
(global-set-key (kbd "<escape>-<up>") 'transpose-line-up)
(global-set-key [M-up] 'transpose-line-up)
(global-set-key [\e \M-O A] 'transpose-line-up)
And C-h c just returns:
ESC <up> (translated from ESC M-O A) is undefined
None of these work, either using ESC or Alt.
Any idea how I can make this work? I would prefer to have these as Alt+[Up/Down] just because that is what I am used to.
Edit
From the comments:
C-q Up prints ^[OA.
C-q M-Up prints ^[ and moves the cursor up a line.
C-h k (Alt+Up) prints ESC <up> (translated from ESC M-O A) is undefined.
Thanks for the suggestions, but they all turned out the same.
Emacs has a complex mechanism to handle the vicissitudes of function key and modifier encodings on various terminal types. It doesn't work out of the box in all cases. The following settings should work on your terminal:
(define-key input-decode-map "\e\eOA" [(meta up)])
(define-key input-decode-map "\e\eOB" [(meta down)])
(global-set-key [(meta up)] 'transpose-line-up)
(global-set-key [(meta down)] 'transpose-line-down)
You should be able to use (kbd "<M-up>") and (kbd "<M-down>") in place of [(meta up)] and [(meta down)], as long as you've done the step of telling Emacs (via input-decode-map) about the escape sequences that your terminal uses to encode these key combinations.
I always use C-h k (key) (i.e. describe-key) to find out how Emacs refers to (key), and then use (kbd) with that same string to utilise it.
In this case, describe-key returns <M-up>, so I would use (global-set-key (kbd "<M-up>") 'transpose-line-up) (exactly as J.F. Sebastian has done).
Edit:
Running emacs -nw (but not through screen), describe-key reports ESC <up> (translated from ESC M-[ A), and (kbd "ESC <up>") is successful for binding it.
Running screen emacs -nw, describe-key reports ESC <up> (translated from ESC M-O A), which seems to match what you see, and the binding for (kbd "ESC <up>") still works for me.
(n.b. Tested under Cygwin with screen 4.00.03, and Emacs 23.2.1.)
(global-set-key [M-up] 'beginning-of-buffer)
(global-set-key [M-down] 'end-of-buffer)
In my OSX, I have this definition to perform Alt-up/down to jump to top/bottom of buffer.
ugly workaround:
I've typed C-q <M-up> it produced ^[[1;3A on the terminal inside screen inside emacs.
(global-set-key (kbd "<M-up>") 'transpose-line-up)
(global-set-key (kbd "^[[1;3A") 'transpose-line-up)
I've got Lisp error: (void-function transpose-line-up) so the key bindings work.
Note: C-q runs the command quoted-insert.
The following lines work for me on macOS 10.11.6 and GNU Emacs 25.2.1:
(global-set-key (kbd "ESC <down>") 'end-of-buffer)
(global-set-key (kbd "ESC <up>") 'beginning-of-buffer)
Assuming you have the functions transpose-line-up and transpose-line-down already defined (as it seems to be from the example code in your original question):
(global-set-key [(meta up)] 'transpose-line-up)
(global-set-key [(meta down)] 'transpose-line-down)
works on OSX Terminal:
(global-set-key (kbd "ESC <up>") 'transpose-line-up)
(global-set-key (kbd "ESC <down>") 'transpose-line-down)