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.
Related
I found vimrc key remapping of / in vim-better-default plugin and added to my config.
The remapping works as expected, but this remap also causes an issue with jump using the relative number. For example, when I run 5<CR>, I need to type enter once more for it to work and it only jumps for 4 lines. The output in the bottom display is :.,.+4
I tried adding these but it did not work either.
autocmd CmdwinEnter * nnoremap <CR> <CR>
autocmd BufReadPost quickfix nnoremap <CR> <CR>
Thanks,
I'd like to automatically format html and js code when I exit insert mode. Currently I have ctrl f mapped to format the current file in my vimrc:
map <c-f> :call JSBeautify()<cr>
Is there a way I can trigger this command each time I exit insert mode?
Thanks
Try to put this
augroup AuJsBeautify
au!
au InsertLeave * call JsBeautify()
augroup END
in your .vimrc.
To know more about autocommands, read :h 40.3 and :h autocommand.
Or if you prefer mapping, you can just map it on your Esc
inoremap <Esc> <Esc>:call JsBeautify()<cr>
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>
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>
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