]> git.sev.monster Git - dotfiles.git/blame - etc/vim/.vimrc
zshrc: add more git stash aliases
[dotfiles.git] / etc / vim / .vimrc
CommitLineData
39d7f74f 1""" paths
2"" config dir in xdg config
afd175d7 3let x = ($XDG_CONFIG_HOME??($HOME.'/.config')).'/vim'
8d4a98e1 4execute 'set runtimepath='.x.','.&runtimepath.','.x.'/after'
5execute 'set packpath='.x.','.&packpath.','.x.'/after'
1b8a4102 6
39d7f74f 7"" state files in xdg state
afd175d7 8let x = ($XDG_STATE_HOME??($HOME.'/.local/state')).'/vim'
4850f4c2 9if !isdirectory(x)
afd175d7 10 call mkdir(x, 'p', 0700)
11endif
afd175d7 12
39d7f74f 13" backup
14" NOTE: keep backup files in vim state dir for both vim and neovim
afd175d7 15let y = x.'/backup'
16if !isdirectory(y)
17 call mkdir(y, 'p', 0700)
8d4a98e1 18endif
afd175d7 19execute 'set backupdir='.y.'//'
58a8dbcc 20set backup backupcopy=yes
afd175d7 21
58a8dbcc 22" NOTE: neovim uses ShaDa (SHAred DAta) format for viminfo and swap, and uses a
39d7f74f 23" different version of undo file, so these types of files when created by
24" vim cannot be used with neovim, and the 'viminfofile', 'directory', and
25" 'undodir' directories cannot be shared.
58a8dbcc 26" NOTE: neovim has a nice default for these directories and files so
27" customizing them is not necessary for it.
28if !has('nvim')
39d7f74f 29 " viminfo
58a8dbcc 30 " NOTE: 'viminfofile' is a deprecated alias for 'shada' in neovim
31 execute 'set viminfofile='.x.'/.viminfo'
32
39d7f74f 33 " swap
58a8dbcc 34 let y = x.'/swap'
35 if !isdirectory(y)
36 call mkdir(y, 'p', 0700)
37 endif
38 execute 'set directory='.y.'//'
39
39d7f74f 40 " undo
58a8dbcc 41 let y = x.'/undo'
42 if !isdirectory(y)
43 call mkdir(y, 'p', 0700)
44 endif
45 execute 'set undodir='.y.'//'
8d4a98e1 46endif
58a8dbcc 47
4850f4c2 48unlet x
afd175d7 49unlet y
1b8a4102 50
0bffb4be 51
39d7f74f 52""" vim-only defaults
53if !has('nvim')
54 " neovim defaults are already set
55 source $VIMRUNTIME/defaults.vim
b3b23234 56
39d7f74f 57 " disable jumping to last edit, we use vim-lastplace instead
58 autocmd! vimStartup
58a8dbcc 59
39d7f74f 60 " packages
01aaaf66 61 packadd! matchit " included with [n]vim but disabled by default
62 packadd! editorconfig " now included with [n]vim but disabled by default
63 packadd! vim-commentary " nvim has this built in now, vim still needs it
39d7f74f 64
65 " options
66 set hlsearch
67 autocmd BufRead * set autoindent
68endif
69
0bffb4be 70
39d7f74f 71""" styling
72" nice default theme
73colorscheme murphy
74
75" always use color column with less intrusive colors
58a8dbcc 76autocmd VimEnter,WinNew * set colorcolumn=80
39d7f74f 77" always use cursor line for better visibility
58a8dbcc 78autocmd VimEnter,WinNew * set cursorline
58a8dbcc 79
39d7f74f 80" unique highlighting for leading spaces and in-line spaces
81" NOTE: this leaves tabs and other special whitespace untouched intentionally
1ac43be8 82autocmd VimEnter,ColorScheme * highlight SpaceInner ctermfg=Grey guifg=#686868
83autocmd VimEnter,ColorScheme * highlight link SpaceOuter NonText
84autocmd VimEnter,WinNew * match SpaceInner / /
85autocmd VimEnter,WinNew * 2match SpaceOuter /\(^ \+\)\|\( \+$\)/
39d7f74f 86
87" always show statusline
88set laststatus=2
89
90" truncate > lastline; lastline is nvim default, sensible.vim may also set it
91set display=truncate
92
93" more context while scrolling
94set scrolloff=5
95set sidescrolloff=8
96
97" show number column on all buffers
98autocmd VimEnter,WinNew * set number
99" use relative numbers in normal modes when focused, but not if number is off
100function! s:SetRelativeNumber(enable)
101 if !getwinvar(winnr(), '&number')
102 return
103 endif
104 if a:enable
105 set relativenumber
106 else
107 set norelativenumber
108 endif
109endfunction
110autocmd InsertEnter,FocusLost,WinLeave * call s:SetRelativeNumber(0)
111autocmd VimEnter,WinNew,
112 \InsertLeave,FocusGained,WinEnter * call s:SetRelativeNumber(1)
113
114" always show custom listchars
58a8dbcc 115autocmd VimEnter,WinNew * set list
1b8a4102 116set listchars=tab:├─,extends:»,precedes:«,space:·,trail:∙,nbsp:■
58a8dbcc 117
0bffb4be 118
39d7f74f 119""" editor
120" always save undo file for all file buffers
01aaaf66 121autocmd BufReadPre * setlocal undofile
122
ec9f2140 123" explicitly use modeline, even on systems where it's disabled by system vimrc
6ebc1fca 124autocmd BufRead * set modeline
ec9f2140 125" should be default disabled but just in case
126set nomodelineexpr
127
39d7f74f 128"" indent
129" always use shiftwidth instead of tabsize
130set smarttab
131" filetype preferences
58a8dbcc 132autocmd FileType python set softtabstop=4 shiftwidth=4 expandtab
133autocmd FileType markdown set softtabstop=4 shiftwidth=4 expandtab
134autocmd FileType javascript set softtabstop=2 shiftwidth=2 expandtab
135autocmd FileType json set softtabstop=2 shiftwidth=2 expandtab
136autocmd FileType html set softtabstop=2 shiftwidth=2 expandtab
e69caf64 137let g:EditorConfig_enable_for_new_buf = 1
58a8dbcc 138
39d7f74f 139"" syntax
fbdf3d0e 140autocmd Syntax php syn clear phpHereDoc phpNowDoc
b3b23234 141
39d7f74f 142"" mappings
143" use more accessible escapes, as C-n and C-o are shadowed by some terminals
b3b23234 144if has('nvim')
145 tnoremap <C-\>n <C-\><C-N>
146 tnoremap <C-\>o <C-\><C-O>
147endif
148
39d7f74f 149" move windows any time
b3b23234 150if has('nvim')
151 " terminal
152 tnoremap <A-h> <C-\><C-N><C-w>h
153 tnoremap <A-j> <C-\><C-N><C-w>j
154 tnoremap <A-k> <C-\><C-N><C-w>k
155 tnoremap <A-l> <C-\><C-N><C-w>l
156endif
157" insert
158inoremap <A-h> <C-\><C-N><C-w>h
159inoremap <A-j> <C-\><C-N><C-w>j
160inoremap <A-k> <C-\><C-N><C-w>k
161inoremap <A-l> <C-\><C-N><C-w>l
162" normal
163nnoremap <A-h> <C-w>h
164nnoremap <A-j> <C-w>j
165nnoremap <A-k> <C-w>k
166nnoremap <A-l> <C-w>l
39d7f74f 167
168"" CTRL-L to clear highlighting and also update diff
169" NOTE: sensible.vim and nvim already do this, so copy sensible.vim
170" functionality if it hasn't been set or we aren't nvim. taken from
171" sensible.vim by Tim Pope, under Vim license; see :help license
172" https://github.com/tpope/vim-sensible/blob/0ce2d843d6f588bb0c8c7eec6449171615dc56d9/plugin/sensible.vim#L57
173if !has('nvim') && maparg('<C-L>', 'n') ==# ''
174 nnoremap <silent> <C-L> :nohlsearch<C-R>=has('diff')?'<Bar>diffupdate':''<CR><CR><C-L>
175endif
8aac59d6 176
177
178""" package config
179"" suda
180" https://github.com/lambdalisue/vim-suda/issues/32#issuecomment-829608925
181if ! &diff
182 let g:suda_smart_edit = 1
183endif
184" re-enable backup, swap, undo for suda buffers that we can read
185" https://github.com/lambdalisue/vim-suda/issues/25
186function s:SudaSettingsSave()
6cd24ad1 187 let b:sev_suda_swapfile = &swapfile
188 let b:sev_suda_undofile = &undofile
b758c100 189 " fix https://github.com/lambdalisue/vim-suda/issues/87
190 setlocal noswapfile
8aac59d6 191endfunction
192function s:SudaSettingsRestore()
193 if filereadable(expand('<afile>')[7:])
6cd24ad1 194 if exists('b:sev_suda_swapfile') && b:sev_suda_swapfile
b758c100 195 try
196 setlocal swapfile
197 catch
198 " ignore swapfile errors, they should have been shown already
199 echohl ErrorMsg
200 for line in split(v:exception, '\n')
201 echomsg printf('[suda] %s', line)
202 endfor
203 echohl None
204 endtry
8aac59d6 205 endif
6cd24ad1 206 if exists('b:sev_suda_undofile') && b:sev_suda_undofile
8aac59d6 207 setlocal undofile
208 endif
209 endif
210endfunction
b758c100 211function s:SudaProcessUndo(cmd)
3dc35911 212 let p = expand('<afile>')[7:]
213 if has('win32') || !&undofile || !filereadable(p)
214 return
215 endif
216 let p = resolve(p)
217 " XXX: comments are from :help backupdir to mark implementation details
218 let x = &undodir
219 " For backwards compatibility with Vim version 3.0 a '>' at the start
220 " of the option is removed.
221 if stridx(x, '>') == 0
222 let x = x[1:]
223 endif
224 " To include a comma in a directory name precede it with a backslash.
225 for u in split(x, '\v\\@1<!,')
226 " Spaces after the comma are ignored, other spaces are considered part
227 " of the directory name.
228 let u = trim(u, ' ', 1)
229 " To have a space at the start of a directory name, precede it with a
230 " backslash.
231 if stridx(u, '\ ') == 0
232 let u = u[1:]
233 endif
234 " Empty means that no backup file will be created.
235 if empty(u)
236 continue
237 elseif u == '.'
238 " A directory "." means to put the backup file in the same
239 " directory as the edited file.
240 let d = fnamemodify(p, ':p:h')
241 elseif stridx(u, './') == 0
242 " A directory starting with "./" (or ".\" for MS-Windows) means to
243 " put the backup file relative to where the edited file is.
244 let d = printf('%s%s', fnamemodify(p, ':p:h'), expand(u))
245 else
246 let d = u
247 endif
248 " NOTE: env vars are not expanded, and backslashes are not handled...
249 if u[-2:-1] == '//'
250 let f = fnamemodify(p, ':p:gs?/?%?')
251 else
252 let f = printf('.%s.un~', fnamemodify(p, ':t'))
253 endif
254 " A directory name may end in an '/'.
255 let d = trim(d, '/', 2)
256 if !isdirectory(d)
257 continue
258 endif
259 try
b758c100 260 execute printf('%s %s/%s', a:cmd, fnameescape(d), fnameescape(f))
3dc35911 261 break
262 catch
263 continue
264 endtry
265 endfor
266endfunction
8aac59d6 267autocmd BufReadPre suda://* call s:SudaSettingsSave()
268autocmd BufReadPost suda://* call s:SudaSettingsRestore()
b758c100 269autocmd BufReadPost suda://* call s:SudaProcessUndo('silent! rundo')
270autocmd BufWritePost suda://* call s:SudaProcessUndo('wundo!')
This page took 0.13544 seconds and 4 git commands to generate.