How can i bind C-x-insert in emacs - emacs

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)

Related

How to unbind "M-1" and "M-2" in Emacs ansi-term

I use tabbar in emacs, and bind following key.
(global-set-key (kbd "M-2") 'tabbar-forward-tab)
(global-set-key (kbd "M-1") 'tabbar-backward-tab)
But, those key-binds don't work in ansi-term mode. When I type 'M-1', it do not run tabbar-backward-tab, the key is captured by bash.
[xx#local ~]$
(arg: 1)
How to unbind "M-1" and "M-2" in Emacs ansi-term?
In term-char-mode M-<n> sequences are bound to term-send-raw (as are most sequences which a terminal would normally handle).
To unbind them, you can use:
(eval-after-load "term"
'(progn
(define-key term-raw-map (kbd "M-1") nil)
(define-key term-raw-map (kbd "M-2") nil)))
That will stop them from shadowing the global bindings.

bind 'indent-to-column' to C-tab

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)

Defining key binding with arguments

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

How to bind 'search' and 'search-repeat' to C-f in Emacs?

How can I to remap incremental search (C-s) to C-f in Emacs?
I try to do (global-set-key (kbd "C-f") 'isearch-forward) but the second C-f does not repeat the search and I need to use C-s.
I then tried (global-set-key (kbd "C-f") 'isearch-repeat-forward) but the first C-f didn't start the search.
And I even tried (global-set-key (kbd "C-f C-f") 'isearch-repeat-forward), but this causes an error.
I want to use C-f for search and search-repeat commands, how can I do this?
Thanks.
(define-key isearch-mode-map "\C-f" 'isearch-repeat-forward)
isearch-repeat-forward is defined in the isearch-mode-map
To resolve your problem do the following :
(global-set-key (kbd "C-f") 'isearch-forward)
(add-hook 'isearch-mode-hook
(lambda ()
(define-key isearch-mode-map (kbd "C-f") 'isearch-repeat-forward)
)
)
EDIT: actually, you don't need to add a hook. The accepted answer by Ross Patterson is correct.

Binding M-<up> / M-<down> in Emacs 23.1.1

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)