Skip to content

Instantly share code, notes, and snippets.

@malkomalko
Last active September 29, 2021 15:22
Show Gist options
  • Save malkomalko/d6397fbb9374ea5b26f46ed12992b21a to your computer and use it in GitHub Desktop.
Save malkomalko/d6397fbb9374ea5b26f46ed12992b21a to your computer and use it in GitHub Desktop.
" Section: init {{{1
set nocompatible
filetype off
filetype indent plugin on
scriptencoding utf-8
syntax on
runtime macros/matchit.vim
" }}}
" Section: settings {{{1
set autoindent
set autoread
set backspace=indent,eol,start
set completeopt=longest,menuone
silent! set cryptmethod=blowfish
set directory=/tmp/
set encoding=utf-8
set equalalways
set expandtab
set formatoptions=qrn1
set hidden
set laststatus=2
set lazyredraw
set listchars=tab:▸\ ,eol:¬,trail:·
set matchpairs+=<:>
set nobackup
set nocursorline
set nolist
set nonumber
set nosol
set noswapfile
set nowrap
set nrformats-=octal
set pastetoggle=<f12>
set scrolloff=0
set shellpipe=>
set shiftround
set shiftwidth=2
set shortmess+=I
set showtabline=2
set noshowmatch
set si
set softtabstop=2
set splitbelow splitright
set synmaxcol=128
set t_Co=256
set tabstop=2
set tags+=tags
set textwidth=80
set ttimeout
set ttimeoutlen=50
set ttyfast
set ttyscroll=3
set viminfo='100,<0,:1000,/250,@0,s10,h
set whichwrap+=<,>,h,l,[,]
set wildmenu
set wildmode=list:longest,full
if has('folding')
set foldcolumn=0
set foldlevel=20
set foldmethod=indent
set foldnestmax=20
set foldignore=
endif
set gdefault
set hlsearch
set ignorecase
set incsearch
set smartcase
" wildignore
set wig+=*.bmp,*.gif,*.ico,*.jpg,*.jpeg,*.png,*.svg
set wig+=*.pdf,*.psd
" }}}
" Section: config {{{1
let mapleader=','
let g:lasttab=1
let g:netrw_banner=0
" }}}
" Section: functions {{{1
fun! CloseHiddenBuffers()
let tpbl=[]
call map(range(1, tabpagenr('$')), 'extend(tpbl, tabpagebuflist(v:val))')
for buf in filter(range(1, bufnr('$')), 'bufexists(v:val) && index(tpbl, v:val) == -1')
silent execute 'bwipeout' buf
endfor
endfun
fun! CloseOtherBuffers()
:wa!
if &filetype ==# 'qf'
return
end
for b in range(1, bufnr('$'))
if (b != bufnr('%'))
silent! exe 'bd ' . b
endif
endfor
endfun
fun! CloseTab()
if tabpagenr() ==# 1 || tabpagenr('$') ==# 1
return
endif
tabc
if exists('g:prevtab')
exe "tabn ".g:prevtab
unlet g:prevtab
endif
endfun
fun! ListMappings(...)
if a:0 > 0
let mapcmd='verbose ' . a:1
else
let mapcmd='verbose map <leader>'
endif
let _a=@a
sil! redir @a
sil! exe mapcmd
sil! redir END
sil! tabnew
sil! put! a
sil! g/^s*$/d " delete empty lines
sil! g/$/j " join every other line because of verbose
sil! normal ggVg
sil! sort
sil! exe "norm! \<esc>"
sil! set nomodifiable
let @a=_a
endfun
fun! Normal(cmd)
silent execute 'normal! ' . a:cmd
endfun
com! -nargs=1 Normal :call Normal(<args>)
fun! OpenTab()
let g:prevtab = tabpagenr()
Normal("mt")
tabe %
Normal("`t")
Normal("zz")
endfun
fun! SetupNetrw()
nunmap <buffer>qb
nunmap <buffer>qf
nunmap <buffer>qF
nnoremap <silent><buffer>q :bw<cr>
setl bufhidden=wipe
endfun
fun! UpdateWildIgnore(filename)
if filereadable(a:filename)
let igstring = ''
for oline in readfile(a:filename)
let line = substitute(oline, '\s|\n|\r', '', "g")
if line =~ '^#' | con | endif
if line == '' | con | endif
if line =~ '^!' | con | endif
if line =~ '^/.*/$' | let igstring .= "," . getcwd() . line . "*" | con | endif
if line =~ '/$' | let igstring .= "," . line . "*" | con | endif
if line =~ '^/' | let igstring .= "," . getcwd() . line | con | endif
let igstring .= "," . line
endfor
let execstring = "set wig+=" . substitute(igstring, '^,', '', "g")
execute execstring
endif
endfun
call UpdateWildIgnore('.gitignore')
call UpdateWildIgnore($HOME . '/.ignore')
call UpdateWildIgnore('.ignore')
call UpdateWildIgnore($HOME . '/.ignore')
" }}}
" Section: mappings {{{1
" abbreviations
cabbr <expr> %% fnameescape(expand('%:p:h'))
" buffers
nnoremap <silent><leader>bb :b#<cr>
nnoremap <silent><leader>bd :bd<cr>
nnoremap <leader>be :ls<cr>:b<space>
nnoremap <leader>bl :ls!<cr>
nnoremap <silent><leader>bq :bw<cr>
nnoremap <silent><leader>qh :call CloseHiddenBuffers()<cr>
nnoremap <silent><leader>qo :call CloseOtherBuffers()<cr>
nnoremap <silent><leader>qq :qa!<cr>
nnoremap <silent><c-@> :b#<cr>
nnoremap <silent><c-space> :b#<cr>
" escape
inoremap <c-g> <esc>
noremap <c-g> <c-c>
noremap! <c-g> <c-c>
snoremap <c-g> <esc>
inoremap jj <esc>
" file search
nnoremap - :Ex<cr>
nnoremap <leader>fa :e **/*
nnoremap <leader>fA :e %%
nnoremap <leader>fh :sp **/*
nnoremap <leader>fH :sp %%
nnoremap <leader>fv :vs **/*
nnoremap <leader>fV :vs %%
" folds
nnoremap <silent><@> @=(foldlevel('.')?'za':'')<cr>zz
nnoremap <silent><space> @=(foldlevel('.')?'za':'')<cr>zz
" list mappings
nnoremap <leader>lb :call ListMappings('map <buffer>')<cr>
nnoremap <leader>li :call ListMappings('imap')<cr>
nnoremap <leader>ll :call ListMappings()<cr>
nnoremap <leader>lm :call ListMappings('map')<cr>
nnoremap <leader>ln :call ListMappings('nmap')<cr>
nnoremap <leader>lo :call ListMappings('omap')<cr>
nnoremap <leader>lv :call ListMappings('vmap')<cr>
" marks
nnoremap ' `
nnoremap ` '
" movement
cnoremap <c-n> <up>
cnoremap <c-p> <down>
nnoremap j gj
nnoremap gj j
nnoremap k gk
nnoremap gk k
" operators
nnoremap Y y$
" search
nnoremap / /\v
nnoremap ? ?\v
nnoremap n nzz
nnoremap N Nzz
nnoremap # #zz
nnoremap * *zz
nnoremap <silent>cok :set bl!<cr>
nnoremap <leader>/ :nohlsearch<cr>:let @/=''<cr>:echo "Cleared search"<cr>
" selection
nnoremap gV `[v`]
" settings
nnoremap <silent><leader>h :set list!<cr> :set nu!<cr>
" shell
nnoremap ! :!
nnoremap <leader>; :call feedkeys(
\ ":nnoremap \<leader\>rr :up!\<cr\>:!clear; ", "nt")<cr>
" tabs
nnoremap <leader>tn :tabnew<cr>
nnoremap <silent><leader>to :call OpenTab()<cr>
nnoremap <silent><leader>tq :call CloseTab()<cr>
nnoremap <silent><leader>tt :exe "tabn ".g:lasttab<cr>
let i = 1
while i <= 9
silent! exe 'nnoremap <leader>t' . i . ' ' . i . 'gt'
let i = i + 1
endwhile
" vimrc
nnoremap <leader>ev :e ~/.vimrc<cr>
nnoremap <leader>sv :source ~/.vimrc<cr>
" windows
noremap <leader>S :split<cr>
noremap <leader>V :vsp<cr>
nnoremap <silent><leader>wo <c-w>o
nnoremap <silent><leader>wq <c-w>c
nnoremap <leader>ww <c-w>p
let i = 1
while i <= 9
silent! exe 'nnoremap <leader>' . i . ' :' . i . 'wincmd w<cr>'
let i = i + 1
endwhile
" }}}
" Section: autocommands {{{1
augroup custom
au!
au FileType netrw call SetupNetrw()
au FileType vim setl et kp=:help fdm=marker
au InsertLeave * set nopaste
au TabLeave * let g:lasttab = tabpagenr()
au VimResized * :wincmd =
augroup END
" }}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment