Skip to content

Instantly share code, notes, and snippets.

@cedmundo
Last active June 24, 2025 20:56
Show Gist options
  • Save cedmundo/ec5c5e2640c83a60bd74519d04665ebf to your computer and use it in GitHub Desktop.
Save cedmundo/ec5c5e2640c83a60bd74519d04665ebf to your computer and use it in GitHub Desktop.
nvim/init.vim
set shell=/bin/bash
" Necesary for lots of cool vim things
set nocompatible
" Plugins
call plug#begin()
Plug 'preservim/tagbar'
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'
Plug 'preservim/nerdtree'
Plug 'prabirshrestha/vim-lsp'
Plug 'prabirshrestha/asyncomplete.vim'
Plug 'prabirshrestha/asyncomplete-lsp.vim'
" Plug 'nvim-treesitter/nvim-treesitter', {'do': ':TSUpdate'}
" Go tools
Plug 'fatih/vim-go', { 'do': ':GoUpdateBinaries' }
call plug#end()
" required
filetype plugin indent on
" Backup
set backupdir=~/.vim//,.
set directory=~/.vim//,.
" Remove any trailing whitespace that is in the file
autocmd BufRead,BufWrite * if ! &bin | silent! %s/\s\+$//ge | endif
" Restore cursor position to where it was before
augroup JumpCursorOnEdit
au!
autocmd BufReadPost *
\ if expand("<afile>:p:h") !=? $TEMP |
\ if line("'\"") > 1 && line("'\"") <= line("$") |
\ let JumpCursorOnEdit_foo = line("'\"") |
\ let b:doopenfold = 1 |
\ if (foldlevel(JumpCursorOnEdit_foo) > foldlevel(JumpCursorOnEdit_foo - 1)) |
\ let JumpCursorOnEdit_foo = JumpCursorOnEdit_foo - 1 |
\ let b:doopenfold = 2 |
\ endif |
\ exe JumpCursorOnEdit_foo |
\ endif |
\ endif
" Need to postpone using "zv" until after reading the modelines.
autocmd BufWinEnter *
\ if exists("b:doopenfold") |
\ exe "normal zv" |
\ if(b:doopenfold > 1) |
\ exe "+".1 |
\ endif |
\ unlet b:doopenfold |
\ endif
augroup END
" Needed for Syntax Highlighting and stuff
syntax enable
set grepprg=grep\ -nH\ $*
" Cool colors
set background=dark
" set t_8f=^[[38;2;%lu;%lu;%lum " Needed in tmux
" set t_8b=^[[48;2;%lu;%lu;%lum " Ditto
set termguicolors
let $NVIM_TUI_ENABLE_TRUE_COLOR=1
colorscheme focuspoint
" No wrap
set nowrap
" Show line numbers
set number
" Who doesn't like autoindent?
set autoindent
" Enable mouse support in console
set mouse=a
" Spaces are better than a tab character
set expandtab
set smarttab
" Got backspace?
set backspace=2
" Who wants an 8 character tab? Not me!
set shiftwidth=2
set softtabstop=2
" Blinking cursor
set guicursor=a:blinkon100
" Show column 80
set colorcolumn=80,120
highlight ColorColumn ctermbg=235 guibg=#262626
" Two spces on html files
au FileType text set expandtab smarttab shiftwidth=2 softtabstop=2
au FileType html set expandtab smarttab shiftwidth=2 softtabstop=2
au FileType js set expandtab smarttab shiftwidth=2 softtabstop=2
au FileType rb set expandtab smarttab shiftwidth=2 softtabstop=2
au FileType ruby set expandtab smarttab shiftwidth=2 softtabstop=2
au FileType javascript set expandtab smarttab shiftwidth=2 softtabstop=2
au FileType css set expandtab smarttab shiftwidth=2 softtabstop=2
au FileType c set expandtab smarttab shiftwidth=2 softtabstop=2
au FileType php set expandtab smarttab shiftwidth=4 softtabstop=4
au FileType java set expandtab smarttab shiftwidth=4 softtabstop=4
au FileType gradle set expandtab smarttab shiftwidth=4 softtabstop=4
au FileType slang set expandtab smarttab shiftwidth=2 softtabstop=2
au FileType go set noexpandtab smarttab shiftwidth=8 softtabstop=8
" Incremental searching is sexy
set incsearch
" Highlight things that we find with the search
set hlsearch
" GLSL Syntax
au BufNewFile,BufRead *.frag,*.vert,*.fp,*.vp,*.glsl setf glsl
au BufRead,BufNewFile *.slang setf slang
" Fold
set foldmethod=indent
set foldlevel=99
" Relative numbers (toggle absoltue on edit)
set number relativenumber
augroup numbertoggle
autocmd!
autocmd BufEnter,FocusGained,InsertLeave * set relativenumber
autocmd BufLeave,FocusLost,InsertEnter * set norelativenumber
augroup END
" LSP Diagnostics with server status
function! LspStatus() abort
let l:diagnostics = lsp#get_buffer_diagnostics_counts()
let l:e_count = get(l:diagnostics, 'error', 0)
let l:w_count = get(l:diagnostics, 'warning', 0)
let l:i_count = get(l:diagnostics, 'information', 0)
let l:h_count = get(l:diagnostics, 'hint', 0)
for ss in split(lsp#get_server_status(), '\n')
if ss =~ ':\srunning$'
return printf('[%dE %dW %dI %dH]', e_count, w_count, i_count, h_count)
endif
endfor
return '[no diagnostics]'
endfunction
" Git branch
function! GitBranch()
return system("timeout 1 git rev-parse --abbrev-ref HEAD 2>/dev/null | tr -d '\n'")
endfunction
" Git status
function! GitStatus()
let l:branchname = GitBranch()
return strlen(l:branchname) > 0?' '.l:branchname.' ':''
endfunction
" Status line
set laststatus=2
set statusline=
set statusline+=%#PmenuSel#
set statusline+=%{GitStatus()}
set statusline+=%#LineNr#
set statusline+=\ %{LspStatus()}
set statusline+=\ %f
set statusline+=%m\ %=
set statusline+=%#CursorColumn#
set statusline+=\ %y
set statusline+=\ %{&fileencoding?&fileencoding:&encoding}
set statusline+=\[%{&fileformat}\]
set statusline+=\ %l:%c/%{line('$')}
set statusline+=\ %p%%
" Leader key
let mapleader = ","
" Command remaps, mostly accidental keyhits
:command Qa qa
:command WQ wq
:command Wq wq
:command W w
:command Q q
" Custom hotkeys
map ñ ^
map Ñ $h
map <A-d> yyp
" map <A-'> c'<C-r>"'<ESC>
" map <A-"> c"<C-r>""<ESC>
" Replace visual selection
vnoremap <C-r> "hy:%s/<C-r>h//gc<left><left><left>
" For local replace
nnoremap gr gd[{V%::s/<C-R>///gc<left><left><left>
" For global replace
nnoremap gR gD:%s/<C-R>///gc<left><left><left>
nnoremap <F2> :NERDTreeToggle<CR>
nnoremap <F3> :TagbarToggle<CR>
" Navigate using Meta-Alt-Arrows
nnoremap <M-A-Left> <C-w>h
nnoremap <M-A-Down> <C-w>j
nnoremap <M-A-Up> <C-w>k
nnoremap <M-A-Right> <C-w>l
nnoremap <M-A-q> <C-w>q
" Compilation and testing
au FileType c set makeprg=cmake\ --build\ build
au FileType c nnoremap <F5> :make<CR>
au FileType c nnoremap <F6> :make test<CR>
" Fuzzy search
command! -bang -nargs=* Ag
\ call fzf#vim#ag(<q-args>,
\ <bang>0 ? fzf#vim#with_preview('up:60%')
\ : fzf#vim#with_preview('right:50%:hidden', '?'),
\ <bang>0)
" Open ag search dialog
nnoremap <C-p> :Ag<CR>
" Open ag search dialog with current selection
vnoremap <C-p> "hy:Ag <C-r>h<CR>
" LSP Settings
if executable('clangd')
au User lsp_setup call lsp#register_server({
\ 'name': 'clangd',
\ 'cmd': {server_info->['clangd', '--compile-commands-dir=cmake-build-debug']},
\ 'allowlist': ['c', 'cpp', 'c++', 'cc'],
\ })
endif
if executable('slangd')
au User lsp_setup call lsp#register_server({
\ 'name': 'slangd',
\ 'cmd': {server_info->['slangd', '--debug']},
\ 'allowlist': ['slang'],
\ })
endif
if executable('gopls')
au User lsp_setup call lsp#register_server({
\ 'name': 'gopls',
\ 'cmd': {server_info->['gopls']},
\ 'allowlist': ['go', 'golang'],
\ })
endif
function! s:on_lsp_buffer_enabled() abort
setlocal omnifunc=lsp#complete
setlocal signcolumn=yes
" highlight lspReference ctermfg=red guifg=red ctermbg=green guibg=green
if exists('+tagfunc') | setlocal tagfunc=lsp#tagfunc | endif
nmap <buffer> gd <plug>(lsp-definition)
nmap <buffer> gs <plug>(lsp-document-symbol-search)
nmap <buffer> gS <plug>(lsp-workspace-symbol-search)
nmap <buffer> gr <plug>(lsp-references)
nmap <buffer> gi <plug>(lsp-implementation)
nmap <buffer> gt <plug>(lsp-type-definition)
nmap <buffer> <leader>rn <plug>(lsp-rename)
nmap <buffer> [g <plug>(lsp-previous-diagnostic)
nmap <buffer> ]g <plug>(lsp-next-diagnostic)
nmap <buffer> K <plug>(lsp-hover)
"nnoremap <buffer> <expr><c-f> lsp#scroll(+4)
"nnoremap <buffer> <expr><c-d> lsp#scroll(-4)
let g:lsp_format_sync_timeout = 1000
autocmd! BufWritePre *.rs,*.go,*.c,*.h,*.slang call execute('LspDocumentFormatSync')
" set foldmethod=expr
" \ foldexpr=lsp#ui#vim#folding#foldexpr()
" \ foldtext=lsp#ui#vim#folding#foldtext()
endfunction
augroup lsp_install
au!
" call s:on_lsp_buffer_enabled only for languages that has the server registered.
autocmd User lsp_buffer_enabled call s:on_lsp_buffer_enabled()
augroup END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment