I've enabled the following mappings in my init.vim:
tnoremap <Esc> <C-\><C-n>
tnoremap <C-h> <C-\><C-n><C-w>h
tnoremap <C-j> <C-\><C-n><C-w>j
tnoremap <C-k> <C-\><C-n><C-w>k
tnoremap <C-l> <C-\><C-n><C-w>l
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l
These greatly improve windows navigation in Vim.
However, I've noticed that C-h does not work as expected when executed in terminal buffer. Trying this on a usual terminal session results in Backspace action. So probably it seems to be one of these alternative key combinations, like C-i for Tab or C-[ for Esc. But is there any way to make C-h work in Neovim's terminal session as per my bindings?
Thanks!
This issue has already been extensively debated here. Original Vim does not rely on terminfo and includes its own patch for correct handling of C-h sequences. Neovim does look at terminfo though.
Briefly, the fix is executing these commands in the shell:
infocmp $TERM | sed 's/kbs=^[hH]/kbs=\\177/' > $TERM.ti
tic $TERM.ti
Related
I'm trying to remap C-any arrow key in Neovim on my Mac to switch windows.
My .config/nvim/init.vim has the following:
" Basic key mappings and controls
" -------------------------------
" Simpler pane moving.
nnoremap <C-J> <C-W><C-J>
nnoremap <C-K> <C-W><C-K>
nnoremap <C-L> <C-W><C-L>
nnoremap <C-H> <C-W><C-H>
nnoremap <C-Down> <C-W><C-J>
nnoremap <C-Up> <C-W><C-K>
nnoremap <C-Right> <C-W><C-L>
nnoremap <C-Left> <C-W><C-H>
However when I restart nvim, the C-J/K/L/H combos work but the C-arrow key combos do not work. What else do I have to do on my Mac to get such remappings to work when I'm working in OSX's default terminal app, or tmux using zsh?
I tried to map <Alt+D> to <Ctrl+D> by adding the below line to .vimrc, but it doesn't work. I checked the .vimrc is loaded by Vim.
map <Alt-D> <C-D>
Is there any error in this mapping?
To Mac users out there: for mapping ALT+hjkl, use instead the real character generated (find out which character using the combination while in INSERT mode), for example with my keyboard I get:
<ALT+j> ==> ª
<ALT+k> ==> º
and so on.
Solution found here on StackOverflow.
I used this to move lines up and down with ALT+k\j, using this on my .vimrc:
nnoremap ª :m .+1<CR>==
nnoremap º :m .-2<CR>==
inoremap ª <Esc>:m .+1<CR>==gi
inoremap º <Esc>:m .-2<CR>==gi
vnoremap ª :m '>+1<CR>gv=gv
vnoremap º :m '<-2<CR>gv=gv
as explained here.
Hope it's useful, enjoy Vim :)
ADDENDUM BY Dylan_Larkin (2019): For this to work on a Mac, "Use Option as Meta Key" must be turned OFF in Terminal->Preferences->Keyboard
UPDATE 09/2021
I recently switched from a "British" keyboard to "ABC - Extended" and noticed this configuration doesn't work as expected.
As an alternative, I mapped the <up> and <down> keys to do the same operation (which, I guess, also solves most of the complexity explained in other answers of this very question):
nnoremap <down> :m .+1<CR>==
nnoremap <up> :m .-2<CR>==
inoremap <down> <Esc>:m .+1<CR>==gi
inoremap <up> <Esc>:m .-2<CR>==gi
vnoremap <down> :m '>+1<CR>gv=gv
vnoremap <up> :m '<-2<CR>gv=gv
This is also a great way for beginners to rewire the habit of using the arrows and instead learn the much more efficient Vim motion way to move around the code. ;)
You can complete your transition mapping <left> and <right> to quickly move between tabs with:
nnoremap <left> gT
nnoremap <right> gt
Or whatever you fancy (even a brutal <NOP>, like I did at the beginning of my journey).
:help key-notation describes what format needs to be used to map different keys. In the case of alt, you can use either <A- or <M-. So your mapping would be
map <M-d> <C-d>
I'd also recommend using the nore variant of :map (e.g., noremap) unless you explicitly want to allow the right-hand side to be re-evaluated for mappings.
I'm not sure is "possible" anymore. Please read the update below.
Yes, you can even in terminal vim, but there's no real catch all answer. You basically have to follow two steps:
Make sure the <M-d> notation exists, and map exactly what your terminal inputs (^[ is the escape character):
$ cat
^[d
$
" in your .vimrc
execute "set <M-d>=\ed"
" you have to use double quotes!
Map something to your newly "created" combination:
noremap <M-d> :echo "m-d works!"<cr>
Understanding how it works, you can expand this "trick" to other "strange" combinations, for instance, I'm using termite, and vim doesn't recognize <S-F1>, using cat I get ^[[1;2P. Then, in my vimrc I do: execute "set <S-F1>=\e[1;2P", and then I can map it to anything.
Note: I don't know why, but for some people using \<Esc> works instead of \e.
Update (february 2016)
Depending on the terminfo your terminal runs, maybe you could... in most terminals, "alt + h", for example, is mapped to ^[h, which is: "escape + h". So it could overwrite keys. I've just tried (again) and it seems to work, but I believe it's a very buggy and error prone implementation.
Nevertheless, for the brave enough, here's an experimental plugin:
https://github.com/vim-utils/vim-alt-mappings
https://github.com/drmikehenry/vim-fixkey
Map Alt Key in Vim on Mac OSx:
Start by viewing the key code your terminal is sending to vim:
$ sed -n l
^[[1;9D
In the above example, I ran the command and pressed Alt + Left.
The ^[[1;9D is the escaped sequence being sent to vim, so we can user that for our mapping.
map <Esc>[1;9D
Use:
map <A-D> <C-D>
See :help key-notation.
My Terminal would produce ^[x commands (e.g. for alt-x). What got it to work inside Vim was this small script from vim.wikia.com:
for i in range(97,122)
let c = nr2char(i)
exec "map \e".c." <M-".c.">"
exec "map! \e".c." <M-".c.">"
endfor
Add to .vimrc to fix all alt key mappings.
as a follow up to Bruno's answer for Mac users, try making sure your option key is mapped to Esc+.
This will give you the "normal" behavior of the option (A) key in Vim.
For example, in iterm2, this option can be found under Preferences > Profiles > Keys:
Your terminal might not transmit "properly" the Alt-D. You can use C-V to actually get the actual escape sequence send to Vim and use it to create your mapping. Ie, edit your .vimrc
and replace the actual by typing the following sequence "C-V Alt-D" so you'll have the correct escape sequence in your vimrc. That won't work if your terminal doesn't send anything to vim.
Find out key mapping by putting following command in your vim editor
:help key-notation
It will display all the key mapping.
In my ubuntu system for Alt it is <M-...>. It is possible for your version mapping might be different. If you too have same mapping then following should work.
map <M-D> <C-D>
Hello after no good solution after years of testing all the above on mac, I kept searching.
Here is my solution:
To create a combination keystroke including Alt you have to declare the combination in the preference > keyboard and use that combination in the vim setup file (check use option as meta key).
The output must be an unusual character (no a for example) so that you're not overriding a regular character.
In the example below you should be able to quite vim with ALT-Up.
vim setting:
mac setting:
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've installed the latest vim using homebrew and also installed mac-vim from the google code homepage.
in mac-vim everything works fine. but when I run vim in terminal.app in mac and go to insert mode I'll get A B C D for arrow keys which is extremely annoying.
I googled it and tried all the solutions but nothing is working for me!
it gets intresting when I run vim with vim -u NONE -U NONE -N then the arrow keys start to function normally in insert mode.
my vimrc files are a clone of janus (from carlhuda)
do you have the same problem in terminal? is there a way to fix it in terminal.app?
Just create an empty ".vimrc file in the home directory of the remote machine. It started working fine for me.
touch ~/.vimrc
You need to set no compatible mode in the vim
Try following command in normal mode
:set nocompatible
This works for me:
map ^[[A <up>
map ^[[B <down>
map ^[[C <right>
map ^[[D <left>
To type the sequence for each arrow key, you need to press ctrl-v, then the arrow key.
For example, to type the first line:
m, a, p, space, ctrl-v, ←, space, <, u, p, >
This also might work:
set t_ku=^[[A
set t_kd=^[[B
set t_kr=^[[C
set t_kl=^[[D
More information is available here.
I'm on a MacBook Pro with OSx El Captain (version 10.11.1) and was having the same problem after updating my Vim with Homebrew.
My Vim version is 7.4.1063.
I was having the problem with the Terminal app as well as the iTerm app (build 2.1.4).
To solve this problem, I followed some of the instructions in the previous answers from #devsathish and #Kailash. The steps I followed were:
1 - Create a .vimrc file with touch ~/.vimrc
2 - Add the following to it:
set nocompatible
set backspace=indent,eol,start
Now the arrow keys and the delete/backspace keys work as expected. I didn't have to change my $TERM env variable as suggested in some of the previous answers.
I hope this helps others with the same problem.
I'm using iTerm2 with spf13-vim, and have same problem today.
It caused by removing plugin vim-autoclose. This solution fixed it.
Just add following lines into your .vimrc
if &term[:4] == "xterm" || &term[:5] == 'screen' || &term[:3] == 'rxvt'
inoremap <silent> <C-[>OC <RIGHT>
endif
This is usually caused by the wrong $TERM environment variable. Not sure which ones are supported on your system but you can try with "linux" or "vt320":
export TERM=linux
None of the above worked for me. Running vim in blank state with vim -u NONE -U NONE -N made it work ok, and because I had not installed any plugins, I knew problem is in my vimrc. So I started commenting out sections from it, and at one moment problem disappeared.
The culprit was this line:
inoremap
Even though it was recommended in a very well written course (http://learnvimscriptthehardway.stevelosh.com/chapters/10.html), it broke the arrow keys on Mac OS X 10.10. Basically, you shouldn't overwrite esc, as the other SO answer explains: How to disable Esc and cursor keys in vim
To make sure that my Terminal is working great with Vim 7.3 in Show Leopard, I have done the following:
Installed the latest Vim via Homebrew.
Set path to enable the latest Vim. E.g.: export PATH=/usr/local/bin:$PATH
Added export TERM=linux to my .bashrc file.
Created a .vimrc file with some options (formerly linked here, link went dead.)
When so is done, everything should work. However, I have experienced that some text don't seem to remove, like its protected until I either dd or x. Has anyone experienced something similar? Also, please share comments on my small guide above.
I have used following code in my .vimrc file, and it has resolved my problem with arrow keys.
...
...
" Use Vim settings, rather than Vi settings (much better!).
" This must be first, because it changes other options as a side effect.
set nocompatible
" allow backspacing over everything in insert mode
set backspace=indent,eol,start
...
...
" CTRL-U in insert mode deletes a lot. Use CTRL-G u to first break undo,
" so that you can undo CTRL-U after inserting a line break.
inoremap <C-U> <C-G>u<C-U>
" In many terminal emulators the mouse works just fine, thus enable it.
if has('mouse')
set mouse=a
endif
...
...
I have posted my complete .vimrc file Here. Hope it helps !
This is what worked for me (based on initial solution from #Phoenix above)
imap ^[OA <ESC>kli
imap ^[OB <ESC>jli
imap ^[OC <ESC>lli
imap ^[OD <ESC>hli
for each
^[
type the sequence press ctrl-v, then ESC
I've had a similar problem with another plugin. I solved it by hardcoding these alternative mappings in the script itself, the first line of each pair was problematic, the second line is the fix:
VIM::command "#{map} <Right> :call <SID>#{prefix}KeyPressed(9)<CR>"
VIM::command "#{map} ^[OC :call <SID>#{prefix}KeyPressed(9)<CR>"
VIM::command "#{map} <Left> :call <SID>#{prefix}KeyPressed(23)<CR>"
VIM::command "#{map} ^[OD :call <SID>#{prefix}KeyPressed(23)<CR>"
VIM::command "#{map} <Down> :call <SID>#{prefix}KeyPressed(14)<CR>"
VIM::command "#{map} ^[OB :call <SID>#{prefix}KeyPressed(14)<CR>"
VIM::command "#{map} <Up> :call <SID>#{prefix}KeyPressed(16)<CR>"
VIM::command "#{map} ^[OA :call <SID>#{prefix}KeyPressed(16)<CR>"
^[ is obtained by hitting <C-v><Esc>.
Maybe you can try something like:
map <Right> ^[OC
I actually resolved this on OS X Mavericks (10.9) by removing set noesckeys from ~/.vimrc
I've run into this problem as well. It has multiple causes, but one of them is vim using vi rather than vim behavior by default. Surprisingly, this can be triggered by the location of your vimrc file.
For example, if you delete your .vimrc from your home directory and move the contents to the system-wide vimrc file, vim will start using vi defaults. With the Mac Terminal, this will give you ABCD from arrows in insert mode. Add "set nocompatible" to the system-wide vimrc, which will force vim to use vim defaults, and the arrows will work again.
I seem to have something odd with either my Mac 10.6 terminal or my .vimrc.
When I type backspace on my laptop's keyboard, it only works when the cursor is at the end of the line. Trying to delete from within a line does nothing. MacVim operates normally. Google hasn't helped because I can't even figure out what to call this behavior.
All other backspace commands in my Terminal work as expected, so I am leaning towards it being Vim specific.
Here's the output of my ~/.vimrc 's mappings, I can't see anything that would make Vim in the terminal operate this way:
cflewis#coral-reef ~> egrep ".*map.*" ~/.vimrc
"inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"
let mapleader = ","
map Q gq
nmap <silent> <leader>s :set nolist!<CR>
" extended '%' mapping for if/then/else/end etc
map <S-Insert> <MiddleMouse>
map! <S-Insert> <MiddleMouse>
nmap <silent> <C-N> :silent noh<CR>
nmap <C-E> :b#<CR>
nmap <C-P> :NERDTreeToggle<CR>
nmap <leader>p :NERDTreeFind<CR>
nmap <leader>/ :call NERDComment(0, "invert")<cr>
vmap <leader>/ :call NERDComment(0, "invert")<cr>
nmap <leader>t :TlistToggle<CR>
nmap <leader>e :e **/
nmap <Leader>b :MiniBufExplorer<cr>
nmap <Leader>sh :ConqueSplit bash<cr>
nmap <Leader>r :ConqueSplit
" map ,y to show the yankring
nmap <leader>y :YRShow<cr>
imap <silent> <Down> <C-o>gj
imap <silent> <Up> <C-o>gk
nmap <silent> <Down> gj
nmap <silent> <Up> gk
cmap w!! %!sudo tee > /dev/null %
inoremap jj <Esc>
nnoremap JJJJ <Nop>
Any ideas would be appreciated. I tried flipping the delete key to send ^H or ^?, to no difference.
Most likely, the "problem" you're seeing is that you can't delete anything that was not typed during your current insert mode session. This is due to the default setting for the 'backspace' option. Adding set backspace=indent,eol,start to your ~/.vimrc is the behavior that you probably want.
This is the only explicit backspace mapping I have in my config. I do not know if it will help for your problem, but it might be worth a try?
" allow backspacing over everything in insert mode
set backspace=indent,eol,start