While many here are seasoned vim / neovim users, there are probably at least a few here that would love to move from (insert your editor) in favor of vim/neovim.
Sharing your .vimrc files and/or plugins may be quite helpful!!!
While many here are seasoned vim / neovim users, there are probably at least a few here that would love to move from (insert your editor) in favor of vim/neovim.
Sharing your .vimrc files and/or plugins may be quite helpful!!!
Mine’s a bit long but does a lot of stuff that I’ve built up over the years, I also try not to set things that are already default.
For those interested I also have a few vim plugins at bitbucket.org/natemaia that are used in the vimrc.
" My vimrc || init.vim
" provides the same functionality in both vim and neovim
" ------ leader ------ {{{1
let g:mapleader = ","
nmap <Space> <Leader>
vmap <Space> <Leader>
" }}}
" ------ runtimepath ------ {{{1
let g:python_host_prog = '/usr/bin/python3'
let s:script_path = $HOME.'/.config/nvim'
if !has('nvim') && &runtimepath !~? s:script_path
let &runtimepath .= ','.s:script_path
if isdirectory('~/.config/nvim')
set runtimepath^=~/.config/nvim
endif
endif
" create the backup and undo directories
silent! call mkdir(s:script_path.'/undo', 'p')
silent! call mkdir(s:script_path.'/backup', 'p')
" }}}
" ------ plugins ------ {{{1
" ------ vim-plug ------ {{{2
if empty(globpath(&runtimepath, 'autoload/plug.vim'))
if has('nvim')
silent! call mkdir($HOME . '/.config/nvim/autoload/', 'p')
execute 'silent !curl -fLo ~/.config/nvim/autoload/plug.vim https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
else
silent! call mkdir($HOME . '/.vim/autoload/', 'p')
execute 'silent !curl -fLo ~/.vim/autoload/plug.vim https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
endif
execute 'normal :PlugUpdate | source $MYVIMRC'
endif
" }}}
" ------ plugin install ------ {{{2
call plug#begin()
if !has('nvim')
Plug 'Shougo/deoplete.nvim' | Plug 'roxma/nvim-yarp' | Plug 'roxma/vim-hug-neovim-rpc'
else
Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' }
endif
Plug 'zchee/deoplete-zsh'
Plug 'Shougo/neco-vim'
Plug 'Shougo/deoplete-clangx'
Plug 'deoplete-plugins/deoplete-tag'
Plug 'Shougo/neoinclude.vim'
Plug 'baskerville/vim-sxhkdrc'
Plug 'dense-analysis/ale'
Plug 'tpope/vim-repeat'
Plug 'tpope/vim-surround'
Plug 'tpope/vim-fugitive'
Plug 'tpope/vim-commentary'
Plug 'tpope/vim-unimpaired'
Plug 'mbbill/undotree'
Plug 'lilydjwg/colorizer'
Plug 'christoomey/vim-tmux-navigator'
Plug 'vim-scripts/ReplaceWithRegister'
Plug 'https://bitbucket.org/natemaia/vim-jinx'
Plug 'https://bitbucket.org/natemaia/vim-yapp'
Plug 'https://bitbucket.org/natemaia/vim-status'
call plug#end()
" }}}
" ------ plugin config ------ {{{2
" ------ deoplete ------ {{{3
let g:deoplete#enable_at_startup = 1
call deoplete#custom#option({ 'max_list': 50, 'ignore_sources': {'_': 'ale'} })
call deoplete#custom#source('_', { 'mark': '-', 'max_menu_width': 0, 'max_abbr_width': 0 })
" }}}
" ------ ALE ------ {{{3
let g:ale_disable_lsp = 1
let g:ale_c_parse_makefile = 1
let g:ale_completion_enabled = 0
call deoplete#custom#source('ale', 'rank', 0)
" }}}
" ------ theme & statusline ------ {{{3
let g:jinx_theme = 'midnight'
let g:status_theme = 'default'
if has('termguicolors') && &t_Co ==# 256
let g:status_theme = 'midnight'
set termguicolors
" fix true colour in st terminal under vim
if !has('nvim') && ($TERM ==# 'st-256color' || &term ==# 'st-256color')
let $TERM = 'xterm-256color'
let &term = 'xterm-256color'
endif
endif
colorscheme jinx
" }}}
" ------ misc ------ {{{
" automatically focus the undotree when opening
let g:undotree_SetFocusWhenToggle = 1
" don't try to colorize codes by default, enable it manually with <Leader>tc
let g:colorizer_startup = 0
" }}}
" ------ mappings ------ {{{3
if !exists('g:loaded_plugmaps') " stop plugin mappings being reloaded when sourcing vimrc
imap <silent><expr> <Tab> Expand_tab()
vmap <silent><expr> <Tab> Expand_tab()
nmap <silent><expr> <Tab> Expand_tab()
smap <silent><expr> <Tab> Expand_tab()
imap <silent><expr> <Space> "\<C-]>\<Space>"
imap <silent><expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"
imap <silent><expr> <Return> pumvisible() ? "\<C-x>" : "\<CR>"
imap <silent><expr> <Up> pumvisible() ? "\<C-p>" : "\<Up>"
imap <silent><expr> <Down> pumvisible() ? "\<C-n>" : "\<Down>"
nmap <silent> <Leader>gd <Plug>(ale_go_to_definition)
nmap <silent> <Leader>n <Plug>(ale_next_wrap)
nnoremap <silent> <F5> :UndotreeToggle<CR>
nnoremap <unique><silent> / :call HLNext('set')<CR>/
nnoremap <unique><silent> ? :call HLNext('set')<CR>?
nnoremap <unique><silent> n nzzzv:call HLNext('move')<CR>
nnoremap <unique><silent> N Nzzzv:call HLNext('move')<CR>
nnoremap <unique><silent> <BS> :call HLNext('off')<BAR>nohlsearch<CR>
let g:loaded_plugmaps = 1
endif
" }}}
" }}}
" }}}
" ------ basics ------ {{{1
scriptencoding utf8
filetype plugin indent on
syntax on
set backup
set undofile
let &undodir = expand(globpath(&runtimepath, 'undo')).'//'
let &backupdir = expand(globpath(&runtimepath, 'backup')).'//'
set undolevels=5000
set number
set relativenumber
set foldmethod=syntax
set linebreak
set breakindent
set breakindentopt=min:20,shift:0,sbr
set scrolloff=8
set tabstop=4
set softtabstop=4
set shiftwidth=4
set confirm
set modeline
set smartcase
set nojoinspaces
set synmaxcol=1000
set showcmd
set noshowmode
set cmdheight=1
set matchpairs+=<:>
set hidden
set splitbelow
set splitright
set noequalalways
set ttimeout
set timeoutlen=600
set ttimeoutlen=10
set updatecount=80
set mouse=niv
set shortmess+=aAcws
set clipboard^=unnamedplus,unnamed
set spell
set spelllang=en
set complete+=kspell
set wildmenu
set wildignore+=*~,*.pyc,*/.git/*,*.so,*.swp,*.o,*.zip,*.zwc,*.png,*.jpg
set cinoptions+=:0,l1,t0
" hide snippet marks, markdown special characters, etc..
if has('conceal')
set conceallevel=2
set concealcursor=niv
endif
if has('shada') " ignore /tmp and /mnt in shada history
set shada=!,'1000,<50,s100,h,r/tmp,r/mnt
endif
" invisible fancy characters when multi_byte support
if has('multi_byte') && $DISPLAY !=# ''
set list listchars=tab:\ \ ,extends:❯,precedes:❮
set fillchars=diff:⣿,vert:┃
let &showbreak = '↪ '
endif
" preview :%s/old/new/ results as you type
if exists('&inccommand')
set inccommand=nosplit
endif
" allow cursor to go past EOL in visual block mode
if exists('&virtualedit')
set virtualedit=block
endif
" set defaults for python
let g:python_highlight_all = 1
let g:python3_host_prog = '/usr/bin/python3'
" netrw file browser
let g:netrw_banner = 0
let g:netrw_dirhistmax = 0
if !has('nvim') " nice settings for vim (neovim defaults)
set ttyfast
set ttymouse=sgr
set viminfo+='1000,n~/.cache/.viminfo
if exists('$TMUX') " change cursor shape for different editing modes
let &t_SI = "\ePtmux;\e\e[5 q\e\\"
let &t_SR = "\ePtmux;\e\e[4 q\e\\"
let &t_EI = "\ePtmux;\e\e[2 q\e\\"
elseif $TERM ==# 'linux'
let &t_SI = "\e[?0c"
let &t_EI = "\e[?8c"
else
let &t_SR = "\e[4 q"
let &t_EI = "\e[2 q"
let &t_SI = "\e[6 q"
endif
endif
" }}}
" ------ ex commands ------ {{{1
"
command! SynShow call Synstack()
command! Ctags exec 'silent !ctags -R --exclude=.git .'
command! LongLineHL call LongLineHL()
command! MacroVisual call Macro_visual()
command! -complete=buffer -nargs=? Bselect call Bselect(<args>)
command! -bang Bclose call Bclose(<bang>)
command! W exec 'silent w !sudo tee % >/dev/null' | edit!
command! DiffOrig vert new | set bt=nofile | r ++edit # | 0d_ | diffthis | wincmd p | diffthis
command! -bar -range ExecVimL silent! <line1>,<line2>yank z | let @z=substitute(@z, '\n\s*\\', '', 'g') | @z
" }}}
" ------ abbreviations ------ {{{1
cnoreabbr Q q
cnoreabbr Qa qa
cnoreabbr QA qa
cnoreabbr Wq wq
cnoreabbr WQ wq
inoreabbr <silent> timef <C-R>=strftime('%a, %d %b %Y')<CR><Space>{{{<CR><CR>}}}<Up><C-R>=Eatchar('\s')<CR>
inoreabbr <silent><expr> #! "#!/usr/bin/env" . (empty(&ft) ? '' : (&ft == 'sh') ? " bash\<CR>" : ' ' . &ft) . Eatchar('\s')
" }}}
" ------ auto commands ------ {{{1
augroup file_setup
autocmd!
autocmd BufReadPost * if &filetype !~ 'git.*\|diff.*'
\ | call setpos(".", getpos("'\"")) |
\ endif
autocmd FocusGained,BufEnter * if mode() !=? 'c'
\ | checktime |
\ endif
autocmd FileChangedShellPost * echo "Changes loaded from source file"
augroup END
if has('nvim')
augroup open_terminal
autocmd!
autocmd TermOpen * setlocal nonumber norelativenumber modifiable | startinsert
autocmd TermClose * buffer #
augroup END
else
if has('terminal')
augroup open_terminal
autocmd!
autocmd TerminalOpen * setlocal nonumber norelativenumber
augroup END
endif
endif
if $TERM !=# 'linux'
augroup active_cursorline
autocmd!
autocmd InsertEnter * setlocal cursorline listchars-=trail:•
autocmd InsertLeave * setlocal nocursorline listchars+=trail:•
augroup END
else
set nocursorline
endif
" }}}
" ------ mappings ------ {{{1
" ------ buffers ------ {{{2
nnoremap <Leader>c :ccl<CR>
nnoremap <Leader>b :Bselect<CR>
nnoremap <Leader>q :Bclose<CR>
" }}}
" ------ tabs ------ {{{2
nnoremap <silent> <Leader>te :tabnew<CR>
nnoremap <silent> <Leader>tn :tabnext<CR>
nnoremap <silent> <Leader>tf :tabfirst<CR>
nnoremap <silent> <Leader>tp :tabprevious<CR>
" }}}
" ------ alternate default mappings ------ {{{2
if has('nvim')
" allow <Esc> in terminal mode, C-v-Esc sends escape to terminal
tnoremap <Esc> <C-\><C-n>
tnoremap <C-v><Esc> <Esc>
endif
" jump back up tag stack, opposite of <C-]> goto definition
nnoremap <C-[> <C-t>
" 0 now toggles between $ and ^ on the current line
nnoremap <silent> 0 :call Toggle_line_ends()<CR>
" Y/D act like you'd expect
nnoremap Y y$
nnoremap D d$
" run the q macro with Q
nnoremap Q @q
" x is a black hole in all modes, use d instead
noremap x "_x
" toggle folding
nnoremap ZA :set foldenable!<CR>
" correct spelling at current word
nnoremap <silent>zz msz=1<CR><CR>`s
" visual the current line without leading/trailing whitespace
nnoremap yl ^vg_
" swap visual mode binds, block is just more useful
nnoremap v <C-V>
nnoremap <C-V> v
vnoremap v <C-V>
vnoremap <C-V> v
" gj/k but preserve numbered jumps ie: 12j or 45k
nmap <buffer><silent><expr>j v:count ? 'j' : 'gj'
nmap <buffer><silent><expr>k v:count ? 'k' : 'gk'
" }}}
" ------ additional helpful mappings ------ {{{2
" highlight long lines, ll (long lines)
nnoremap <silent> <Leader>ll :LongLineHL<CR>
" fix syntax highlighting issues
nnoremap U :syntax sync fromstart<CR>:redraw!<CR>
" yank the entire file
nnoremap gyy gg"+yG''
vnoremap gyy <Esc>gg"+yG''
" Find merge conflict markers
nnoremap <leader>fc /\v^[<\|=>]{7}( .*\|$)<CR>
" find lines longer than 80 characters
nnoremap <Leader>fl /\%>100v.\+<CR>
" get unicode string from char under the cursor
nnoremap <Leader>un mz"zylo<C-r>=printf('U+%04X', char2nr(@z))<CR><ESC>`z
" }}}
" ------ mappings that should be functions ------ {{{2
" strip trailing whitespace, ss (strip space)
nnoremap <silent><Leader>ss
\ :let b:_p = getpos(".") <Bar>
\ let b:_s = (@/ != '') ? @/ : '' <Bar>
\ %s/\s\+$//e <Bar>
\ let @/ = b:_s <Bar>
\ nohlsearch <Bar>
\ unlet b:_s <Bar>
\ call setpos('.', b:_p) <Bar>
\ unlet b:_p <CR>
" global replace, sw (substitute word)
vnoremap <Leader>sw "hy:let b:sub = input('global substitute: ') <Bar>
\ if b:sub !=? '' <Bar>
\ let b:rep = escape(getreg('h'), '/\$^.*[]') <Bar>
\ execute '%s/'.b:rep.'/'.b:sub.'/g' <Bar>
\ unlet b:sub b:rep <Bar>
\ endif <CR>
nnoremap <Leader>sw
\ :let b:sub = input('replacement: ') <Bar>
\ if b:sub !=? '' <Bar>
\ execute '%s/<C-r><C-w>/'.b:sub.'/g' <Bar>
\ unlet b:sub <Bar>
\ endif <CR>
" prompt before each replace, cw (change word)
vnoremap <Leader>cw "hy:let b:sub = input('interactive replacement: ') <Bar>
\ if b:sub !=? '' <Bar>
\ let b:rep = escape(getreg('h'), '/\$^.*[]') <Bar>
\ execute '%s/'.b:rep.'/'.b:sub.'/gc' <Bar>
\ unlet b:sub b:rep <Bar>
\ endif <CR>
nnoremap <Leader>cw
\ :let b:sub = input('replacement: ') <Bar>
\ if b:sub !=? '' <Bar>
\ execute '%s/<C-r><C-w>/'.b:sub.'/gc' <Bar>
\ unlet b:sub <Bar>
\ endif <CR>
" local keyword jump, fw (find word)
nnoremap <silent><Leader>fw
\ [I:let b:jump = input('jump to: ') <Bar>
\ if b:jump !=? '' <Bar>
\ execute 'normal! '.b:jump."[\t" <Bar>
\ unlet b:jump <Bar>
\ endif <CR>
" }}}
" }}}
" ------ functions ------ {{{1
function! Eatchar(pat) abort " {{{2
let l:c = nr2char(getchar(0))
return (l:c =~ a:pat) ? '' : l:c
endfunc " }}}
function! Macro_visual() abort " {{{2
echo '@'.getcmdline()
let l:range = nr2char(getchar())
execute ":'<,'>normal @".l:range
endfunction " }}}
function! Synstack() abort " {{{2
echo join(map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")'), ' > ')
endfunc " }}}
function! Warn(msg) abort " {{{2
echohl ErrorMsg
echomsg a:msg
echohl NONE
endfunction " }}}
function! Expand_tab() abort " {{{2
if pumvisible()
return "\<C-n>"
elseif mode() =~? 'n'
return '==1j'
elseif mode() =~? '\v(v|V|)'
return '='
endif
return "\<Tab>"
endfunction " }}}
function! Toggle_line_ends() abort " {{{2
normal ms^mc`s
if col('.') == col("'c")
normal $
else
normal ^
endif
endfunction " }}}
function! Toggle_case(str) abort " {{{2
if a:str ==# toupper(a:str)
let l:ret = tolower(a:str)
elseif a:str ==# tolower(a:str)
let l:ret = substitute(a:str, '\(\<\w\+\>\)', '\u\1', 'g')
else
let l:ret = toupper(a:str)
endif
return l:ret
endfunction " }}}
function! Bselect(...) abort " {{{2
let l:pattern = a:0 >= 1 ? a:1 : ''
if l:pattern ==? ''
let l:pattern = input('String match: ')
redraw!
endif
if l:pattern !=# ''
let l:bufcount = bufnr('$')
let l:currbufnr = 1
let l:nummatches = 0
let l:matchingbufnr = 0
while l:currbufnr <= l:bufcount
if bufexists(l:currbufnr)
let l:currbufname = bufname(l:currbufnr)
if (match(l:currbufname, l:pattern) > -1)
echo l:currbufnr . ': ' . l:currbufname
let l:nummatches += 1
let l:matchingbufnr = l:currbufnr
endif
endif
let l:currbufnr += 1
endwhile
if (l:nummatches == 1)
execute 'buffer '.l:matchingbufnr
elseif (l:nummatches > 1)
let l:desiredbufnr = input('Enter buffer number: ')
if (strlen(l:desiredbufnr) != 0)
execute 'buffer '.l:desiredbufnr
endif
else
call Warn('No matching buffers matching '.l:pattern)
endif
endif
endfunction " }}}
function! Bclose(...) abort " {{{2
let l:bang = a:0 >= 1 ? a:1 : ''
let l:target = bufnr('%')
if empty(l:bang) && getbufvar(l:target, '&modified')
call Warn('No write since last change for buffer '.l:target.' (use :Bclose!)')
return
endif
let l:num_wins = filter(range(1, winnr('$')), 'winbufnr(v:val) == l:target')
let l:cur_win = winnr()
for l:w in l:num_wins
execute l:w.'wincmd w'
let l:prevbuf = bufnr('#')
if l:prevbuf > 0 && buflisted(l:prevbuf) && l:prevbuf != l:w
buffer #
else
bprevious
endif
if l:target == bufnr('%')
let l:blisted = filter(range(1, bufnr('$')), 'buflisted(v:val) && v:val != l:target')
let l:bhidden = filter(copy(l:blisted), 'bufwinnr(v:val) < 0')
let l:bjump = (l:bhidden + l:blisted + [-1])[0]
if l:bjump > 0
execute 'buffer '.l:bjump
else
execute 'quit'.l:bang
endif
endif
endfor
execute 'bdelete'.l:bang.' '.l:target.' | quit'
if tabpagenr() > 1 && len(tabpagebuflist()) == 0
execute 'tabclose'.l:bang.' | tabprevious'
endif
endfunction " }}}
function! HLNext(switch) abort " {{{2
let l:action = a:switch
if l:action ==? 'set'
augroup HLNext
autocmd!
autocmd CursorMoved * call HLNext('move')
augroup END
return
elseif l:action ==? 'move'
augroup HLNext
autocmd!
augroup END
let l:action = 'on'
endif
if exists('w:HLNext_matchnum')
call matchdelete(w:HLNext_matchnum)
unlet! w:HLNext_matchnum
endif
if l:action ==? 'on'
let w:HLNext_matchnum = matchadd('HLNext', '\c\%#\%('.@/.'\)')
endif
endfunction " }}}
function! LongLineHL() abort " {{{2
if exists('w:longlines')
silent! call matchdelete(w:longlines)
unlet w:longlines
echo 'Long line highlight disabled'
else
let w:longlines = matchadd('ColorColumn', '\%81v', 100)
echo 'Long line highlight enabled'
endif
endfunction " }}}
function! ProfileStart(...) abort " {{{2
" optional args: logfile-path, copy-to-@*
if a:0 && a:1 != 1
let g:profile_file = a:1
else
let g:profile_file = '/tmp/vim.'.getpid().'.'.reltimestr(reltime())[-4:].'profile.txt'
echom 'Profiling into '.g:profile_file
if a:0 < 2 || a:2
let @* = g:profile_file
endif
endif
exec 'profile start '.g:profile_file
profile! file **
profile func *
endfunction
if len(get(g:, 'profile', ''))
call ProfileStart(g:profile)
endif
if 0
call ProfileStart(1, 0)
endif " }}}
" }}}
cat ~/.vimrc >> alforums
set mouse-=a
syntax on
set t_Co=16
augroup resCur
autocmd!
autocmd BufReadPost * call setpos(".", getpos("’""))
augroup END
I keep it pretty simple. Only text editor I use though.