Disable highlighting matching parenthesis in insert mode in Vim - vim

Is there a way to disable the highlight of matching parenthesis in Vim only in insert mode?
Using :NoMatchParen will disable it in both normal and insert mode.
Edit:
I tried using autocmd as suggested in a comment, and I have now in my .vimrc the lines
au InsertEnter * NoMatchParen
au InsertLeave * DoMatchParen
However, with this, when I have two buffers in a split, leaving the insert mode changes the buffer I'm in. Any suggestions?

Related

How do I autocomplete and format brackets in Vim?

I'm working my way through a Java book and as a result, I find myself typing in a lot of curly braces. Considering that I knew how powerful Vim can be, though being far from an expert, I've looked for ways to let it handle the braces for me. I came across the use of autocmds, and I put the following in my .vimrc:
autocmd FileType java inoremap <buffer> { {<CR><CR>}<Up><Tab>. I also tried just a normal inoremap, to no avail.
What I have now works for the highest level block. However, when I get to any block deeper, the <Tab> doesn't seem to execute. Everything else works as expected. But no matter how deep after that, but the cursor always ends up back at the beginning of the method level. I imagine that it's because I told it to add exactly one tab. How can I make the tab count depend on the depth?
Assuming you have some sort of autoindenting set up:
augroup Java
autocmd!
autocmd FileType java inoremap <buffer> { {<CR>}<C-c>O
augroup END
What your version does:
insert the opening brace,
insert a newline,
insert a newline,
insert the closing brace,
move the cursor up to column 1,
insert a tab.
You got it right: since you only insert a single tab this can only get you to the first level of indentation.
What my version does:
insert the opening brace,
insert a newline,
insert the closing brace,
exit insert mode without triggering autocommands,
open a new line above the current line.
:help O respects your autoindenting settings so it will enter insert mode at the right indentation level.
Alternatively, you could modify your version like this:
augroup Java
autocmd!
autocmd FileType java inoremap <buffer> { {<CR><CR>}<C-c>S
augroup END
Where you leave insert mode on the blank line between the braces and do :help S to enter insert mode at the right indentation level.
There are many ways to skin that cat.
I'd suggest letting Vim use its built-in Java mode. If you add
source $VIMRUNTIME/vimrc_example.vim
to your .vimrc, Vim will pick up a load of useful defaults, including automatic filetype detection and formatting rules. You'll find that opening a .java file will give you most of what you want.
And to anticipate your next question, if the tab width isn't what you want, add something like
autocmd FileType java setlocal shiftwidth=4
to the end of your .vimrc.

Toggle absolute and relative numbering in VIM by insert/normal mode

I use the relative line number setting in vim. I would like vim to automatically switch to absolute line numbers when I am in insert mode and back to relative in normal mode. Currently I have tried this code in my vimrc
autocmd InsertEnter * :set number
autocmd InsertLeave * :set relativenumber
Unfortunately this only gets me part of the way there. This toggles on relative numbers the first time I leave insert mode, but will no longer go back to absolute numbers upon entering insert mode again.
First, do a
set number
Then
autocmd InsertEnter * :set norelativenumber
autocmd InsertLeave * :set relativenumber

Vim line numbers, absolute within visual mode and relative within insert

I have line numbers set within my .vimrc.
I'm wanting to switch to relativenumbers when in insert mode, and switch back to regular numbers in visual mode.
I've tried:
autocmd InsertEnter * :set number
autocmd InsertLeave * :set relativenumber
But this isn't working. I'm using Vim7.4.52 in terminal, Ubuntu.
If possible I want to do this automatically, so I haven't got to fiddle with manually entering a command.
number and relativenumber setting are not exclusive anymore, so rather use something like this:
autocmd InsertEnter * :set nonumber relativenumber
autocmd InsertLeave * :set number norelativenumber
Or use one of the many plugins, that do that (sorry, I don't remember which ones do that).

Execute :nohlsearch on InsertEnter

I can do <C-O>:noh<CR> when I'm in insert mode, but it doesn't work when done automatically:
autocmd InsertEnter * :nohlsearch
This works, but it behaves differently:
autocmd InsertEnter * :set nohlsearch
To clarify, what I want is to run :nohlsearch if I enter insert node, but I still want to keep the ability to do /<CR>N to search for another item.
I think, what you want can be accomplished by setting the search register directly:
:autocmd InsertEnter * :let let #/=''
If you want to restore the highlighting when returning from insert mode, you would need to save and restore the pattern, something like this should do it:
:autocmd InsertEnter * :let b:_search=#/|let #/=''
:autocmd InsertLeave * :let #/=get(b:,'_search','')
This saves and restores the current search pattern in the buffer local variable b:_search.
You should write a function calling :nohl and then redraw:
function DisableHL()
nohl
redraw
endfunction
and then autocmd InsertEnter * :call DisableHL()
I think I came up with a smart-ish way of accomplishing exactly the same effect (at least it does exactly what I want and I seem to want what you asked for).
I have this in my vimrc:
Disable highlighting when entering Insert mode
autocmd InsertEnter * set nohlsearch
Re-enable highlighting when pressing any of the nN?/ keys before sending the key, then send the key
for s:k in ['n', 'N', '?', '/']
execute('nnoremap ' . s:k . ' :set hlsearch<cr>' . s:k)
endfor
to hide last search's hightlighting
(I like to be able to quickly disable it without entering insert mode)
nnoremap <silent> <bs> :set nohlsearch<cr>
It basically sets [no]hlsearch on the fly as you use the commands.
I also checked, and at least on my setup it doesn't mess if using nN?/ as normal mode command (for vim's default commands at least) arguments (like dtN to delete until next N), although this might be a concern if you ever remap any of those keys, or if a plugin does it without you noticing it.

Vim: Different line-number color in the different modes

I've just started using Vim and I find myself being confused on which mode I'm currently in. I was wondering if it's possible to change the line number color (which I have displayed by default) in the different modes (command, visual, and insert) so that I can easily identify it.
Vim has an option called showmode that will put a message on the last line if you are not in normal mode. See :help 'showmode' for more info.
If you really want to change the background of the number column while in insert mode, you could do something like the following:
autocmd InsertEnter * highlight LineNr ctermbg=red guibg=red
autocmd InsertLeave * highlight LineNr ctermbg=black guibg=black
Note that this method will break if you are bad and use C-c to exit insert mode instead of <Esc> or <C-[.
Relevant :help tags: autocmd, autocmd-events, highlight, highlight-groups

Resources