Skip to content

Instantly share code, notes, and snippets.

@jion
Last active July 12, 2023 15:50
Show Gist options
  • Save jion/a795e579d555550b8157c71c9229228c to your computer and use it in GitHub Desktop.
Save jion/a795e579d555550b8157c71c9229228c to your computer and use it in GitHub Desktop.
Sensitive configuration for relativenumbers mode

Toggleable Relative Numbering for Vim

This Vim configuration snippet introduces a feature that allows users to easily toggle between "relative" and "absolute" line number modes in Vim.

In "relative" line number mode, the lines are numbered relative to the cursor's current position, which can be helpful when making movements or operations that are relative to the current line. In "absolute" line number mode, Vim displays the actual line numbers, just like most other text editors.

The user can toggle between these two modes by pressing a combination of keys (<leader>`). The script remembers the user's preference and applies it to whichever window (a.k.a buffer) the cursor is currently in. This means that if a user prefers relative numbering, as they navigate between different windows, each will switch to relative numbering as they come into focus.

In addition, there are specific rules in place for certain buffer types, like terminal buffers, to disable line numbering altogether.

The configuration is made user-friendly with comprehensive comments and well-structured code, making it easy for anyone to understand and modify according to their preference.

" ######################################
" ###### Relative Number Settings ######
" ######################################
" Start in relativenumber mode by default
set number
set relativenumber
" add a global variable to hold the state (1=relative, 0=absolute)
let g:relative_number = 1
" Autocommand group for handling the relativenumber functionality
augroup numbertoggle
autocmd!
" Don't use relative numbers for terminal or nofile buffers
let buftype_blacklist = ['terminal', 'nofile']
" When we enter a window, check if we should be in relativenumber mode
" We'll check our global variable and the buffer type
autocmd WinEnter * if g:relative_number == 1 && index(buftype_blacklist, &buftype) < 0 | set relativenumber | endif
" When we leave a window, turn off relativenumber mode in it
autocmd WinLeave * set norelativenumber
" For terminal buffers, don't use any numbers, and also disable the signcolumn
autocmd BufWinEnter,WinEnter,TermOpen,FocusGained term://* setlocal norelativenumber
autocmd BufWinEnter,WinEnter,TermOpen,FocusGained term://* setlocal nonumber
autocmd BufWinEnter,WinEnter,TermOpen,FocusGained term://* setlocal signcolumn=no
augroup END
" Keybinding to switch between relative and absolute numbers
" We also update our global variable to remember the mode
nnoremap <silent> <leader>` :if &relativenumber<CR> let g:relative_number = 0<CR> set norelativenumber<CR> else<CR> let g:relative_number = 1<CR> set relativenumber<CR> endif<CR>
" End of Relative Number Settings
" #################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment