Skip to content

Instantly share code, notes, and snippets.

@ur4ltz
Forked from samoshkin/difftool_vimrc.vim
Created February 21, 2021 03:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ur4ltz/89f5cfd647396496fc7bbe8322065236 to your computer and use it in GitHub Desktop.
Save ur4ltz/89f5cfd647396496fc7bbe8322065236 to your computer and use it in GitHub Desktop.
Test vimrc configuration to turn Vim into a difftool
set nocompatible
filetype plugin indent on
set tabstop=2 softtabstop=2 shiftwidth=2 expandtab
set number
set hidden
set splitbelow
set splitright
" Additional <ESC> mappings:
" jk, in INSERT mode
" <C-c>, I'm so used to it after shell environment
inoremap jk <ESC>
noremap <C-C> <ESC>
xnoremap <C-C> <ESC>
" Set <leader> key to <Space>
nnoremap <Space> <Nop>
let mapleader=" "
let maplocalleader=","
call plug#begin('~/.vim/plugged')
Plug 'dracula/vim', { 'as': 'dracula' }
Plug 'airblade/vim-gitgutter'
call plug#end()
if &t_Co >= 256 || has("gui_running")
"let g:dracula_italic=0
let &t_8f="\<Esc>[38;2;%lu;%lu;%lum"
let &t_8b="\<Esc>[48;2;%lu;%lu;%lum"
if (has("termguicolors"))
set termguicolors
endif
endif
augroup aug_color_scheme
au!
autocmd ColorScheme dracula call s:PatchColorScheme()
augroup END
function s:PatchColorScheme()
hi! link DiffChange NONE
hi! clear DiffChange
hi! DiffText term=NONE ctermfg=215 ctermbg=233 cterm=NONE guifg=#FFB86C guibg=#14141a gui=NONE
endfunction
" Apply a particular color scheme
" NOTE: Should go after 'autocmd ColorScheme' customization
set background=dark
colorscheme dracula
set diffopt=vertical,filler,context:3,indent-heuristic,algorithm:patience,internal
" Detect if vim is started as a diff tool (vim -d, vimdiff)
" NOTE: Does not work when you start Vim as usual and enter diff mode using :diffthis
if &diff
let s:is_started_as_vim_diff = 1
syntax off
setlocal nospell
endif
augroup aug_diffs
au!
" Inspect whether some windows are in diff mode, and apply changes for such windows
" Run asynchronously, to ensure '&diff' option is properly set by Vim
au WinEnter,BufEnter * call timer_start(50, 'CheckDiffMode')
augroup END
" In diff mode:
" - Disable syntax highlighting
" - Disable spell checking
function CheckDiffMode(timer)
let curwin = winnr()
" Check each window
for _win in range(1, winnr('$'))
exe "noautocmd " . _win . "wincmd w"
call s:change_option_in_diffmode('b:', 'syntax', 'off')
call s:change_option_in_diffmode('w:', 'spell', 0, 1)
endfor
" Get back to original window
exe "noautocmd " . curwin . "wincmd w"
endfunction
" Detect window or buffer local option is in sync with diff mode
function s:change_option_in_diffmode(scope, option, value, ...)
let isBoolean = get(a:, "1", 0)
let backupVarname = a:scope . "_old_" . a:option
" Entering diff mode
if &diff && !exists(backupVarname)
exe "let " . backupVarname . "=&" . a:option
call s:set_option(a:option, a:value, 1, isBoolean)
endif
" Exiting diff mode
if !&diff && exists(backupVarname)
let oldValue = eval(backupVarname)
call s:set_option(a:option, oldValue, 1, isBoolean)
exe "unlet " . backupVarname
endif
endfunction
function s:set_option(option, value, ...)
let isLocal = get(a:, "1", 0)
let isBoolean = get(a:, "2", 0)
if isBoolean
exe (isLocal ? "setlocal " : "set ") . (a:value ? "" : "no") . a:option
else
exe (isLocal ? "setlocal " : "set ") . a:option . "=" . a:value
endif
endfunction
nnoremap <silent> <leader>q :call <SID>QuitWindow()<CR>
function s:QuitWindow()
if get(s:, 'is_started_as_vim_diff', 0)
qall
return
endif
quit
endfunction
nmap <expr> <Up> &diff ? '[czz' : '<Up>'
nmap <expr> <Down> &diff ? ']czz' : '<Down>'
let g:gitgutter_enabled = 1
set updatetime=1000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment