Use only two functions from the evil package as keybinding - emacs

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.

Related

Shortcut to open a specific file in Emacs

I want to be able to use a keyboard shortcut to edit my .emacs file instead of typing Ctrl-XCtrl-F.emacsEnter every time (here's an analogous question regarding Vim). There's probably an obvious way of doing this, but I can't find the Emacs Lisp command to open a file. I'd think it would be something beginning with "open" or "file" but typing M-x and those terms doesn't seem to bring up anything relevant. I tried
(global-set-key (kbd "<f6>") (find-file "~/.emacs"))
but that doesn't work.
According to the documentation
(global-set-key KEY COMMAND)
Give KEY a global binding as COMMAND. COMMAND is the command
definition to use; usually it is a symbol naming an
interactively-callable function.
So you have to use an interactively-callable function:
(global-set-key (kbd "<f6>") (lambda() (interactive)(find-file "~/.emacs")))
Personally I prefer to use emacs registers to store files which I use often.
I would store '~/.emacs' in a register:
(set-register ?e (cons 'file "~/.emacs"))
and open it with C-x r j e
Bookmarking is an excellent solution for this purpose that is packaged with emacs.
That way if it's ok for you to see the list of files that you want to open, you can easily browse them. There is also BookmarkPlus which offers variety of options.
M-x list-bookmarks

Is there an Emacs paredit hook available so I can redefine C-j?

I like using C-j to eval-last-sexp but paredit-mode (which I otherwise like) overrides this to paredit-newline. Looking in the paredit-mode docs I don't see anything like a paredit-mode-hook defined where I can add-hook to call local-set-key or a similar function.
Anyone have a suggestion?
Update
After trying out the two answers below and not having much success, I think the problem may be related to the fact that paredit is getting loaded in a few different contexts? To wit, I am opening both Common Lisp, Clojure and Emacs Lisp files, all of which can use paredit. Sadly, the various forms of eval-last-sexp have slightly different names in each mode, so I can't define the key once for everything. Rather, I need to bind the key based on the major mode that I am in also. Hopefully that adds another useful data point.
No need to use hooks, something like the following should work:
(eval-after-load "paredit"
#'(define-key paredit-mode-map (kbd "C-j") 'eval-last-sexp))
Alternatively, if for some reason that doesn't work, or you simply prefer the use of hooks, you can add the above define-key invocation to the hooks of the major modes for which paredit is activated.
Every mode defined by one of the define-*-mode macros automatically runs a corresponding MODE-hook.
I see that paredit-mode is a minor mode defined with (define-minor-mode paredit-mode ...), and therefore it will run paredit-mode-hook.
You can type M-x find-function RET define-minor-mode RET and search for run-hooks to see where this behaviour is defined.
Edit (based on addition to question):
Do they all use C-x C-e as a default binding for the mode-specific eval-last-sexp function, by any chance? That would seem like a consistent thing for them to do, and if so then you could use the following approach:
(local-set-key (kbd "C-j") (key-binding (kbd "C-x C-e")))
There is a paredit-mode-hook. You don't see it until you add something to it. Weird, but this is the way hooks behave.
However, in your case, the best approach may be to clear the paredit binding for C-j:
(eval-after-load 'paredit
#'(define-key paredit-mode-map (kbd "C-j") nil))
And then set your own via local-set-key in every major-mode hook.

Emacs macro works in .emacs file but not in a source file?

I have an emacs macro (global-set-key) that works perfectly fine in my .emacs file, but for whatever reason, it does not work in my .c file.
(global-set-key "\C-c\C-d" "\C-a\C- \C-e\M-w\C-j\C-y")
If I close and re-open my .emacs file and start messing around, this macro behaves as expected, copying a line to a line below. However, when I open a C file the same macro simply deletes a character (it only seems to pick up on C-d).
Any ideas?
The cc-mode defines C-c C-d in c-mode-base-map to be c-hungry-delete-forward, which is hiding your binding at the global level. So, the better way to solve this is to undefine the binding that cc-mode made, and you do that with the following:
(eval-after-load "cc-mode"
'(define-key c-mode-base-map (kbd "C-c C-d") nil))
You could also do it in a hook, but I prefer eval-after-load because it only gets executed once.
Note: I determined the existing binding by opening up a file in c-mode and typing C-h C-k C-c C-d (aka M-x describe-binding C-c C-d), and seeing:
C-c C-d runs the command c-hungry-delete-forward, which is an
interactive compiled Lisp function in `cc-cmds.el'.
This made it pretty clear that the binding was set up in (one of the) c-modes, so I just opened up (or greped) the source files for c-hungry-delete-forward whereupon I found:
(define-key c-mode-base-map "\C-c\C-d" 'c-hungry-delete-forward)
And then the answer was straight forward.
I think it's better to undefine local bindings that hide the global bindings you want, rather than redefining them. It's just as much work to find the problematic bindings, and this way if you want to change the function for the global binding, you only have to do it in one place.
Clearly the C mode is removing the binding, or changing it. You can try to add it to a c-mode-hook and see if it works then. Similar to this:
(add-hook `c-mode-hook '(lambda ()
(global-set-key "\C-c\C-d" "\C-a\C- \C-e\M-w\C-j\C-y")))
Either c-mode-hook or c-mode-common-hook. You can also use local-set-key instead of the global one to apply the binding just to this buffer.

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.

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.