I have been modifying my Emacs setup quite alot recently but I reached a problem which is starting to annoy me. I would like to be able to introduce additional modifier like keys. What I am trying to do, to make things clearer, is when I am in dired-mode (which doesn't accept textual input so normal letters can be rebound) I would like it so that when I hold down the letter s and press j or l the cursor moves to the next and previous directory line respectively. Effectively making the s key act like a modifier.
I have looked into making the s apply a modifier such as super or hyper but those are all used for global things. Is this possible? if not then that's a shame.
Edit:
There seems to be some confusion with what I'm after. If I define a normal key sequence such as
(define-key map (kbd "s j") 'dired-next-dirline)
Then I have to keep pressing the s key every time before I press j to move to the next directory line. This is not what I am looking for (not to sound angry :P) I want s to act like a modifier where I can keep the s key held down and keep tapping j to move down the lines.
I hope I have made this more clear. Thanks.
I dug around in the code and came up with this. It binds s j to my-dired-next-dirline, but right after you've done that, just j is enough to do it again. Any other key resets the temporary binding.
Note that the function set-temporary-overlay-map was added in Emacs 24.2, which at the time of this writing hasn't been released yet, so you'll need to build Emacs from git.
(defun my-dired-next-dirline ()
(interactive)
(dired-next-dirline 1)
(set-temporary-overlay-map
(let ((map (make-sparse-keymap)))
(define-key map [?j] 'my-dired-next-dirline)
map)
nil))
(eval-after-load "dired"
'(progn
(let ((prefix-map (make-sparse-keymap)))
(define-key prefix-map "j" 'my-dired-next-dirline)
(define-key dired-mode-map "s" prefix-map))))
Key chords may be what you are looking for.
This question pre-dates it, but the Hydra package allows you to do this and more.
https://github.com/abo-abo/hydra
See examples here: https://github.com/abo-abo/hydra/blob/master/hydra-examples.el and here https://github.com/abo-abo/hydra/blob/master/hydra-test.el
Wiki also has more useful examples https://github.com/abo-abo/hydra/wiki/Hydras-by-Topic
Hydra originally only had a rather opaque metaphorical syntax, which made it quite difficult to get started with. I'm not sure why such a bizarre double-layer of metaphors from colors to mythical stories and monsters with many heads being "vanquished" was used to describe features which are essentially:
do action and remain in the key-mode (effectively what you're asking for.) (a so called blue hydra)
do action and quit the key-mode (a red hydra)
and so on.
Thankfully a more literal syntax was added in Feb 2015, inspect the tests to see the color and literal syntax side by side. (also this commit https://github.com/abo-abo/hydra/commit/0a3cc60f5856eb4a38204b9075d67d058ba56bef)
See also this article by the author. http://oremacs.com/2015/02/02/colorful-hydrae/
If key chords is not what you are looking for (as you suggested on a comment to Tom's answer), then you seem to want a simple define-key that uses a key sequence. i.e.
(define-key dired-mode-map (kbd "s j") 'dired-previous-line)
This has the disadvantage of disabling the original functionality of the s key, but you can rebind that to the sequence s-s, I suppose.
Related
I am switching to emacs and in the process of configuring my emacs environment, I am stuck with implementing a functionality which I used to have in eclipse and IntelliJ. Move back and forth in code with alt left/right keys.
I want it to be able to work across all major modes and across all buffers(not just for java or any specific language). For example,
say I'm at beginning of file1.txt(say p1). I search for some text and go to line 10(p2) in file1.txt. Then I open another file, file2.txt and repeat the same , start at line 1(p3) and then go to line 10(p4). Upon Alt + Left and Alt + right (or any other similar keybinding), I should be able to cycle between positions p1 <-> p2 <-> p3 <-> p4
I am aware of the following.
ctags/etags specific approach of generating tags, jumping to functions and then back. But I want it to be applicable to text files and a lot of files for which ctags cant be generated or not worth generating as I use them rarely e.g Makefiles generated from cmake builds etc.
Questions, answers and comments in
https://superuser.com/questions/241939/how-to-jump-back-to-the-last-position-of-the-cursor-in-emacs
,
In Emacs, how to go back to previous line position after using semantic Jump to Symbol?
where answers are mentioned about using mark ring and registers for
storing mark. But after trying such approach initially, I found that
once if I go forward, then backward, I pop stuff from the register.
I need it to be there forever(:D )
I read really bad things about icicles and bit hesitant to try
it.
Any other approach that I have missed ? Any plugin or anything ? Implementing this is a bit complex. I hoping this is a common problem and hoping some piece of elisp code exists for solving this problem.
maybe this is useful .maybe not
https://github.com/joodland/bm
(setq-default
bm-recenter nil
bm-highlight-style 'bm-highlight-line-and-fringe
bm-cycle-all-buffers t
bm-in-lifo-order t)
;; (global-set-key (kbd "M-.") 'bm-toggle); you need set mark before you jump
;; (global-set-key (kbd "M-/") 'bm-next)
;; (global-set-key (kbd "M-,") 'bm-previous)
and I know evil-mode
C-o evil-jump-backward
C-i evil-jump-forward
whenever you use gg G / it can remember last position
I've been using Vim for a several years. And now I want to give a try to Emacs.
For Vim I have a general config file (here) where I'm overriding defaults (e.g. hey, Vim, show me the line numbers; save more history, don't create these stupid backup files, etc...)
I want the same thing for Emacs. While searching, the best thing I've found is better-defaults.el from technomancy. I'm still digging in Prelude and Emacs-Starter-Kit sources, but there are too many overrides and plugins.
So, what I want:
ability to see a list of variables, which I can customize (e.g. indent-tabs-mode or newline-and-indent). I know about C-h v variable-name but this command requires me to know a name of variable, but I want a list of them
sample config file for Emacs which sets helpful defaults with comments for each command
For your first question: M-x customize-option.
C-h v TAB is not what you want, as it shows you also non-option variables (e.g., internal variables).
However, if you load library help-fns+.el then C-u C-h v TAB shows you only the user options (in buffer *Completions*).
My advice would be to not look for an existing "sample config file", if you intend to start with it, as opposed to just seeing how another user redefines things. And for help with the latter, I would still recommend the Emacs manual over looking at someone elses init file. Especially to start with.
However, if you really want to look at init files from other users then this is the place to start. (And this is a good place to start, other than the manual (which is the best place), to learn about customizing Emacs.)
Finally, my (unsolicited) advice wrt learning Emacs, including customizing, is to start by not customizing it at all. I say that without irony as one who has heavily customized Emacs.
If you want to "get it", i.e., to get a feel for the Emacs design and what makes it different, then let yourself get used to Emacs as it is out of the box -- for maybe a month or so. At that point you can think about customizing, and your customizations are likely to be much wiser (in your own terms, i.e., for whatever it is that you want).
Another way of putting this is that until you know Emacs a bit, you really do not know what it is that you want or need in terms of customization. In particular, it would be a mistake, IMO, to start out by trying to think of Emacs in terms of Vim or trying to make Emacs do what you've done in Vim. There is plenty of time for that later, if, based on understanding Emacs, you really do want to do that.
Welcome to Emacs. Enjoy.
I'm going to take a reasonable dissent from Drew's excellent answer, there are some things you really ought to set in your emacs-file immediately, that aren't set out of the box that you really ought to set.
Issue number 1: THAT $(generate-swearing) BELL!
The bell will ding like a madman. That's annoying. You can turn it off.
In your init-file, do this:
(setq visible-bell 1)
Issue number 2: Emacs has an interesting view of backup files.
If you edit a file, say "foo.txt", emacs will create little backups of the file with the name "foo.txt~" in the same directory.
This is annoying as all hell, and you can fix it by doing this:
(setq backup-directory-alist '(("" . "~/.emacs.d/emacs-backup")))
Issue number 3: Emacs uses C-w differently than bash does, and that's a bit annoying.
C-w usually deletes a word backwards. By standard in emacs, it deletes the marked region. That's a bit silly.
It is better to do something like this:
;; This is my preference, your mileage may vary.
(global-set-key (kbd "C-x C-k") 'kill-region)
(global-set-key (kbd "C-x k") 'kill-buffer)
(global-set-key (kbd "C-w") 'backward-kill-word)
Issue number 4: Alt-X is a clunky way of running an interactive command.
It is better to do something like this instead, avoid your hand cramping up all the time.
(global-set-key (kbd "C-x C-m") 'execute-extended-command)
(global-set-key (kbd "C-x C-m") 'execute-extended-command)
You also may want to check out Steve Yegge's Effective Emacs: https://sites.google.com/site/steveyegge2/effective-emacs
It's pretty amazing. One thing to note though is that the caps-lock to ctrl thing is also available through a microsoft tool here:
https://learn.microsoft.com/en-us/sysinternals/downloads/ctrl2cap
This is better than the manual hack Yegge suggests, and you can turn it off if you don't like it.
I have done some searching around and can not quite find what I am looking for so I figured I would ask. This could be due to not using the right terms however.
I am new to emacs, having used vim for eight years or so, but I really need an editor I can morph to my will so I am switching. Now in vim there is a leader key you can define to avoid overlapping bindings with various extensions. In emacs there are supposed to be chords set aside for the user only but various modes do not adhere to this. Is there a way in emacs I can ensure my custom bindings do not overlap similar to the vim leader key? The reason I ask is I want to keep my bindings sane.
The manual details the conventions:
C-hig (elisp) Key Binding Conventions RET
Sequences reserved for the end-user are:
C-c<letter> for any (un-modified) upper or lower case letter: [A-Za-z]
Function keys F5, F6, F7, F8, F9 (again, without modifier keys)
I recommend using the easiest of these sequences as prefix bindings, as you can then follow them with any key at all, giving you a large number of options.
Someone else's recommendation that I liked was to unbind C-z if you don't commonly use suspend-frame, as that opens up another convenient prefix.
Of course, if you set up Super and Hyper modifier keys for your OS & keyboard, you would likely gain more convenient sequences than you could find uses for. This is a very reasonable option for many people with the additional modifier keys found on many modern keyboards.
Finally, the key-chord library is quite a popular way of creating new convenient and non-conflicting bindings by using pairs of (un-modified) keys typed together or in quick succession (or a single key tapped twice). This works very well in my experience, although you obviously have to be very careful to avoid binding sequences which might occur naturally.
I personally use C-' as my 'leader key' for my personal keybinding map. You can create the prefix and bind keys to it like so:
(global-set-key (kbd "C-'") ctl-quote-map)
(define-key ctl-quote-map (kbd "C-p") 'stumpwm-move-window-up)
(define-key ctl-quote-map (kbd "C-n") 'stumpwm-move-window-down)
(define-key ctl-quote-map (kbd "C-f") 'stumpwm-move-window-right)
(define-key ctl-quote-map (kbd "C-b") 'stumpwm-move-window-left)
(define-key ctl-quote-map (kbd "r") 'stumpwm-interactive-resize-window)
Nobbody steps over something that obscure, and if you use the left control it's a balanced double pinky motion. C-; is also good, which is what I use for the stumpwm escape key.
There are Emacs keybinding conventions, some of which honestly surprised me.
The relevant pieces are that C-c [a-zA-Z] and <F[5-9]> are reserved for end users.
Good answers so far:
I'd throw in free-keys as a good way to check your binding is free before creating it - to avoid overlapping with yourself. helm-descbinds is good for finding your existing bindings to help you remember them as well incidentally.
I'm familiar with scheme, but new to emacs (switching over from VIM) and elisp.
I know how to do the following:
make a simple key binding
C-c iwb = indent whole buffer
F2 = turns folding on/off
use slime from emacs
some basic keys, like C-x 2, paredit keys, some basic movement keys
I need help doing something a bit more advanced:
I want F3 to equal:
put emacs into C-x 2 mode
in bottom window, switch to "slime-repl" buffer
in the "slime-repl" buffer, send the command "(test/run)" <-- note, this is meant to be sent to the swank server, NOT to elisp
I realize it's terrible form to ask people to write a script for me; however, if anyone could do that, I would learn rather quickly from it. [And it would allow me to do more complicated types of scripting through studying your example.]
Thanks!
This is not exactly what you want, but should be a good starting point for further tweaking:
(defun slime-run-test ()
(interactive)
(slime-interactive-eval "(test/run)")
(slime-pop-to-buffer (slime-output-buffer) t))
(global-set-key (kbd "<f3>") 'slime-run-test)
I don't use slime, but assuming it uses comint-mode then I would think the following might do the trick:
(defun my-slime-test-run ()
(interactive)
(delete-other-windows)
(split-window-below)
(with-selected-window (next-window)
(switch-to-buffer "slime-repl")
(goto-char (point-max))
(insert "(test-run)")
(comint-send-input)))
(global-set-key (kbd "<f3>") 'my-slime-test-run)
There is probably a better way to do this, but hopefully that gives you a little insight into how you can write elisp functions to carry out tasks in the editor (and note how the function reads very much like a set of editor instructions -- you can do a lot simply by converting the keystrokes you would use into equivalent code -- or even not writing code at all, and simply recording & saving keyboard macros).
Use C-hf name-of-the-function RET to get documentation on any of the function/macro calls in that function.
For the keybinding, I used C-hkF3 to check how Emacs referred to that key, and then used that string as the argument to kbd (and note how you can use that sequence to find the name of the function bound to any given key sequence, which you can then utilise in code if desired).
Many things are far less obvious if you don't already know them, but that's only to be expected with a code base as large as this (and dating back as long as this).
The great thing is that if you don't know what you're looking for, you can always search for function names matching patterns with C-uC-ha (and similarly for variables, values, libraries, and documentation; see M-: (info "(emacs) Apropos") RET for more about this facility). Plus the info manuals (complete with indexes -- press I or i within any particular manual, or use the info-apropos command to search all info manuals at once).
Truly one of the very best things you can do is to learn how to use the self-documenting nature of Emacs to find answers to the things you don't already know.
I'm trying to learn emacs, getting vi custom key bindings.
Using Viper-mode, what is the correct way to re-bind a key? (I'm using Colemak keyboard layout(instead of qwerty) so have to change things like n->j) But would like it to work in viper-mode.
From this key binding guide on GNU.org:
http://www.gnu.org/software/emacs/manual/html_node/viper/Key-Bindings.html
It says the command to put in your .viper file is:
(define-key viper-vi-global-user-map "\C-v" 'scroll-down)
It doesn't work for me... in fact not sure I even have the function "define-key"...
M-x define-key [No match]
I'm not sure if 'define-key' is available on my version of emacs?
This works, but not in viper-mode
(global-set-key "n" "j")
Any help would be much appreciated. This is my first day using Emacs, to it's a pain getting Colemak & Viper-mode to work properly.
Thank for any help...
Hopefully some useful answers here:
First, having that line in the .viper works for me. Note that the viper-vi-global-user-map applies when you're in command mode, not insert mode.
Secondly, define-key isn't a command, it's a regular function, which just means that it cannot be called using M-x. See this Emacs wiki page for a little more detail on that distinction. But that was a good attempt.
Third, the global-set-key is a command, you could have tried making a change using M-x global-set-key. But, that sets the key in the current global map, which isn't the same as viper-vi-global-user-map. Viper-mode uses a bunch of different keymaps to make Emacs behave like vi, but all of the maps are overlaid on top of the global map.
I'm guessing that you found that C-v wasn't bound like you want when you're in insert mode. And that can be solved by adding this to your .viper:
(define-key viper-insert-global-user-map "\C-v" 'scroll-down)
Lastly, scroll-down may not be what you want. The down refers to the text moving down (given the perspective of a fixed window). C-v is generally bound to 'scroll-up. But,maybe it is exactly what you want.
Caveat: I'm not a viper-mode user, I don't even know how to use vi. So my terminology may be off. But I find the challenge of changing things in viper-mode very interesting.
Edited to add
From your comment it sounds like you want n to be the same as what j is bound to by default. Try adding this:
(define-key viper-vi-global-user-map "n" 'viper-next-line)
In "normal" mode I did M-x describe-key j, which told me that j is bound to 'viper-next-line, and the above line will bind n to the same routine. Repeat for the rest of the bindings you want to shift around.
in modern times evil-mode is the vim emulation layer for emacs, and to tweak it for colemak, my https://github.com/wbolster/evil-colemak-basics package helps a lot.