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

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

Related

In vimscript, how can I highlight the whole text of the buffer when losing focus?

In vimscript, I want to highlight the whole text when losing focus; the following command does not work but moves cursor one line below.
autocmd FocusLost * execute '/.'
Let me know the correct way.
I include my code with what #Ry- answered.
The code dims the whole text of the buffer when losing focus and reverts it in syntax highlight when gaining focus.
augroup dimbuf
autocmd!
autocmd FocusLost * execute 'highlight Search cterm=none guifg=#555555 guibg=NONE
autocmd FocusLost * call matchadd('Search', '.', 0, 1843991912)
autocmd FocusGained * call matchdelete(1843991912)
autocmd FocusGained * execute 'highlight Search none' | execute 'highlight Search cterm=reverse'
augroup END
From :help autocmd-searchpat:
Autocommands do not change the current search patterns. Vim saves the current search patterns before executing autocommands then restores them after the autocommands finish. This means that autocommands do not affect the strings highlighted with the 'hlsearch' option. Within autocommands, you can still use search patterns normally, e.g., with the n command.
If you want an autocommand to set the search pattern, such that it is used after the autocommand finishes, use the :let #/ = command.
The search-highlighting cannot be switched off with :nohlsearch in an autocommand. Use the 'h' flag in the 'shada' option to disable search-highlighting when starting Vim.
So, you can set the last search register directly:
autocmd FocusLost * let #/ = '.'
But if you’re looking for the visual effect, have you considered adding a match instead? It won’t clobber your last search.
augroup blur-highlight
autocmd!
autocmd FocusLost * call matchadd('Search', '.', 0, 1843991912) " random integer
autocmd FocusGained * call matchdelete(1843991912)
augroup END

Disable highlighting matching parenthesis in insert mode in 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?

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).

What something fishy is going on in Vim with relative number?

In Vim, you can set relative numbers
:set relativenumber
You will see the numbers who are subtracting of counting from the line where your cursor is placed. But I don't like the 0 as cursor, so I did :set number
And tadaa, you see the number of the current line where your cursor is.
I set it in my vimrc.
set relativenumber
set number
But I noticed something wrong. When you opened a split window, you see that the number where your cursor is, have jumped back to 0 in the window. When you close the window, you see that the number is still 0 in another buffer.
I tried to fix this in my vimrc, to give the autocmmands when you doing something with windows or buffers:
set relativenumber
set number
autocmd BufWinEnter * set nu
autocmd BufDelete * set nu
autocmd BufCreate * set number
autocmd BufLeave * set number
autocmd BufWinEnter * set nu
autocmd WinEnter * set nu
But nothing of them worked so far. The cursor still jumps back to 0. Can anyone tell me what I'm doing wrong there?
When I am in the window, and I set the number manually, it worked. But why can't I give it to an autocommand?

(g)Vim -> show 4 spaces, but save 2 spaces (tabs)

Is it possible to open files that are indented with 2 spaces, but show me 4 space indentation, and when I make 4 spaces, it saves in a 2 space format?
Edit
It turns out I also need to be able to ensure that it works if the file has a mix of tabs, 2 spaces, and 4 spaces.
Edit 2
So, here is my current solution. I'm having to remap my (originally mapped to :w) so that I can place my cursor back where it was (and give me one "history back" as far as cursor positions when I do a save. Is there a way to do this without affecting cursor position (and not adding the substitution to the history, either)?
function! s:ShimSpaces()
nunmap <C-S>
nmap <C-S> ms``mt:w<Cr>`t`s
augroup SeoTabs
autocmd!
autocmd BufReadPost,BufWritePost * set tabstop=4
autocmd BufReadPost,BufWritePost * %substitute/^ \+/&&/e
autocmd BufReadPost * %substitute/ \+$//e
autocmd BufWritePre * %substitute/^\( \+\)\1/\1/e
autocmd BufWritePre * set tabstop=2
autocmd BufWritePre * retab
augroup END
endfunction
command! -n=0 -bar ShimSpaces :call s:ShimSpaces()
This is the opposite of what was asked here.
The help has an example for a similar use case of different tab widths, see :help retab-example.
Adapting that to doubling / halving spaces:
:augroup AdaptIndent
:autocmd!
:autocmd BufReadPost,BufWritePost * %substitute/^ \+/&&/e
:autocmd BufWritePre * %substitute/^\( \+\)\1/\1/e
:augroup END
With *, this will affect all opened files. To restrict this to certain files, see :help autocmd-patterns.
Edit: With the :augroup wrapping, this can be turned off again via :autocmd! AdaptIndent. This way, you can easily toggle this on / off. For ease of use, I'd put this in a function and define a custom command calling it.

Resources