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

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)

Related

emacs: assign single key-binding to multiple commands, using the universal argument

Here is my attempt:
(global-set-key [M-left] (key-binding (kbd "C-u C-#")))
After I evaluate the above expression, invoking alt + left gives me the message <M-left> is undefined. The following, however, works:
(global-set-key [M-left] (key-binding (kbd "C-u")))
But this is only the universal argument part of my command. How do I combine these two commands into one Emacs key-binding?
There are two ways to do this: define a Keyboard Macro interactively or write a function:
(define-key global-map [M-left]
(lambda ()
(interactive)
(set-mark-command t)))
sds has provided the solutions, but for clarification, if you evaluate (key-binding (kbd "C-u C-#")) you'll see that it returns nil -- because that is not a bound key sequence.
In fact C-u runs the command universal-argument, which takes care of reading a subsequent key sequence from the user (C-# in your case).

How can i bind C-x-insert in 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)

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

unable to set keymap for key Ctrl-;

I am trying to set the global key for the C-; key by placing the below code in init.el file.
(global-set-key "\C-;" 'backward-kill-word)
But this gives the following error in the *Warning* buffer
error "Invalid modifier in string"
try this,
(global-set-key [(control ?\;)] 'backward-kill-word)
another way and I guess most easiest way of doing is with kbd function
(global-set-key (kbd "C-;") 'backward-kill-word)

How to override/change mode key bindings in elisp?

In particular, when I load dired-x, it sets M-o to toggle the omit minor mode. I use M-o for other-window, so I would like to change the key that dired-x binds to something else. I've attempted setting the key after the mode loads like this:
(add-hook 'dired-mode-hook
(lambda ()
(dired-omit-mode 1)
(global-set-key (kbd "M-o") 'other-window)
))
but to no avail.
Slightly better than adding another copy of your custom global binding to the local mode map, would be removing the local binding so that it no longer shadows the global binding. You might also give that function a new key before you do this.
(eval-after-load "dired-x"
'(progn
;; Add an alternative local binding for the command
;; bound to M-o
(define-key dired-mode-map (kbd "C-c o")
(lookup-key dired-mode-map (kbd "M-o")))
;; Unbind M-o from the local keymap
(define-key dired-mode-map (kbd "M-o") nil)))
The dired-mode bindings "shadow" the global ones so your "global-set-key" isn't helping. What you want to do is override the dired-mode binding:
(add-hook 'dired-mode-hook
(lambda ()
(dired-omit-mode 1)
(define-key dired-mode-map (kbd "M-o") 'other-window)
))