Vim autocomplete key bindings for closing & choosing - vim

I have j/k set to scroll in the autocomplete box as posted in this question: Scrolling in vim autocomplete box with jk movement keys
Now I want to map h to close the box without completion, and l to close with completion. Both should stay in insert mode. How can I do this?

inoremap <expr> h ((pumvisible())?("\<C-e>"):("h"))
inoremap <expr> l ((pumvisible())?("\<C-y>"):("l"))

Related

How to prevent Vim from jumping to next line, when auto-completing words using `C-P`?

When auto-completing words in Vim using C-P and pressing Enter to select the first candidate, without first using the arrow-keys to select between candidates, Vim will jump the cursor to the next line.
Conversely, if I first use the arrow-keys to select between candidates, Vim won't jump to the next line.
How can I prevent Vim from jumping to the next line, when selecting first candidate using Vim's auto-completion feature C-P?
When popup (completion) menu is active, you're supposed to use <C-N> <C-P> <C-E> <C-Y> instead of <Down> <Up> <Esc> <CR> respectively (note that all these keys behave a little differently). But if you really like it you can use a mapping:
inoremap <expr><CR> pumvisible() ? "\<C-Y>" : "\<CR>"
See also Improve completion popup menu Vim tip.

Vim mapping Cntrl-i to toggle between normal and insert

inoremap <C-i> <Esc> " cntrl-i to switch to normal
nnoremap <C-i> a "cntrl-i to switch to insert
Tried the following to have cntrl-i toggle between normal and insert modes. However, when I toggle into insert mode it works but pastes everything after a, this happens also when replacing a with i. It fails completely when in insert mode and just inserts a tab.
Alternatively if anybody has better suggestions for a mapping to toggle between both, I'm all ears. I wanted to do caps lock however couldn't find a mapping for it in the vim docs.
Dont put comments after maps:
inoremap <C-i> <Esc>
nnoremap <C-i> i

Arrow key mappings do not function

Well,
One can use <C-w>[hjkl] to move between window, and it's works!
Then I shortcut the horizontal moves like noremap <S-RIGHT> <C-W>l, and it's works!
But, when I try to shortcut the vertical moves, I use noremap <S-UP> <C-W>k and noremap <S-DOWN> <C-W>j, and it doesn't work!
Any suggestions?
EDIT : Assuming than the terminal handles the <S-UP> and <S-DOWN>, which conflict with my vim (or whatever term app) preferences, is there any way to force the terminal to forget this mapping ??

Toggle highlight while searching in Vim

I like highlighting while searching in vim. Here's what I want:
I search for a word with /
Then, all of the results are highlighted. If I press any key other than n or N, I want the highlighting to be toggled off.
If I press n or N again after any number of commands, I want to toggle on the highlighting.
Where do I start? I'm not even sure what to google.
I have this in my .vimrc
nnoremap <CR> :noh<CR>
so that when I'm done seeing the highlighting, I just hit enter to remove it. It stays gone until I hit n or N again.
Note: If you want to keep the functionality of enter, add another <CR> on the end of the command.
I remap control-l (lower case L) so that it clears the search result as well as repaints the screen. This line in .vimrc does it:
nnoremap <silent> <C-l> :nohl<CR><C-l>
You can manually disable the last highlight with nohl.
I will let you know if I can figure out how to automate this.
One method is to setup a toggle mapping. These are some toggle mappings I have in my .vimrc:
let mapleader="\\"
noremap <silent> <Leader>th :set invhls hls ?<CR>
noremap <silent> <Leader>tn :set invnumber number ?<CR>
noremap <silent> <Leader>ts :set invspell spell ?<CR>
noremap <silent> <Leader>tw :set invwrap wrap ?<CR>
To toggle highlighting just type \th for toggle hls. The others are line number, spell checking, line wrapping. The final hls ? will display the new mode.
I prefer this, because to me it is nothing but natural.
Start searching with /<pattern> and once done, simply type <Leader>/ to stop the highlighting.
nnoremap <silent> <Leader>/ :set nohl<CR>

vimdiff and move among left and right pane

I am using vimdiff for the first time. Online I found written that to move from the left pane you use CTRL + w + Left or right arrow
This does not work for me. But I see that if I press just CTRL + w and press w for a sec and let it go, it switches pane after ~500ms.
Is this how it is supposed to work? Am I doing something wrong?
Ctrl+w and right and left arrow can be used to move between any split windows on vim, not only vimdiff splits.
These keys do work here on cygwin; also, Ctrl+w w also moves to the next window, but without the delay you mentioned.
It is possible that you have mapped these keys in your .vimrc or via some vim plugin. You can check this with :map w, :map <left> and :map <right>.
As moving between windows is something that you use often, you may consider using the following mappings:
nnoremap <C-J> <C-W>j
nnoremap <C-K> <C-W>k
nnoremap <C-H> <C-W>h
nnoremap <C-L> <C-W>l
Then you can use Ctrl+h and Ctrl+l to move left and right, without moving your hands from the home row. And the nnoremap will ensure that these works despite of any other mappings that you may have.
Press Ctrl + W and then (after releasing Ctrl + W) press the arrow keys to change the pane.
It is very useful to use set mouse=a in your .vimrc file. It gives you possibility to switch between windows using mouse. Additionally you can resize windows using it.
If you prefer to use keyboard I have also mapped arrow keys in .vimrc in this way:
map <C-Left> <C-W>j
map <C-Down> <C-W>k
map <C-Up> <C-W>h
map <C-Right> <C-W>l
To move among left and right pane, Press ctrl+w and then ctrl+r. This is both left and right vice-versa.
You can also use :wincmd w for next window, and :wincmd W for previous window.
The :wincmd is especially useful when ctrl+w is captured by the environment. For example see: https://stackoverflow.com/a/73749587/811335

Resources