Skip to content

Instantly share code, notes, and snippets.

@placek
Last active July 25, 2023 13:40
Show Gist options
  • Save placek/36001864ecaa1f301e7f03a76fdd294f to your computer and use it in GitHub Desktop.
Save placek/36001864ecaa1f301e7f03a76fdd294f to your computer and use it in GitHub Desktop.
Some simple vimrc that makes VIM into an IDE (kind of).
""""""""""""""""""""""""""""""""" ESSENTIALS """""""""""""""""""""""""""""""""""
filetype plugin indent on " for plugins to load correctly
syntax enable " turn on syntax highlighting
set nocompatible " don't try to be vi compatible
"""""""""""""""""""""""""""""""""" SETTINGS """"""""""""""""""""""""""""""""""""
" interface
set cmdheight=1 " set command line height
set cursorline " line at current cursor position
set foldcolumn=1 " always show at least one cahr in fold column
set hlsearch " highlight search
set laststatus=2 " always show last status
set noshowmode " do not show mode in command line
set showmatch " highlight matching brackets
set signcolumn=yes " always show sign column
set showfulltag " show info about tag in completemenu
set showcmd " always show the command opearator
set ruler " show file stats
set number " show line numbers
set nospell " do not display spellchecking
set list listchars=tab:»\ ,nbsp:␣,trail:·,extends:›,precedes:‹ " show blank characters
set wrap " wrap a text on screen
set scrolloff=4 " always show 4 lines before/after coursor
set shortmess+=a shortmess+=c " shorter messages
" behaviour
set autowrite " autosave buffer when using jumps
set backspace=indent,eol,start " set the behavious of backspace
set clipboard=unnamedplus " linux standard clipboard
set encoding=utf-8 " encoding
set termencoding=utf-8 " encoding for terminal
set hidden " allow hidden buffers
set incsearch " show search results while typing regex
set matchpairs+=<:> " use % to jump between < and > as well
set mouse=a " full mouse intagration
set undofile " save history of the file
set nobackup " no mess in the project
set noswapfile " no problematic swap files
set undodir=/tmp/undodir " set the directory for undofiles
" completion
set omnifunc=syntaxcomplete#Complete " set onmicomplete function
set completeopt=menuone,noselect " on completion always show menu, but do not select anything
" diff options
set diffopt+=algorithm:minimal " most minimal diff possible
set diffopt+=indent-heuristic " indentation
set diffopt-=internal " don't use internal diff algorithm
" more natural splits
set splitbelow " split below to current window
set splitright " split right to current window
" indentation
set noshiftround " do not round indent to multiple of 2
set shiftwidth=2 " indent size
" tabs into space
set expandtab " tabs into spaces
set softtabstop=2 " width of the tab in spaces
set tabstop=2 " tabs at 2 spaces
" text width
set textwidth=80 " 80 columns wide text
set colorcolumn=80,160 " a bar at 80 and 160 column
" gotta go fast!
set updatetime=300 " CursorHold delay
set timeoutlen=1000 ttimeoutlen=0 " immediate escape key result
set ttyfast " fast rendering
" autocomplete paths in command mode
set path+=** " search down files into subdirectories
set wildmenu " display all matching files on tab completion
set wildignore+=.git/,**/node_modules/** " ignore files from unwanted directories
set wildmode=longest:full,full " match longest path fully matching pattern
" status line
set statusline=%1*\ %{mode()}\ %2*\ %F:%l:%c%=
set statusline+=\ %1*\ %m%r[%n]\ %{strlen(&fenc)?&fenc:'none'}\
""""""""""""""""""""""""""""""""""" FUNCTIONS """"""""""""""""""""""""""""""""""
" returns a text under visual selection
function! s:selectedText()
silent normal gv"vy
let l:selection = getreg('v')
let l:escaped = escape(l:selection, '\/')
let l:regexp = substitute(l:escaped, "\n", "\\\\n", "g")
normal \<Esc>
return l:regexp
endfunction
" triggers a buffer search using a given query
function! s:findTextInBuffer(register, query)
call setreg(a:register, "\\V" . a:query)
normal n
endfunction
" triggers a grep on query and opens quickfix list
function! s:findTextInPwd(query)
if len(a:query) != 0
execute "silent grep \"" . a:query . "\" | copen"
endif
endfunction
" finds a file using git ls-files or find command
function! s:find(query)
if len(a:query) != 0
if executable('git') && isdirectory(".git")
execute "silent cexpr system('git ls-files \"**" . a:query . "*\" | sed \"s/.*/&:1:1/\"') | copen"
else
execute "silent cexpr system('find . -type f -printf \"%p:1:1\\n\" \"*" . a:query . "*\"') | copen"
endif
endif
endfunction
"""""""""""""""""""""""""""""""""" VARIABLES """""""""""""""""""""""""""""""""""
" set leader keys
let g:mapleader = "\\" " main leader set to backslash
let g:maplocalleader = "," " local leader set to comma
" netrw settings
let g:netrw_keepdir = 0
let g:netrw_banner = 0
let g:netrw_list_hide = '\(^\|\s\s\)\zs\.\S\+'
let g:netrw_localcopydircmd = 'cp -r'
let g:netrw_liststyle = 3
let g:netrw_browse_split = 1
""""""""""""""""""""""""""""""""""" MAPPINGS """""""""""""""""""""""""""""""""""
" files explorer
nnoremap <leader>/ :edit %:p:h<cr>
" 'fuzzy finder' on buffers
nnoremap <leader>b :buffer
nnoremap <leader>m :marks<cr>
nnoremap <leader>r :registers<cr>
nnoremap <leader>h :jumps<cr>
nnoremap <leader>u :undolist<cr>
" 'fuzzy finder'
command! -nargs=1 -complete=buffer Find call Find(<q-args>) " find and open quickfix
nnoremap <silent><leader>\ :<c-u>call <sid>find(input("/"))<cr>
vnoremap <silent><leader>\ :<c-u>call <sid>find(<sid>selectedText())<cr>
" fast grepping through git project
if executable('git') && isdirectory(".git")
set grepprg=git\ grep\ --no-color\ --line-number\ --column\ -E\ $* " git grep as search engine
endif
command! -nargs=1 -complete=buffer Grep call <sid>findTextInPwd(<q-args>) " grep and open quickfix
nnoremap <silent><leader>f :<c-u>call <sid>findTextInPwd(input("/"))<cr>
vnoremap <silent><leader>f :<c-u>call <sid>findTextInPwd(<sid>selectedText())<cr>
" move around windows with leader key
nnoremap <leader><up> <c-w><up>
nnoremap <leader><down> <c-w><down>
nnoremap <leader><left> <c-w><left>
nnoremap <leader><right> <c-w><right>
" some appearance togglable settings
nnoremap <leader>1 :set relativenumber!<cr>
nnoremap <leader>2 :set hlsearch!<cr>
nnoremap <leader>3 :set spell!<cr>
" `:make` command that passes the file as an argument
nnoremap <leader>" :lmake %:t:r<cr>
nnoremap <leader>' :lmake %:.<cr>
nnoremap <leader><M-'> :lmake %:.:<c-r>=line('.')<cr><cr>
" make folds and return to manual foldmethod
function! MakeFolds()
setlocal foldmethod=indent
norm zR
setlocal foldmethod=manual
endfunction
set foldmethod=manual
nnoremap <leader>z :call MakeFolds()<cr>
" intuitive jumps
nnoremap <silent>[a :previous<cr>
nnoremap <silent>]a :next<cr>
nnoremap <silent>[b :bprevious<cr>
nnoremap <silent>]b :bnext<cr>
nnoremap <silent>[w <c-w>W
nnoremap <silent>]w <c-w>w
nnoremap <silent>[t :tabprevious<cr>
nnoremap <silent>]t :tabnext<cr>
nnoremap <silent>[c :cprevious<cr>
nnoremap <silent>]c :cnext<cr>
nnoremap <silent>[l :lprevious<cr>
nnoremap <silent>]l :lnext<cr>
" toggle loclist list
function! ToggleLocList()
if empty(filter(getwininfo(), 'v:val.loclist'))
lopen
else
lclose
endif
endfunction
nnoremap <silent><leader>Q :call ToggleLocList()<cr>
" toggle quickfix list
function! ToggleQuickFix()
if empty(filter(getwininfo(), 'v:val.quickfix'))
copen
else
cclose
endif
endfunction
nnoremap <silent><leader>q :call ToggleQuickFix()<cr>
" `*` and `#` in visual mode!
vnoremap <silent>* :<c-u>call <sid>findTextInBuffer("/", <sid>selectedText())<cr>n
vnoremap <silent># :<c-u>call <sid>findTextInBuffer("#"), <sid>selectedText())<cr>n
" MakeTags - use a `git ctags` command to generate tagfile
function! MakeTags()
call jobstart("git ctags", { "on_exit": function('MakeTagsResult') })
endfunction
function! MakeTagsResult(job, status, event)
if a:status == 0
echom "MakeTags: done"
else
echom "MakeTags: tags generation failed"
endif
endfunction
" add .git/tags file to scope
set tags+=.git/tags
" generate tags in working directory
command! -nargs=0 MakeTags call MakeTags()
" go to a tag under a cursor
nnoremap gd g<c-]>
" go to a tag
vnoremap gd g<c-]>
" search tags
nnoremap <leader>t :ltag
autocmd! BufWritePost * :silent! MakeTags
" poor Harpoon™
" when editing a file press <space><enter> in normal mode to add it to list of arguments
nnoremap <localleader><cr> :argadd %<cr>
" rotate between arguments with <space><left> or <space><right>
nnoremap <localleader><left> :previous<cr>
nnoremap <localleader><right> :next<cr>
" list arguments with <space><space>
nnoremap <localleader><space> :args<cr>
" remove a file from argument list with <space><esc>
nnoremap <localleader><esc> :argdelete %<cr>
" use `open` command to open a current buffer
command! -count Open !open %
" leave only the current buffer - delete others
command! BufOnly execute '%bdelete|edit #|normal `"'
" remove trailing whitespaces after saving a file
autocmd! BufWritePre * :%s/\s\+$//e
" undo sequence for space, dot and newline
inoremap <space> <C-G>u<space>
inoremap . <C-G>u.
inoremap <cr> <C-G>u<cr>
" `<C-p>` that acts like `p`, but not change the `+` register
vnoremap <C-p> "pdp
vnoremap <C-P> "pdP
"""""""""""""""""""""""""""""""""""" COLORS """"""""""""""""""""""""""""""""""""
hi ColorColumn ctermbg=8
hi Comment ctermfg=7
hi DiffAdd ctermbg=8 ctermfg=10 cterm=none
hi DiffChange ctermbg=8 ctermfg=3 cterm=none
hi DiffDelete ctermbg=8 ctermfg=9 cterm=none
hi DiffText ctermbg=8 ctermfg=11 cterm=underline
hi Directory ctermfg=4
hi FoldColumn ctermbg=0 ctermfg=7
hi Folded ctermbg=0 ctermfg=15 cterm=bold
hi Pmenu ctermbg=0 ctermfg=7
hi PmenuSbar ctermbg=0 ctermfg=7
hi PmenuSel ctermbg=4 ctermfg=0 cterm=bold
hi Search ctermbg=3 ctermfg=0 cterm=none
hi SignColumn ctermbg=0
hi TabLine ctermbg=8 ctermfg=7 cterm=bold
hi TabLineFill ctermbg=0 ctermfg=0
hi TabLineSel ctermbg=7 ctermfg=0 cterm=bold
hi User1 ctermbg=6 ctermfg=0 cterm=bold
hi User2 ctermbg=0 ctermfg=15 cterm=bold
hi VertSplit ctermbg=8 ctermfg=8
hi Visual ctermbg=7 ctermfg=0
hi String ctermfg=3
hi Boolean ctermfg=13 cterm=bold
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment