Invoke single normal-mode command with Ctrl-o in Vimpulse? - emacs

The usual behavior in vim is for C-o in insert-mode to allow the user to use one normal-mode command and then return to insert-mode.
Following the suggestions in this comment, I've set
(vimpulse-imap "\C-o" 'viper-escape-to-vi)
But when I press C-o in insert-mode, the characters "^O" are inserted in the buffer and I am unable to switch to normal-mode for one command as I expect. I understand viper-escape-to-vi is bound to C-z by default but I am also unable to switch to normal-mode using this key combination either ("^Z" is printed in the buffer instead). What am I doing wrong?
Also, second question: I would like for C-z to not be bound by viper-mode (or vimpulse) as I use it for elscreen. How can I tell it to leave C-z alone?
Edit: Found this bit of code in vimpulse-misc-keybindings.el:
;; temporarily escape to vi state
(define-key viper-insert-basic-map "\C-o" 'viper-escape-to-vi)
So should be defined already? Not sure why it doesn't work.

Ad 1) It works for me, maybe something went wrong with your config?
But the output is strange: The only way to get it is a quoted-insert (or similar).
viper-escape-to-vi in insert state is bound to viper-toggle-key (but well that defaults to C-z), so see below. Again your output is strange.
Ad 2) You have to set viper-toggle-key to a sexp before viper is loaded, alternatively customize it.
For the former (using the Pause key for it)
(setq viper-toggle-key [pause])
(require 'viper)
You should try evil, being the successor to vimpulse. It's coming along well and gets rid of viper.
Setting the toggle key there (to pause again) is a (evil-set-toggle-key "<pause>") away, it's a kbd sequence not a sexp.
C-o works fine, too.

Related

Emacs: How to bind key only in regular buffers and not in the minibuffer?

I have written a fancy function, which I would like to bind to TAB. The functionality is only meaningful in any non-read-only text buffer. Currently, I bind it either like that:
(global-set-key (kbd "<tab>") 'my-indent-region)
or
(define-key global-map (kbd "<tab>") 'my-indent-region)
The problem with this binding is that now tab-completion does no longer work in the minibuffer, which is an essential feature (e.g. for buffer/file names, or M-x).
Is it possible to bind TAB only for regular modes? I know that I can use define-key some-major-mode-map, but since I want it in all modes except for the minibuffer, this would be annoying to maintain. Thus, I'm probably looking for something like a define-key any-mode-except-minibuffer ....
If such a functionality does not exist: Is there a workaround to get the tab-completion working in the minibuffer again? Maybe I can re-set the original minibuffer tab binding after changing the global binding? I couldn't figure out though which function I actually have to bind to make it work.
After some more research I found a workaround/solution to the problem in this answer.
Apparently, my problem was that I was binding to (kbd "<tab>"). If I understand it correctly, my problem was in fact not that I overwrote the actual keymap of the minibuffer -- I guess they are correctly loaded when entering the minibuffer minor modes. However, there seems to be a precedence of a binding to (kbd "<tab>") over a binding to just "\t". According to the above answer, the minibuffer bindings just use "\t", so binding to (kbd "<tab>") shadows them. I'm now using the following binding instead:
(global-set-key "\t" 'my-indent-region)
Now everything seems to be working fine.
Do you see this behavior when you start Emacs without your init file (emacs -Q)? I doubt it. If not, then recursively bisect your init file to find out what is causing the problem.
The minibuffer uses its own keymaps, which are local and which therefore take precedence over global keymap bindings.
However, any minor-mode keymaps take precedence over local keymaps. So if, for example, you have a (global) minor mode turned on that binds <tab> then that will override any binding for that key in the minibuffer keymaps.
Another thing you can do is simply bind whatever command you want to <tab> in the minibuffer keymaps. But again, you should not need to do that, if you want the usual <tab> behavior for the minibuffer.
[Another possible confusion: Some things, such as Isearch, which you might think use the minibuffer do not use it. Isearch uses its own keymap, isearch-mode-map.]
UPDATE after your comment:
Assigning a key in the global map, as you have done, should not affect what that key does in the minibuffer, provided it has a different binding in the minibuffer keymaps. TAB is typically bound in all of the minibuffer completion keymaps (but not in the non-completion minibuffer keymaps).
See the Elisp manual, nodes Completion Commands and Text from Minibuffer for information about the minibuffer keymaps.
To see what the current bindings are for a keymap that is associated with a variable (such as minibuffer-local-completion-map), load library help-fns+.el and use C-h M-k followed by the keymap variable's name. (See Help+ for more information about the library.)
If you do not want TAB to use your global command binding in the non-completion minibuffer maps (minibuffer-local-map, minibuffer-local-ns-map), then just bind it in those maps to whatever command you like. But for the completion maps you should not need to do anything - TAB should already be bound there.
Did you try emacs -Q, to see if something in your init file is interfering? If not, do that first.

How to bind Emacs key for delete whole word backword as the same in a shell?

I have just started learning Linux and Emacs. It was pleasant to have the same key bindings in both Emacs and a shell (bash/tcsh) for the most frequently used cursor movements, so that I do not have to consciously think which one I have to use. Still worse, use the wrong command and undo the mistake. There were two exceptions, though.
One often used command was the equivalent of backspace, delete a character backwards. In a shell, it was C-h. I got the same behaviour in Emacs, thanks to this tip from Janos, who probably felt the same way.
http://www.math.rutgers.edu/~komlos/emacs.htm
Now the mistake I do often in Emacs is trying to delete words backwards with the command M-C-h, as in a shell.
Can somebody please give a binding that will make Emacs delete words backwards with 'M-C-h'? Currently, this command selects the whole of the text in a buffer, which is quite an useful thing (C-a in windows), but not so frequently used as deleting words backwards.
Moreover, any binding to replace the current binding of M-h (from the link above) to help will be appreciated.
Thank you,
Elan.
Below binds C-M h to backward-kill-word. You can put it in your .emacs file.
(global-set-key (kbd "C-M-h") 'backward-kill-word)
You can use M-<backspace> in terminal and emacs to delete word backward.
It's best to use key translation so C-M-h works exactly the same as M-backspace would in any minor mode (regardless of whether M-backspace is bound to backward-kill-word or not).
;; bind C-h to <backspace>
(define-key key-translation-map [?\C-h] [?\C-?])
;; bind C-M-h to M-<backspace>
(define-key key-translation-map [?\C-\M-h] [?\C-\M-?])

Emacs define-key, Viper-mode key binding

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.

writing lisp emacs key binding and cannot specify the <delete> character

For some reason I got the default M-del key binding for backward-kill-word mapped to a scan for matching brackets and resetting is not working, so I am trying to set the global key binding in lisp. So I wrote in ~/.emacs.d/init.el the lisp commands:
(global-set-key (kbd "M-h") 'backward-kill-word)
(global-set-key (kbd "M-<\delete>") ‘backward-kill-word)
I tried them with C-x C-e and they both give the 'backward-kill-word output but only the first key-binding works "M-h", the other is ignored and M-del still trying the strange scanning action. The delete key works in emacs elsewhere, so it seems like "delete" is not being mapped to the physical key in lisp (and the backslash is there to show in this text only as the word was being commented out). Any idea what keyword to use or special character?
Best.
(I looked for libraries that may have overrided this command but I cannot find them)
On some systems, the delete key is defined as an alias to C-d. This is done through function-key-map on GNU Emacs <23 and local-function-key-map on GNU Emacs 23. (I've observed this behavior on Debian and Ubuntu 10.04 under X.) The purpose of such translations is to isolate people who code modes from the terminal intricacies: a mode that wants to shadow the delete command only needs to rebind C-d and not wonder if it should rebind delete (is that a delete left or delete right?) or deletechar or something else.
If there is a global or local binding for delete, it shadows this translation to C-d. However, if you press ESC delete, if there is no global or local binding for ESC delete, the second key is translated to C-d. This translation has precedence over the interpretation of ESC delete as M-delete. So ESC delete becomes equivalent to C-M-d.
This is arguably a bug in Emacs: the effect of ESC delete should be the same as M-delete, and there is no reason why ESC delete would run down-list which has nothing to do with deletion.
There are several possible fixes; I don't know which is best. One that should work with any version of Emacs is
(global-set-key [?\e delete] 'backward-kill-word)
The really nice thing about kbd is that what you type there is the same string that Emacs displays. So, try the following
C-h k M-<\delete> (to use your syntax)
or
M-x describe-key M-<\delete>
Emacs (for me) responds with:
M-DEL (translated from <M-delete>)
runs the command backward-kill-word,
which is an interactive compiled Lisp
function in `simple.el'.
It is bound to , M-DEL.
(backward-kill-word arg)
....
Which you can see shows that the representation for the key you want is M-DEL or M-delete.
Which is a long way of getting to the point that what you want is
(global-set-key (kbd "M-delete") 'backward-kill-word)
Of course, if you have something in your .emacs that overrides it, the above won't help. You'll need to find that included library and stop using it (or customize its behavior).
You might want to call global-set-key interactively to see how it interprets meta-delete. Also try local-set-key to ensure the strange binding is not mode-specific.
After not being able to find the library holding the conflict I found this webpage
http://www.cs.cmu.edu/cgi-bin/info2www?%28emacs%29Rebinding
Changing Key Bindings Interactively...
`M-x global-set-key KEY CMD '
Define KEY globally to run CMD....
Normally, C-z' is bound to the function
suspend-emacs' (when not using the X Window System), but you can
change C-z' to invoke an interactive subshell within Emacs, by binding
it toshell' as follows:
M-x global-set-key <RET> C-z shell <RET>
`global-set-key' reads the command name after the key. After you
press the key, a message like this appears so that you can confirm that
you are binding the key you want:
Set key C-z to command:...
And now the standard default is returned to by doing
M-x global-set-key M-del ...
backward-kill-word
But this is transient and must be done on each reload, any way to make this permanent?
Putting a command into the init.el is not overriding the other effect

Simple Emacs keybindings

I have two operations that I do all the time in Emacs:
Create a new buffer and paste the clipboard. C-S-n
Close the current buffer. C-S-w
Switch to the last viewed buffer. C-TAB
I feel like a keyboard acrobat when doing the first two operations. I think it would be worth trying some custom key bindings and macros.
A few questions about this customization:
How would I make a macro for #1?
Are these good key bindings? (I know this is a bit subjective, but they might be used by something popular that I don't use.)
Has anyone found a C-TAB macro that will act like Alt+Tab in Linux/Windows? Specifically, I want to have a stack of buffers according to the last viewed timestamp (most recent on top). I want to continue cycling through the stack until I let go of the Ctrl key. When the Ctrl key is released, I want the current buffer to get an updated position on the stack.
Have you tried using vertically or horizontally split windows for this (via C-x 3 or C-x 2)? It seems like it would give you fewer steps - even if you implement something like you're talking about.
I find split windows really speed up copy and pasting operations. I use the arrow keys on my num pad to switch among windows (windmove-left/-right/-up/-down), so it's only one key to press and you go to the window you want.
I guess this is a little different from what you're asking for, but it sounds like it might help speed things along a bit.
C-x left and C-x right cycle through buffers, but you have to hit it multiple times, you can't just keep the key pressed down.
For creating a macro for #1, you just start a macro, hit the keys you usually do to create a new buffer, and stop the macro.
So it would be something like:
C-x ( C-x b NEW RET C-x )
You can then save NEW to a file once you're done pasting, so you can use the macro again to create a new buffer. C-x e to try out the macro. If it works you can save it into your init.el file. This is done with:
M-x name-last-kbd-macro
Then you'll get a prompt to enter the name of your choice. This is only good for the current session. Then you save the named macro to your initialization file. First you open your .emacs or init.el file. Then you place point where you want the macro definition to go, then you type:
M-x insert-kbd-macro
Now you can run your macro using its name via M-x <macroname> . You can bind your macro to keys too (in your .emacs or init.el file):
(global-set-key (kbd "C-c a") '<macroname>)
For example this is how your init.el would look after creating a macro that opens a new buffer called NEW that is not associated with a file and binding this macro to C-c n:
;; Creates a new unassociated buffer called NEW
(fset 'new-buffer "\C-xbNEW\C-m");
;; Shortcut for new-buffer
(global-set-key (kbd "C-c n") 'new-buffer)
You can also throw in the paste, buffer close, and buffer switching operations. I guess you'd have to save the buffer to a file manually.
Some resources
Information about macros on EmacsWiki
Possibly useful: Swap text between buffers
start by invoking start-kbd-macro, finish by with end-kbd-macro. Afterwards you may immediately test the new macro with call-last-kbd-macro. If you're happy with the result you might want to save the macro.
Emacs generally doesn't use C-S keybindings and they are easy to use, so I'd call them good. They might cause problems if you're using the terminal version of Emacs, but I assume that's not the case with you.
I use this simple snippet:
(global-set-key (kbd "<C-tab>") 'bury-buffer)
bury-buffer basically makes the current buffer the last in the buffer-list so you'll be able to cycle buffers in a predictable order.
I wouldn't make a macro for that but write a function like someone else posted on this page. Instead of (cua-paste nil) you could also use (yank). I'm not sure which one's better and why.
I don't like them that much. For things that I use often I'd like to do as little finger acrobatics as possible, so that would mean modifier+key instead of modifier1+modifier2+key.. or use a function key if you don't feel tied to the homerow.
no comment