Vim: Changing colorscheme in normal mode - vim

I would like to have a different colorscheme when i'm in normal mode/visual mode and switch to my default colorscheme when i'm in my insertion mode. Is it possible ?

Instead of overriding the (built-in) commands, you can also hook into the InsertEnter / InsertLeave autocommands:
autocmd InsertLeave * highlight Normal guibg=grey8
autocmd InsertEnter * highlight Normal guibg=black
This will also cover custom (plugin) mappings that change modes, and it avoids the remapping of <Esc>, which can be problematic.

Thanks but it's for the status line. I found a solution like this in my vimrc :
noremap i :highlight Normal guibg=grey8<cr>i
noremap o :highlight Normal guibg=grey8<cr>o
noremap s :highlight Normal guibg=grey8<cr>s
noremap a :highlight Normal guibg=grey8<cr>a
noremap I :highlight Normal guibg=grey8<cr>I
noremap O :highlight Normal guibg=grey8<cr>O
noremap S :highlight Normal guibg=grey8<cr>S
noremap A :highlight Normal guibg=grey8<cr>A
"You need the next line to change the color back when you hit escape.
inoremap <Esc> <Esc>:highlight Normal guibg=black<cr>

Related

vim re-enter insert mode on CmdWinLeave

I'm trying to map the <C-q> combination to enter/exit the Command Line Window (CmdWin).
But when I leave the CmdWin, I'm not able to return to the insert mode, but vim automatically goes back to the normal mode
In my .vimrc the following snippet is used, but doesn't work.
nmap <C-q> <Esc>:<C-f>
imap <C-q> <Esc><C-q>
au CmdWinLeave * startinsert!
I've also tried with:
au CmdWinLeave * startinsert
au CmdWinLeave * normal
You can map: imap <C-q> <Esc>:q<CR>a, then it will re-enter insert mode when you press <C-q> when you are editing in CmdWin.

Clear search highlight on autocmd BufWrite

I tried most of the suggestions in those three questions:
Get rid of Vim's highlight after searching text
How to get rid of search highlight in Vim
Vim clear last search highlighting
It's mainly the :noh, and it works when I type it manually. I just want it happen on BufWrite, so I tried multiple ways, none of which worked:
function! RemoveHighLight()
:noh
endfunction
autocmd BufWrite * :call RemoveHighLight()
autocmd BufWrite * :noh
autocmd BufWrite * :execute "normal! :noh\<cr>"
Debuging echoms and adebug\<esc> in the function and in the third autocmd show that they execute successfully, just the :noh has no effect.
(Also tried :let #/ = "" which worked but it clears the search pattern, which is not I'm looking for. I just want to get rid of the highlight til pressing n or similar)
Using BufWritePost doesn't have effect, either.
It is a workaround but you can set nohlsearch by autocmd. Then you can add a mapping to set it back by n and N.
au BufWrite * set nohlsearch
nnoremap <silent> n n:set hlsearch<CR>
nnoremap <silent> N N:set hlsearch<CR>
Or maybe better, check if it is already set
au BufWrite * set nohlsearch
nnoremap <silent> n n:call ToggleHlBack()<CR>
nnoremap <silent> N N:call ToggleHlBack()<CR>
function! ToggleHlBack()
if &hlsearch == 'nohlsearch'
set hlsearch
endif
endfunction

remap Enter in normal mode but not in command line

I have remapped the Enter key in normal mode to add a new line:
nnoremap <CR> o<Esc>k
Works great, but now in the command line (entered by q:) this command add new line either, but I would like to leave it in default mode, this is execute the selected command from history.
So the question is, how can I remap a key in normal mode, but not in command line?
This autocommand does what you want:
augroup commandlinewindow
autocmd!
autocmd CmdwinEnter * nnoremap <buffer> <CR> <CR>
augroup END
It's the "command-line window", by the way, not the "command line".
In addition to command-line window you'll almost certainly need this one. For quickfix window:
autocmd BufReadPost quickfix nnoremap <buffer> <CR> <CR>

Vim imap jk <Esc> not working even with escape character

i read this answer about mapping <ESC> key to jk. i tried the answer but nothing worked for me. these are i have tried in my vimrc:
inoremap jk <ESC>, inoremap jk <^[>, inoremap jk ^[, inoremap <special> jk <ESC
are they all wrong? i also tried setting cpo-=< above the mapping definition. and there is no comment after the key
here is my vimrc, the command is on the bottom of the file
im new in vim. thanks!
inoremap is spelled wrong. (I also assume you put each of those on a separate line.) This should work.
inoremap <special> jk <ESC>
<special> forces <ESC> to be act as escape regardless of what cpo is set to.
problem solved. the reason why <esc> doesnt work is because of this function in my vimrc:
if ! has("gui_running")
set ttimeoutlen=10
augroup FastEscape
autocmd!
au InsertEnter * set timeoutlen=0
au InsertLeave * set timeoutlen=1000
augroup END
endif
inoremap jk <ESC> works perfectly after getting rid of above function.
i guess its augroup FastEscape that makes <esc> mapping not working.
For those that are here because of the title, the jk mapping will not work when :set paste is enabled.

How can I activate relative line numbering in (and only in) Vim's visual mode?

I’m trying to get Vim to switch to relative line numbering when I enter visual mode, and back to absolute numbering afterwards. I've noticed there's InsertEnter and InsertLeave autocmd events, which I could use like this:
autocmd InsertEnter :set rnu
autocmd InsertLeave :set nu
Problem is, I can’t seem to find an equivalent for visual mode.
There are no such events for visual mode (yet implemented; you could submit a patch). For entering visual mode, you can simply override the few commands that enter visual mode:
:nnoremap <silent> v v:<C-u>set nonu rnu<CR>gv
:nnoremap <silent> V V:<C-u>set nonu rnu<CR>gv
:nnoremap <silent> <C-v> <C-v>:<C-u>set nonu rnu<CR>gv
The restore of 'number' is more difficult, because apart from explicitly exiting via <Esc>, there are many commands that stop visual mode. Best I can come up with is a trigger on CursorMoved:
vnoremap <Esc> <Esc>:set nu<CR>
:autocmd CursorMoved * if mode() !~# "[vV\<C-v>]" | set nu | endif

Resources