Rebind C-space in Emacs - 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)

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)

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)

Can't map Ctrl + minus in Emacs in Mac OS X

I'm trying to map Ctrl + minus ("C--") to undo in Emacs 24.3 (from http://emacsformacosx.com) in Mac OS X 10.8.4, but I can't get it to work. There seems to be some very global key binding for decreasing the font size, which I can't seem to override. Can anyone tell me what I'm doing wrong?
I have the following in my .emacs.
(global-set-key (kbd "C--") 'undo) ;; Doesn't work
(global-set-key (kbd "C-u") 'undo) ;; Just for testing, does work
When I press Ctrl+U, it triggers undo, but when I press Ctrl+minus, it decreases the font size. It might be simply that I should use something other than "C--", but it looks like it should work. I checked the key bindings (via C-h b) and there, C-u is bound to undo, but C-- is bound to text-scale-decrease. It would probably be possible to find where that key is bound and get some clue, but my Emacs-fu is too weak.
I'm using the graphical version of Emacs, not the terminal version.
With these type of problems I usually
try f1 k and right after the key combination that I'm having a problem with,
C-- in your case.
One of two things should happen:
Nothing happens - this means that the shortcut is being intercepted
at the level of the operating system.
It gives you a description of a function that's being called.
It's probable that it was set by either your major mode or one of the minor modes.
So you should investigate that as well, searching though the references
to this function, which is text-scale-decrease in you case.
After you find either global-set-key, or local-set-key or define-key
with this function, either comment it out, or better just
call the same function with same shortcut and nil in your ~/.emacs.
UPD: how to unset a key
When you find that some source that you're loading e.g. starter-kit is setting the key,
you just need to unset it later in the same way:
If it was set with (global-set-key (kbd "C--") 'text-scale-decrease),
you unset it with (global-set-key (kbd "C--") nil).
If it was set with (define-key markdown-mode-map (kbd "C--") 'text-scale-descrease),
you unset it with (define-key markdown-mode-map (kbd "C--") nil).
If it was set with
(add-hook 'markdown-mode-hook
(lambda()(local-set-key (kbd "C--") 'text-scale-descrease))
you unset with
(add-hook 'markdown-mode-hook
(lambda()(local-set-key (kbd "C--") nil))

Emacs: Keyboard shortcut to run ansi-term with a specific shell

I would like to associate a keyboard binding in Emacs (e.g. C-c a) that automatically starts an ansi-term window with a shell that I have pre-specified in my .emacs file (without prompting anything)
For reference, there are two threads in StackOverflow that address similar problems:
Remote ssh connection from within Emacs
Emacs: Default binary to run with M-x ansi-term
but it isn't obvious to me how to combine the ideas in those threads to get an answer to my question.
(global-set-key (kbd "C-c a") '(lambda () (interactive) (ansi-term "/bin/zsh")))
I suggest you to use multi-term. As its name implies, it lets you deal with multiple term using ansi-term.
Here is a small configuration:
(require 'multi-term)
(eval-after-load "multi-term"
'(setq multi-term-program "/bin/bash"
term-unbind-key-list '("C-x"
"C-h"
"M-x"
"C-z")
term-term-name "xterm-256color"))
(global-set-key (kbd "C-c a") 'multi-term-next)
My whole configuration for multi-term is
here
(compilation-shell-minor-mode is really nice).

Define key-bindings in 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.