Define key-bindings in emacs - emacs

I'd like to map a command in emacs to a key-binding. I want the command Control-l to have the same effect as the command Alt-x goto-line followed by a return (since that command first needs a return to be invoked and then a line number).
I modified the init file as follows:
(define-key (M-x goto-line) '\C-l)
but that didn't work. The error was that define-key was being given more than 1 arguments.
Does anyone know how to reset key-bindings in emacs?
Thanks!

M-g g is the default shortcut for goto-line. You might want to try that.
To redefine C-l use:
(global-set-key (kbd "C-l") 'goto-line)

Easiest way to customize lots of keybindings is to install John Wiegley's bind-key module, which is a part of use-package Lisp package. Solution in your init.el:
(require 'bind-key)
(bind-key "C-l" 'goto-line)
Minor modes keys usually override global keys, so if you don't want such behavior, use function bind-key* instead. The package is on MELPA, if you don't know what is it, quickly learn about Emacs package management (should take you 2 minutes to set up MELPA as your repository).
The main problem with keybindings in Emacs is that minor modes keys often override your custom ones. In vanilla Emacs people workaround by creating a minor mode for your own keybindings. If you really wanna understand how Emacs keys work, read Key Bindings # Emacs Manual and Keymaps # Elisp Manual carefully.

I have set as (global-set-key (kbd "C-x g") 'goto-line). You can use that or (global-set-key (kbd "C-l") 'goto-line). I would personally do not touch the C-l key from its default behavior.
If you must use M-x define-key, use
(define-key global-map (kbd "C-l") 'goto-line). The 1st argument to define-key is a KEYMAP.

Related

Is it possible to collapse a function in emacs?

So, for a lisp homework assignment I have, it has a long defparameter expression that's a large data set. What I'm wondering is, does emacs or SLIME have anything to "collapse" that large defparameter into a single line, like, say, MATLAB does?
Like Bertfred mentioned, hideshow works great, and it comes build in with more recent versions of emacs. To use it simply add the following snippet to your init file:
(add-hook 'prog-mode-hook #'hs-minor-mode)
(global-set-key (kbd "C-c <right>") 'hs-show-block)
(global-set-key (kbd "C-c <left>") 'hs-hide-block)
The first line enables the functionality in any major mode associated with programming. Once there, C-c <left> and C-c <right> should do what you expect - just be mindful of where point is.
https://www.emacswiki.org/emacs/HideShow
There's also a package on Melpa called vimish-fold (or the equivalent evil version of it - evil-vimish-fold).
It is not as "automatic" as hideshow or outline in the sense that you have to select the lines you want to fold, but the advantage is that you can fold any lines. And the folds don't disappear the when you close your file.
You can define your keybindings for creating/deleting folds and for unfolding/refolding folds, and there you go!
(global-set-key (kbd "your-keybinding") 'vimish-fold)
(global-set-key (kbd "your-keybinding") 'vimish-fold-delete)
(global-set-key (kbd "your-keybinding") 'vimish-fold-toggle)

Use only two functions from the evil package as keybinding

I want to get the search at point functionality ("*" and "#") in emacs.
So I want to use "*" and "#" from the emacs evil-mode, as this is one of the suggestions. However, I don't want anything else from the evil-mode, just those two functions!
This is my .emacs file:
(package-initialize)
(evil-mode 1) ;; enable evil-mode
(global-set-key (kbd "C-*") 'evil-search-symbol-forward)
(global-set-key (kbd "C-#") 'evil-search-symbol-backward)
Now the keybindings work, but I loaded the whole evil-mode, so it messes up my standard emacs keybindings like "C-y" for yank.
If I don't load the evil-mode in the .emacs file, I get the error:
Symbol's function definition is void: evil-search-symbol-forward
#event_jr's answer mentions highlight-symbols, which is probably a much more lightweight way to do what you want.
However, if you really want to use the evil version, you can (require 'evil) in your .emacs file without turning it on (ie, leave off the (evil-mode 1) statement).
Meanwhile, the functions you want are actually named evil-search-word-forward and evil-search-word-backward, not the ...-symbol-... version you saw on the wiki page (which is probably outdated).
You should use highlight-symbols for symbol jumping and highlighting.

Emacs/elisp: global-set-key bindings not taking effect for Meta-<down> or -<up>?

I am trying to bind M-<up> and M-<down> to scroll-down-line and scroll-up-line respectively as indicated here: https://stackoverflow.com/a/16229080/562139.
This is what I have in my .emacs:
;; Key bindings
(global-set-key (kbd "M-g") 'goto-line)
;; Scroll line by line
(global-set-key (kbd "M-<down>") 'scroll-up-line)
(global-set-key (kbd "M-<up>") 'scroll-down-line)
Problem:
The scroll key bindings are not taking effect, while the one for goto-line does.
When I run M-x scroll-down-line however, emacs prompts me and says
"you can run the command with <M-down>"
Note:
When I run global-set-key (kbd "M-<down>") 'scroll-up-line) or (global-set-key (kbd "M-<up>") 'scroll-down-line) directly in the mini-buffer, the bindings take effect! However, I seem to have noticed through the corner of my eye when I do the latter, that pressing M-<up> actually sends something like ESC ESC-<up>.
I'm foxed. What gives?
Note: I am running emacs 24.3 in a terminal (via iTerm on OSX with Option key mapped to ESC+) over SSH to a RHEL5 virtual machine.)
Update
I followed the suggestion in this answer and found that pressing M-<up> results in something completely different:
ESC <up> (translated from ESC M-[ A) runs the command
scroll-down-line, which is an interactive compiled Lisp function.
It is bound to <M-up>, ESC <up>.
(scroll-down-line &optional ARG)
I'm going to try binding that key sequence to the function and check the result.
Try starting Emacs without your init file: emacs -Q, and see if you can reproduce the problem.
I do not see the problem, with Emacs 24.3 in terminal mode.
What you saw briefly was probably ESC <up>, which is equivalent to M-<up>.
Did you perhaps mean to type "When I run M-x scroll-up-line (instead of down)?
I suspect that you are in some mode that gives a local binding or a minor-mode binding to these keys, which overrides the global binding. To test that, try in a buffer that is in fundamental mode. If that is the case, then to override that overriding you will need to also bind the keys in that mode's keymap.
If you cannot repro the problem starting from emacs -Q then bisect your init file (~/.emacs) recursively until you find the culprit code.
Seems key got lost in translation.
Planted a forward-paragraph at openSuse that way:
(global-set-key [(meta down)] 'forward-paragraph)

Rebind C-space in Emacs

I've tried various version to no avail:
(global-set-key (kbd "C-<space>") 'tempo-complete-tag)
(global-set-key [C-space] 'tempo-complete-tag)
I'm using CUA mode and running Emacs on Ubuntu, version: GNU Emacs 23.1.50.1 (x86_64-pc-linux-gnu, GTK+ Version 2.18.0)
of 2009-09-27 on crested, modified by Debian
When I run tempo-complete-tag manually it tells me it is bound to C-space but C-space still runs cua-set-mark (or if CUA is disable, set-mark-command).
How can I rebind the C-space shortcut in Emacs to a command I decide?
C-h k (key) will tell you how Emacs refers to a given key (which is "C-SPC" in this instance). (global-set-key (kbd "C-SPC") 'tempo-complete-tag) will do what you want.
I always use the (kbd) function for key-bindings, as it allows you to refer to the key the same way it is typically written everywhere else.
Do keep in mind that C-SPC is a standard set-mark-command binding! Personally I'd pick something different :)
Also keep in mind that "global-set-key" will only do what you want, if your mode doesn't override it. I'm too lazy to load tempo to see if it does indeed override C-SPC, but it might well do so, in which case, you want to put this in your .emacs:
(add-hook 'tempo-mode-hook
(lambda ()
(local-set-key (kbd "C-SPC") 'tempo-complete-tag)
))
Alternative syntax for key binding is via vector:
(global-set-key [?\M-\ ] 'cycle-spacing)
(global-set-key [?\C-\ ] 'tempo-complete-tag)

Assign multiple Emacs keybindings to a single command?

I'm giving ErgoEmacs mode a try to see if I can use Emacs more comfortably. Some of its keybindings are fairly intuitive, but in many cases I don't want to outright replace the defaults.
For example, in the context of ErgoEmacs' navigation shortcut structure, M-h makes sense as a replacement for C-a--but I want to be able to use both, not just M-h. I tried simply duplicating the commands:
;; Move to beginning/ending of line
(defconst ergoemacs-move-beginning-of-line-key (kbd "C-a")) ; original
(defconst ergoemacs-move-end-of-line-key (kbd "C-e")) ; original
(defconst ergoemacs-move-beginning-of-line-key (kbd "M-h")) ; ergoemacs
(defconst ergoemacs-move-end-of-line-key (kbd "M-H")) ; ergoemacs
But Emacs simply overwrites the first keybinding with the second. What's the best way to address this?
To re-post reply from ergo-emacs mailing list:
Xah Lee said:
that's very easy.
in the
ergoemacs-mode.el file, there's this
line (load "ergoemacs-unbind") just
comment it out. That should be all
you need to do. However, note that
ErgoEmacs keybinding defines those
common shortcuts such as Open, Close,
New, Save... with keys Ctrl+o,
Ctrl+w, Ctrl+n, Ctrl+s etc. About 7 of
them or so. So, i think some of these
will hit on emacs traditional
bindings with Ctrl. if you are new to
ErgoEmacs and trying to explore it,
you might just try starting with few
keys. this page might have some
useful info:
http://code.google.com/p/ergoemacs/wiki/adoption
thanks for checking out ErgoEmacs!
Xah ∑ http://xahlee.org/
As it turns out, ErgoEmacs uses two files to define the keybinding. One is the main ergoemacs-mode.el file, and the other is the specific keyboard layout you select (e.g. ergoemacs-layout-us.el). The latter document creates a constant, which the former uses to create the keybinding. So while I thought I was duplicating the keybinding, I was actually changing the constant which was subsequently used for that purpose.
Solution:
In ergomacs-mode.el:
;; Move to beginning/ending of line
(define-key ergoemacs-keymap ergoemacs-move-beginning-of-line-key 'move-beginning-of-line)
(define-key ergoemacs-keymap ergoemacs-move-end-of-line-key 'move-end-of-line)
(define-key ergoemacs-keymap ergoemacs-move-beginning-of-line-key2 'move-beginning-of-line) ; new
(define-key ergoemacs-keymap ergoemacs-move-end-of-line-key2 'move-end-of-line) ; new
In ergoemacs-layout-us.el:
;; Move to beginning/ending of line
(defconst ergoemacs-move-beginning-of-line-key (kbd "M-h"))
(defconst ergoemacs-move-end-of-line-key (kbd "M-H"))
(defconst ergoemacs-move-beginning-of-line-key2 (kbd "C-a")) ; new
(defconst ergoemacs-move-end-of-line-key2 (kbd "C-e")) ; new
Huh? Is having one and only one way for every function some golden principle of ErgoEmacs? Because normal keybinding works exactly the opposite way: you name one key at a time and specify what it should do. If a mode defines a global variable to mean "the key that end-of-line is bound to", then of course there can be only one value, but with the normal binding commands you can bind the same function to as many combinations as you like. In fact, every keybinding I have ever seen used looked either like this
(global-set-key [(meta space)] 'just-one-space)
or like this
(add-hook 'c-mode-hook 'my-c-mode-hook)
(defun my-c-mode-hook ()
(define-key c-mode-map [(control c) b] 'c-insert-block))
if it's only for a specific mode.