I'm trying to adapt to vim, again, I'm doing pretty well for now but unfortunately Gvim and Vim doesn't handle the same way the alt key.
In Vim, pressing ALT+ any other key) is the same to press (ESC + any other key). Even in bash's prompt using the vi mode (set -o vi).
If I use the same shortcuts in Gvim, estranges UTF-8 characters are printed.
How can I disable
In Vim, pressing (ALT + any other key) is the same to press (ESC + any other key). Even in bash's prompt using the vi mode (set -o vi).
Vim doesn't do that, your terminal does -- which is why you see the same behavior in other programs in that terminal, like bash. Instead of removing behavior from gvim, you need to add behavior to gvim that matches the terminal behavior you expect.
Depending on your window manager, you may be able to map to do what you want:
# in .vimrc, or without guards in .gvimrc
if has("gui_running")
map <m-j> (something)
endif
Use map, nmap, imap, ... depending on which modes you want.
As mentioned by #fred-nurk the difference in key mappings between terminal based vim and gvim is based on the key mappings and capabilities of your GUI environment (window manager) and your terminal emulator(s).
I'm assuming also that your question is referring to 'insert mode' issues so I'm answering accordingly.
One way to map the gvim keys to behave more closely to vim would be remap each offending key and also disable menu keys. Honestly, this is pretty much all you probably need in your .vimrc somewhere (near the bottom[?]):
[edited to add menubar toggles]:
" vim.gtk/gvim: map alt+[hjkl] to normal terminal behaivior
if has("gui_running")
" inoremap == 'ignore any other mappings'
inoremap <M-h> <ESC>h
inoremap <M-j> <ESC>j
inoremap <M-k> <ESC>k
inoremap <M-l> <ESC>l
" uncomment to disable Alt+[menukey] menu keys (i.e. Alt+h for help)
set winaltkeys=no " same as `:set wak=no`
" uncomment to disable menubar
set guioptions -=m
" uncomment to disable icon menubar
set guioptions -=T
" macro to toggle window menu keys
noremap ,wm :call ToggleWindowMenu()<CR>
" function to toggle window menu keys
function ToggleWindowMenu()
if (&winaltkeys == 'yes')
set winaltkeys=no "turn off menu keys
set guioptions -=m "turn off menubar
" uncomment to remove icon menubar
"set guioptions -=T
else
set winaltkeys=yes "turn on menu keys
set guioptions +=m "turn on menubar
" uncomment to add icon menubar
"set guioptions +=T
endif
endfunction
endif
Related
I'm trying to use zsh in the CLion terminal window (changed my shell to /bin/zsh)
But when I try to move around using Ctrl + Left, Ctrl + Right, Ctrl + E... I get the literal characters D, C and Ctrl + E opens the "Recent files" UI
Is there any way around this? I'd like to use the movement keystrokes as I can with bash.
Tempoz,
Check out the offical Zsh Line Editor doc and the Z-Shell Line Editor guide for tons of detail.
To start, you can see your current mappings by typing bindkey at your prompt. There are two start modes: emacs [default], and vi. If you want to use vi mode, add this to your ~/.zshrc
bindkey -v
and source it (or restart your shell, or restart your IDE). Check out how the bindkey output has changed.
If you decide you want to change or augment your key mappings, use bind in your ~/.zshrc to reassign, or add to your key map.
# Example key binding change
# bindkey key-sequence editor-command
bindkey '^Z' vi-kill-line
You will also probably want to fix your $PATH - all the JetBrains products do it incorrectly. See this answer: https://stackoverflow.com/a/51006003/1089228
I recently switch from bash to zsh shell. To be more precise, the oh-my-zsh
Very nice, but the shortcut I most often use; jumping to the beginning/end of the line doesn't work anymore. From the docs it should be
ctrl + A --> beginning
ctrl + E --> end
However, when I do that I get the following
$~> my-command
$~> my-command^A # did a ctrl + A here
Although I see this working by everybody else, on my system something seems to be different. Any suggestions what that might be ?
If you're wondering why this happened: You likely have $EDITOR or $VISUAL set to vi/vim which made zsh default to the vi keymap which doesn't use ctrl+a for moving the caret.
Adding bindkey -e to ~/.zshrc will restore the old behavior (emacs keymap).
you don't have to config the Ctrl+A behavior if you use default keymap (emacs keymap). It does what you are expecting.
However if you set your zle to use vi keymap, you have to define the keybind for vi-beginning-of-line. same for Ctrl+E.
So check which keymap did you set in config. If it was vi, try pressing ESC then ^ and $ should do what you want.
zsh .zshrc
bindkey "^A" vi-beginning-of-line
I'm using iTerm2 on Mac OSX 10.8 with an xterm key binding and zsh.
I'd like zsh to use option left arrow and option right arrow to do the standard Mac bindings of left and right word.
If I hit ctrl-v then option-left and right arrows, where are the two key sequences that print:
^[[1;9D
^[[1;9C
I tried using binding these sequences using bindkey -v, but with no luck.
You can configure iTerm2 to do this like so:
Go to iTerm2 > Preferences > Profiles > Keys
If there is already an ⌥ ← or ⌥ → setting, delete it by selecting it and hitting -.
Add a new shortcut by hitting the + button.
Type ⌥+← in the Keyboard shortcut box.
Select Send Escape Sequence in the Action box.
Enter b for Characters to send.
Click Ok.
Repeat the above procedure for ⌥ →, this time entering f for the Characters to send.
Taken from this great tutorial which describes the whole process in detail and with pictures:
Use ⌥ ← and ⌥ → to jump forwards / backwards words in iTerm 2, on OS X | Coderwall
Add the following to .zshrc
# Skip forward/back a word with opt-arrow
bindkey '[C' forward-word
bindkey '[D' backward-word
If you're looking to easily add this and a bundle of similar mappings, there's a "Natural Text Editing" preset under Preferences > Profiles > Keys (version 3):
Simply add these to your .zshrc
bindkey "\e[1;3D" backward-word # ⌥←
bindkey "\e[1;3C" forward-word # ⌥→
bindkey "^[[1;9D" beginning-of-line # cmd+←
bindkey "^[[1;9C" end-of-line # cmd+→
It works for me using kitty
Got my first Mac over the weekend, and I'm trying to get adjusted. This line in my vimrc, which worked on my windows, won't work with vim through iTerm
inoremap <S-CR> <Esc>
I'm wanting Shift-Enter to act as Escape in insert mode. I've tried using Enter and Return, but that requires me to use the Fn key on my Macbook, which is just as annoying as the escape key.
I Appreciate the help!
The problem here is with the terminal emulation. Most terminals cannot distinguish between non-printing keys [1] and those keys combined with modifier keys.
However, you can still make the desired combination work if your terminal application has the ability to remap key combinations (as iTerm2, for example, does). Map the terminal application's combination to some Unicode character you'll never use, then map that key in Vim to the desired effect, and you can get around this limitation.
For this example, in iTerm2, open the Keys Preferences pane, add a Global Shortcut key, input shift and return, give it an action of Set Text, and then put ✠ (a Maltese Cross, but you could use any random unlikely-to-be-used Unicode character) as its value. In your .vimrc, add these lines:
" Map ✠ (U+2720) to <Esc> as <S-CR> is mapped to ✠ in iTerm2.
inoremap ✠ <Esc>
Or:
inoremap <S-CR> <Esc>
" Map ✠ (U+2720) to <S-CR>, so we have <S-CR> mapped to ✠ in iTerm2 and
" ✠ mapped back to <S-CR> in Vim.
imap ✠ <S-CR>
Entering <S-CR> in Vim in iTerm2 will now ultimately result in <Esc> in Vim as desired.
[1]: E.g. space, tab, enter, delete, control, alt, escape.
That's because for iTerm <S-CR> is the same as <CR>, type Ctrl+V Return then Ctrl+V Shift+Return and you'll see that the same character is inserted in both cases.
So, when you type <S-CR> Vim gets <CR> and your mapping is not triggered.
MacVim is the equivalent of GVim: a GUI for Vim. You don't run MacVim through iTerm. You either run the GUI version (MacVim.app) OR the CLI version ($ vim).
You can launch the GUI from the CLI but iTerm's settings won't interfere in any way with MacVim's settings.
In MacVim your mapping works perfectly.
As far as I know all or most "terminals" treat ⇧↩ the same as ↩. Maybe you should try another sequence like jj?
I'd like to be able to switch temporarily from emacs mode to vi mode, since vi mode is sometimes better, but I'm usually half-way through typing something before I realize I want to use vi mode.
I don't want to switch permanently to vi mode, because I normally prefer emacs mode on the command line, mostly because it's what I'm used to, and over the years many of the keystrokes have become second nature. (As an editor I generally use emacs in viper mode, so that I can use both vi and emacs keystrokes, since I found myself accidentally using them in vi all the time, and screwing things up, and because in some cases I find vi keystrokes more memorable and handy, and in other cases emacs.)
You can create a toggle since the key bindings are separate between vi mode and emacs mode.
$ set -o emacs
$ bind '"\ee": vi-editing-mode'
$ set -o vi
$ bind '"\ee": emacs-editing-mode'
Now Alt-e (or Esc e) will toggle between modes.
Add this somewhere in your definition for PS1 so you have an indicator in your prompt of which mode you're in. It won't show the change immediately when you toggle modes, but it will update when a new prompt is issued.
$(set -o | grep emacs.*on >/dev/null 2>&1 && echo E || echo V)
Aha! I looked at the readline source, and found out that you can do this:
"\M-v": vi-editing-mode
"\M-e": emacs-editing-mode
There doesn't appear to be a toggle, but that's probably good enough!
For posterity's sake, here's my original answer, which could be useful for people trying to do things for which there is no readline function.
Here's a way you could set it up, clearing the current command line in the process. Not what you want, I know, but maybe it'll help someone else who finds this question. In ~/.inputrc:
"\M-v": "\C-k\C-uset -o vi\C-j" # alt (meta)-v: set vi mode
"\M-e": "\C-k\C-uset -o vi\C-j" # alt (meta)-e: set emacs mode
or to toggle...this should work:
"\M-t": "\C-k\C-u[[ \"$SHELLOPTS\" =~ '\\bemacs\\b' ]] && set -o vi || set -o emacs\C-j"
These are essentially aliases, taken one step farther to map to keys in readline so that you don't have to type an alias name and hit enter.
The following .inputrc lines allow Meta / Alt+E to switch between emacs and vi-insert modes.
Mooshing both j and k simultaneously will take you to vi-command mode.
Note: The only English word with "kj" is "blackjack", no words contain "jk")
set keymap emacs
"\ee": vi-editing-mode
"jk": "\eejk"
"kj": "\eejk"
set keymap vi-insert
"\ee": emacs-editing-mode
"jk": vi-movement-mode
"kj": vi-movement-mode
set keymap vi-command
"\ee": emacs-editing-mode
Note: If you add a binding under keymap emacs to vi-movement-mode to try to switch straight to vi-command mode, the prompt doesn't update if you have show-mode-in-prompt on, hence the above work-around is needed.
I finally found out how to toggle vi and emacs mode with a single key e.g. [alt]+[i] in zsh:
# in the .zshrc
# toggle vi and emacs mode
vi-mode() { set -o vi; }
emacs-mode() { set -o emacs; }
zle -N vi-mode
zle -N emacs-mode
bindkey '\ei' vi-mode # switch to vi "insert" mode
bindkey -M viins 'jk' vi-cmd-mode # (optionally) exit to vi "cmd" mode
bindkey -M viins '\ei' emacs-mode # switch to emacs mode