emacs: turning electric-indent-mode on/off easily - emacs

I'm a bit of an elisp newbie. How can I tell emacs to toggle electric-indent-mode when I press "C-c e" ?
I tried
(global-set-key (kbd "C-c e") 'toggle-electric-indent-mode)
but that does't seem to do it for me.

The mode is just a function that you can call. Try:
(global-set-key (kbd "C-c e") 'electric-indent-mode)

Related

How to disable local key binding in octave-mode?

Here is my key binding.
I want to set C-h to delete-backward-char in all modes, but in octave mode, when I pressed C-h, it shows C-h (Type ? for further options)-. So I add the last 3 lines in my .emacs file, but it doesn't work. C-h still works as the help function.
(global-set-key (kbd "C-?") 'help-command)
(global-set-key (kbd "C-h") 'delete-backward-char)
(define-key octave-mode-map (kbd "C-h") nil)
(define-key octave-mode-map (kbd "C-h a") nil)
(define-key octave-mode-map (kbd "C-h d") nil)
octave.el appears to set those bindings in three different keymaps:
octave-mode-map
inferior-octave-mode-map
octave-help-mode-map
You might also refer to Globally override key binding in Emacs

How to customize Emacs keyboard shortcut for deleting current line

I want the current line to be deleted when Alt, and the letter d is pressed twice.
How can I achieve this inside my Emacs configuration file?
Currently all I have is this in my .emacs:
(global-set-key (kbd "M-9") 'prev-window)
(global-set-key (kbd "M-0") 'other-window)
You can use your own keymap:
(defvar somename-map (make-sparse-keymap) "Keymap for M-d")
(define-key somename-map (kbd "M-d") 'kill-line)
(define-key somename-map (kbd "M-w") 'kill-word)
(global-set-key (kbd "M-d") somename-map)

Disable Mail in emacs 23

I am wondering if its possible to disable mail in emacs 23. Basically, I occasionally press C-xm and it annoys me that it will create a Mail folder in my home directory. (I also want to remap this to 'execute-extended command).
I have tried
(global-unset-key "\C-x m")
(global-set-key (kbd "C-c m") 'execute-extended-command)
but it doesn't seem to affect anything.
This will set the binding of C-xm to the execute-extended-command function:
(global-set-key "\C-xm" 'execute-extended-command)
or
(global-set-key [?\C-x ?m] 'execute-extended-command)
or
(global-set-key (kbd "C-x m") 'execute-extended-command)

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)