" Copyright (c) 2023 Ivan Bityutskiy " " Permission to use, copy, modify, and distribute this software for any " purpose with or without fee is hereby granted, provided that the above " copyright notice and this permission notice appear in all copies. " " THE SOFTWARE IS PROVIDED 'AS IS' AND THE AUTHOR DISCLAIMS ALL WARRANTIES " WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF " MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR " ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES " WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN " ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF " OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. " normal mode commands for file type .vim: " zM - folds all markers " zR - unfolds all markers " za - while inside maker, folds/unfolds curren marker " zd - remove fold under the cursor (also deletes marker characters) " zE - remove all folds in a current buffer (also deletes marker characters) " user settings " allow features not present in vi set nocompatible " milliseconds to wait for next key in a sequence set ttimeoutlen=50 " set spacebar to be the leader key in normal mode noremap let mapleader = "\" " the terminal has 256 colors set t_Co=256 " disable bell set noerrorbells " disable visual bell set novisualbell " light colorscheme that's easy on eyes {{{ colorscheme ivanb if &diff colorscheme ivanb endif " }}} " allow switching beetween buffers without saving first set hidden " set undo, swap, backup directories {{{ let s:my_home_dir = expand('$HOME') if has('win32') if !isdirectory(s:my_home_dir . '\vimfiles\undodir') call mkdir(s:my_home_dir . '\vimfiles\undodir', "p") endif let &undofile = 1 let &undodir = s:my_home_dir . '\vimfiles\undodir//' if !isdirectory(s:my_home_dir . '\vimfiles\swap') call mkdir(s:my_home_dir . '\vimfiles\swap', "p") endif let &directory = s:my_home_dir . '\vimfiles\swap//' if !isdirectory(s:my_home_dir . '\vimfiles\backupdir') call mkdir(s:my_home_dir . '\vimfiles\backupdir', "p") endif let &backupdir = s:my_home_dir . '\vimfiles\backupdir//' else if !isdirectory(s:my_home_dir . '/.vim/undodir') call mkdir(s:my_home_dir . '/.vim/undodir', "p") endif let &undofile = 1 let &undodir = s:my_home_dir . '/.vim/undodir//' if !isdirectory(s:my_home_dir . '/.vim/swap') call mkdir(s:my_home_dir . '/.vim/swap', "p") endif let &directory = s:my_home_dir . '/.vim/swap//' if !isdirectory(s:my_home_dir . '/.vim/backupdir') call mkdir(s:my_home_dir . '/.vim/backupdir', "p") endif let &backupdir = s:my_home_dir . '/.vim/backupdir//' endif " }}} " save backups of files in backupdir set writebackup set backup " make vim work with the 'crontab -e' command set backupskip+=/var/spool/cron/* " skip backup for 'netrwhist' file let &backupskip = &backupskip . ',' . expand('~/.vim/.netrwhist') " use swapfile set swapfile " use undofile set undofile " in Terminal position the cursor, Visually select, scroll with the mouse. {{{ " only xterm, (and st) can grab the mouse events when using the shift key, for " other terminals use ':', select text and press Esc. if has('mouse') if &term =~? '\v[xs]t.*' set mouse=a else set mouse=nvi endif endif " }}} " switch off blinking of the cursor: let &guicursor = "a:blinkon0" " language and font settings {{{ set encoding=utf-8 " to make vim understand UTF-16 LE and such; utf-16 is UTF-16 BE " if working with BOMless UTF 16 files, move utf-16le or utf-16 before ucs-bom " set fileencodings=ucs-bom,utf-16le,utf-16,utf-8,default,latin1,cp1251 " english Menu and UI set langmenu=en_US.UTF-8 language en_US.UTF-8 if has("gui_running") " set guifont=Liberation_Mono:h16:cRUSSIAN:qDRAFT " set guifont=Monospace\ 16 set guifont=Liberation\ Mono\ 16 " remove menu and scrollbars from gui " set guioptions-=mrL " set guioptions=aegiT set guioptions=aegi " gvim window size set lines=30 set columns=88 if has('win32') set guifont=Liberation_Mono:h16:cRUSSIAN:qDRAFT " remove tearoff menu in windows gui " set guioptions-=t " set guioptions=egT set guioptions=eg endif endif " spell checking languages set spelllang=en,ru " }}} " syntax filetype plugin indent on " syntax on syntax enable " confirm closing unsaved files set confirm " matchit makes % normal command work better packadd! matchit " indentation and spacing {{{ " remember indentation for new line set autoindent " replace tabs with spaces set expandtab " 1 tab == 2 spaces set tabstop=2 " 2 spaces for autoindent set shiftwidth=2 " delete 2 spaces in indent with single backspace set softtabstop=2 " the lenght of strings in letters " set textwidth=132 set textwidth=140 " }}} " fix backspace on most terminals set backspace=indent,eol,start " quality-of-life improvements to Vim: {{{ " disable bell set belloff=all " verbose cscope output set cscopeverbose " C-N in insert mode for autocompletion set complete-=i " commands history set history=999 " C-A, C-X to increment/decrement numbers set nrformats-=octal " do not carry options between sessions set sessionoptions-=options " avoid Hit-Enter prompts set shortmess=atI " fast tty connection set ttyfast " do not redraw screen when using macros, registers, etc. " tremendously increase performance of macro commands set lazyredraw " }}} " load all plugins " packloadall " load help files for all plugins " silent! helptags ALL " folding options {{{ set foldenable " fold based on indentation in code set foldmethod=indent set foldignore= " visualize folds set foldcolumn=1 " limit the amount of nested folds set foldnestmax=10 " open 10 levels of nested folds when file is read into a buffer set foldlevelstart=99 " }}} " enable enhanced tab autocompletion set wildmenu set wildmode=list:longest,full " line numbers set number set relativenumber " search settings set hlsearch set incsearch " set ignorecase " info at the bottom set ruler " pretty word-wrap set linebreak " pretty long lines set display+=lastline " custom commands {{{ " function to load the template for current file type, if it exists. " 'abort' causes a function to exit on 1st error, otherwise the function " will continue to execute the code after an error being encountered. function! s:MyLoadVimTemplate() abort let l:templateFile = expand("~/Additional/templatesVim/") . &filetype . ".txt" if filereadable(l:templateFile) execute "0r " . l:templateFile execute "normal! zR" endif endfunction nnoremap tt :call MyLoadVimTemplate() " difference between current buffer and original file if !exists(":DiffOrig") command DiffOrig vert new | set bt=nofile | r ++edit # | 0d_ | diffthis | wincmd p | diffthis endif " clear all registers command! ClearRegisters for ltr in range(34, 122) | silent! call setreg(nr2char(ltr), []) | endfor " }}} " augroup my_vimrc {{{ augroup my_vimrc " clear previous autocmd commands autocmd! " unfold markers after opening a file: autocmd BufRead * normal! zR " when opening a document, return to the last cursor position autocmd BufReadPost * \ if line("'\"") >= 1 && line("'\"") <= line("$") | \ execute "normal! g`\"" | \ endif " don't write swapfile on most commonly used directories for NFS mounts or USB sticks autocmd BufNewFile,BufReadPre /media/*,/run/media/*,/mnt/* set directory=~/tmp,/var/tmp,/tmp augroup END " }}} " don't show relative numbers in Insert mode {{{ augroup insert_relative_nums autocmd! autocmd InsertEnter * setlocal norelativenumber autocmd InsertLeave * setlocal relativenumber augroup END " }}} " c file settings {{{ augroup filetype_c autocmd! autocmd FileType c setlocal foldignore=# augroup END " }}} " python file settings {{{ augroup filetype_python autocmd! autocmd FileType python setlocal tabstop=4 shiftwidth=4 softtabstop=4 augroup END " }}} " rust file settings {{{ augroup filetype_rust autocmd! autocmd FileType rust setlocal tabstop=4 shiftwidth=4 softtabstop=4 augroup END " }}} " vimscript file settings {{{ augroup filetype_vim autocmd! autocmd FileType vim setlocal foldmethod=marker augroup END " }}} " remaps {{{ " repeat the last substitute with options nnoremap & :&& xnoremap & :&& " c18 multi-line comments nnoremap m a/* */ " m4 utf-8 quotes set matchpairs+=☾:☽ nnoremap q Adivert(`-1')changequote(`☾', `☽') nnoremap b a☾☽ nnoremap B a☾☾☽☽ nnoremap d Adivert(☾0☽)dnl nnoremap D Adivert(☾-1☽) inoremap ☾ inoremap ☽ inoreabbrev dfn1_ define(☾%☽, ☾;☽)F%s:call getchar() inoreabbrev dfn2_ define(☾%☽, ☾☾;☽☽)F%s:call getchar() inoreabbrev ife1_ ifelse(☾%☽, ☾;☽, ☾;☽, ☾;☽)F%s:call getchar() inoreabbrev ife2_ ifelse(☾%☽, ☾;☽, ☾☾;☽☽, ☾;☽)F%s:call getchar() " search visually selected text with * # function! s:VisSrch(cmdtype) abort let l:temp = @s normal! gv"sy let @/ = '\V' . substitute(escape(@s, a:cmdtype.'\'), '\n', '\\n', 'g') let @s = l:temp endfunction xnoremap * :call VisSrch('/')/=@/ xnoremap # :call VisSrch('?')?=@/ " traverse Vim's lists nnoremap [b :bprevious nnoremap ]b :bnext nnoremap [B :bfirst nnoremap ]B :blast " show whitespace characters set listchars=eol:$,tab:>-,trail:~,extends:>,precedes:<,nbsp:+ nnoremap :set list! inoremap :set list! cnoremap :set list! " open/close Lex file explorer nnoremap :Lex2j:vertical resize 18 inoremap :Lex2j:vertical resize 18 cnoremap :Lex2j:vertical resize 18 " toggle highlighting of color column function! s:CColToggle() abort if &colorcolumn let &colorcolumn = 0 else let &colorcolumn = 80 endif endfunction nnoremap :call CColToggle() inoremap :call CColToggle() cnoremap :call CColToggle() " toggle highlighting of cursor line function! s:ClToggle() abort if &cursorline let &cursorline = 0 else let &cursorline = 1 endif endfunction nnoremap h :call ClToggle() " reverse highlighted text vnoremap r c:set revins":set norevins " }}} " insert mode remaps: {{{ " Alt-Space to move one letter to the right in insert mode inoremap la " Ctrl-D-Ctrl-D to insert current date inoremap =strftime("%d.%m.%Y") " Ctrl-T-Ctrl-T to insert current time inoremap =strftime("%H:%M") " Ctrl-D-Ctrl-T to insert current date and time inoremap =strftime("%Y-%m-%dT%T") " be able to undo Ctrl-U in insert mode inoremap u " }}}