When using zsh, I sometimes accidentally press Escape out of habit, expecting it to clear the entire line as it does in Windows. Instead, it goes into a mode that I'm not sure how to get out of. The cursor goes back one character, and some keys perform some special commands, but all I really want to do is get out of this mode and be able to press Ctrl+U to clear the line.
Searching around has been tough - I get results for escaping characters.
Short answer: press a.
Medium answer: press a, then enter bindkey -e.
Long answer: Like a lot of UNIX shells, zsh has an emacs-like mode and a vi-like mode. You're in vi-like mode, and ESC takes you out of the vi-like insert mode. a puts you back into insert mode, with the cursor after the current character. (Sorry for the two different uses of "mode," but it is the accepted terminology in both cases.)
bindkey -e overrides the settings from the rc files and puts zsh into emacs mode, which only has one mode (i.e., no "ESC mode"), so this won't bother you any more. Unfortunately, it won't carry over to your next shell invocation. bindkey -v would switch from emacs mode back to vi mode.
In the absence of any other configuration, zsh defaults to emacs mode, so unless there's something in one of the rc files, the likely culprit is that the EDITOR variable is some form of vi, which causes zsh to default to vi mode. If you don't like vi mode, then you should probably hunt down what part of the system-wide or user-specific configuration is causing zsh to default to vi mode and turn it off by removing it or overriding it in one of those rc files.
If everything else fails, when you're in ESC mode, type :w then Enter to save, and then :q to exit. You can also type :wq and Enter
Related
https://www.gnu.org/software/bash/manual/html_node/Readline-vi-Mode.html
said we could do set -o vi to use vi mode instead of emacs mode for command line editing.
And the help: ex-edit-index sais
c_CTRL-P CTRL-P after using 'wildchar' with multiple matches:
go to previous match, otherwise: recall older
command-line from history.
However when I press CTRL-P I just got ^P
May I know why ?
Readline's "vi mode" is neither vi nor Vim. It is a partial emulation of vi shoehorned into the command line context.
That <C-p> you are referring to is a Vim command so there is no reason whatsoever to expect it to do anything in that context. Vim's documentation is totally irrelevant in this case.
Search for Vim Mode bindings in $ man readline for the actual bindings at your disposal in that "mode".
Sometimes when you ctrl-v with bash it will run the command even though you didn't intend to run it yet - is there a way to paste a command into the bash shell / terminal making sure you don't actually run any of the command(s)?
if you could set what was on the terminal prompt programmatically, you could do this with bash on MacOS:
export BASH_PROMPT="$(pbpaste)"
which ties into my other question that I just asked:
How to change the value that's in the prompt
There is a Readline variable:
enable-bracketed-paste
When set to On, Readline will configure the terminal in a way that will enable it to insert each paste into the editing buffer as a single string of characters, instead of treating each character as if it had been read from the keyboard. This can prevent pasted characters from being interpreted as editing commands. The default is off.
To turn this on, put something like
set enable-bracketed-paste on
into your ~/.inputrc.
This was introduced in Bash 4.4 / Readline 7.0.
Use ^X^E aka Ctrl+X Ctrl+E in bash to open your $EDITOR for command entry.
Paste and/or edit as much as you want, across as many lines as you want. When you're done, save and exit, and bash will run it.
(In vi mode, the shortcut is v)
I was fascinated when I discovered vi-like bash's editing mode. One can easily toggle it by issuing set -o vi command. But is there any way to toggle vim-like mode?
I mean when you are in a vi-like mode you can press v key and edit your commands in a fully functional vi editor's window but I would like to know how to force bash to start vim editor instead of vi because of the perks vim provides us (visual selection, additional commands etc)?
If you set EDITOR variable, in bash you can press Ctrl-x ctrl-e to edit your current command line in your EDITOR e.g. vim
EDIT
the ctrl-x ctrl-e is for emacs mode commandline editing, which is the default one. If you have already set it to vi mode, you could do what you have said, pressing the v. If you want to open the cmd line in vim, you have to set the EDITOR variable (in your .bashrc for example)
Personally I edit command line in emacs mode, even though vim is my main (and only) editor.
In your .bashrc, put the following line:
export VISUAL=/usr/bin/vim
If you want vim in many other contexts too, such as in git, you should also set EDITOR:
export EDITOR=/usr/bin/vim
I recently switched to using vi mode (set :o vi) in my bash terminal. In the past, I've always used ctrl+p to look through previous commands, but for some reason after switching to vi mode bash hangs for about 20 seconds or so when I press ctrl+p in insert mode.
Note, this only happens over ssh. Locally it just inserts ^P.
Does anyone know why this is happening, or how I can hack it to remove the ctrl+p shortcut from OS X Terminal (I'm happy with using ESC, k, k,...)?
Thanks
In insert mode, ^P is bound to menu-complete-backward, so it’s running through all of bash-completion, which takes a while. If you want to cycle through the command history, you need to leave insert mode and use k and j. You could also bind ^P to previous-history.
In the OS X version of bash (which uses BSD libedit instead of GNU readline), ^P in insert mode doesn’t do anything.
In my quest to configure my shell to work exactly how I want it with respect to the alt/meta key I am having some trouble. Recently I added "bindkey -m" to my .zshrc and now whenever I start a zsh shell (ie open a terminal window) I get this error "warning: `bindkey -m' disables multibyte support".
Now since I don't care much about multibyte support atm, is there a way I can disable just this warning? Even better would be a way to use 8th-bit meta as well as multibyte. Also note that this happens on a clean zsh install on 4.3.9 and 4.3.10
My reasoning for wanting bindkey -m
vim: alt mappings (my own personal commands/mappings)
zsh: alt mappings (such as Alt-. to recall the last argument of the previous command)
emacs: alt mappings (lots of built-ins)
So, is there any way to disable this warning or otherwise accomplish what I'm trying to do?
You can sweep the message under the rug:
bindkey -m 2>/dev/null
Don't mappings to escape (\[) also work for Alt for you? Alt-. works for me, for example, by default.
Also, unless you're talking about the command line editing modes, vim and emacs handle their own keyboard mapping. Settings in zsh won't affect them.