]>
Commit | Line | Data |
---|---|---|
1 | """ paths | |
2 | "" config dir in xdg config | |
3 | let x = ($XDG_CONFIG_HOME??($HOME.'/.config')).'/vim' | |
4 | execute 'set runtimepath='.x.','.&runtimepath.','.x.'/after' | |
5 | execute 'set packpath='.x.','.&packpath.','.x.'/after' | |
6 | ||
7 | "" state files in xdg state | |
8 | let x = ($XDG_STATE_HOME??($HOME.'/.local/state')).'/vim' | |
9 | if !isdirectory(x) | |
10 | call mkdir(x, 'p', 0700) | |
11 | endif | |
12 | ||
13 | " backup | |
14 | " NOTE: keep backup files in vim state dir for both vim and neovim | |
15 | let y = x.'/backup' | |
16 | if !isdirectory(y) | |
17 | call mkdir(y, 'p', 0700) | |
18 | endif | |
19 | execute 'set backupdir='.y.'//' | |
20 | set backup backupcopy=yes | |
21 | ||
22 | " NOTE: neovim uses ShaDa (SHAred DAta) format for viminfo and swap, and uses a | |
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. since neovim has a nice default | |
26 | " for these directories and files, customizing them is not necessary. | |
27 | if !has('nvim') | |
28 | " viminfo | |
29 | execute 'set viminfofile='.x.'/.viminfo' | |
30 | ||
31 | " swap | |
32 | let y = x.'/swap' | |
33 | if !isdirectory(y) | |
34 | call mkdir(y, 'p', 0700) | |
35 | endif | |
36 | execute 'set directory='.y.'//' | |
37 | ||
38 | " undo | |
39 | let y = x.'/undo' | |
40 | if !isdirectory(y) | |
41 | call mkdir(y, 'p', 0700) | |
42 | endif | |
43 | execute 'set undodir='.y.'//' | |
44 | endif | |
45 | ||
46 | unlet x | |
47 | unlet y | |
48 | ||
49 | ||
50 | """ vim-only defaults | |
51 | if !has('nvim') | |
52 | " neovim defaults are already set | |
53 | source $VIMRUNTIME/defaults.vim | |
54 | ||
55 | " disable jumping to last edit, we use vim-lastplace instead | |
56 | autocmd! vimStartup | |
57 | ||
58 | " packages | |
59 | packadd! matchit " included with [n]vim but disabled by default | |
60 | packadd! editorconfig " now included with [n]vim but disabled by default | |
61 | packadd! vim-commentary " nvim has this built in now, vim still needs it | |
62 | ||
63 | " options | |
64 | set hlsearch | |
65 | autocmd BufRead * set autoindent | |
66 | endif | |
67 | ||
68 | ||
69 | """ styling | |
70 | " nice default theme | |
71 | colorscheme murphy | |
72 | ||
73 | " always use color column with less intrusive colors | |
74 | autocmd VimEnter,WinNew * set colorcolumn=80 | |
75 | " always use cursor line for better visibility | |
76 | autocmd VimEnter,WinNew * set cursorline | |
77 | ||
78 | " unique highlighting for leading spaces and in-line spaces | |
79 | " NOTE: this leaves tabs and other special whitespace untouched intentionally | |
80 | autocmd VimEnter,ColorScheme * highlight SpaceInner ctermfg=Grey guifg=#686868 | |
81 | autocmd VimEnter,ColorScheme * highlight link SpaceOuter NonText | |
82 | autocmd VimEnter,WinNew * match SpaceInner / / | |
83 | autocmd VimEnter,WinNew * 2match SpaceOuter /\(^ \+\)\|\( \+$\)/ | |
84 | ||
85 | " always show statusline | |
86 | set laststatus=2 | |
87 | ||
88 | " truncate > lastline; lastline is nvim default, sensible.vim may also set it | |
89 | set display=truncate | |
90 | ||
91 | " more context while scrolling | |
92 | set scrolloff=5 | |
93 | set sidescrolloff=8 | |
94 | ||
95 | " show number column on all buffers | |
96 | autocmd VimEnter,WinNew * set number | |
97 | " use relative numbers in normal modes when focused, but not if number is off | |
98 | function! s:SetRelativeNumber(enable) | |
99 | if !getwinvar(winnr(), '&number') | |
100 | return | |
101 | endif | |
102 | if a:enable | |
103 | set relativenumber | |
104 | else | |
105 | set norelativenumber | |
106 | endif | |
107 | endfunction | |
108 | autocmd InsertEnter,FocusLost,WinLeave * call s:SetRelativeNumber(0) | |
109 | autocmd VimEnter,WinNew, | |
110 | \InsertLeave,FocusGained,WinEnter * call s:SetRelativeNumber(1) | |
111 | ||
112 | " always show custom listchars | |
113 | autocmd VimEnter,WinNew * set list | |
114 | set listchars=tab:├─,extends:»,precedes:«,space:·,trail:∙,nbsp:■ | |
115 | ||
116 | ||
117 | """ editor | |
118 | " always save undo file for all file buffers | |
119 | autocmd BufReadPre * setlocal undofile | |
120 | ||
121 | " explicitly use modeline, even on systems where it's disabled by system vimrc | |
122 | autocmd BufRead * set modeline | |
123 | " should be default disabled but just in case | |
124 | set nomodelineexpr | |
125 | ||
126 | "" enable editorconfig parsing for new buffers | |
127 | let g:EditorConfig_enable_for_new_buf = 1 | |
128 | ||
129 | "" indent | |
130 | " always use shiftwidth instead of tabsize | |
131 | set smarttab | |
132 | " filetype preferences | |
133 | autocmd FileType python set softtabstop=4 shiftwidth=4 expandtab | |
134 | autocmd FileType markdown set softtabstop=4 shiftwidth=4 expandtab | |
135 | autocmd FileType javascript set softtabstop=2 shiftwidth=2 expandtab | |
136 | autocmd FileType json set softtabstop=2 shiftwidth=2 expandtab | |
137 | autocmd FileType html set softtabstop=2 shiftwidth=2 expandtab | |
138 | ||
139 | "" syntax | |
140 | autocmd Syntax php syn clear phpHereDoc phpNowDoc | |
141 | ||
142 | "" mappings | |
143 | " use more accessible escapes, as C-n and C-o are shadowed by some terminals | |
144 | if has('nvim') | |
145 | tnoremap <C-\>n <C-\><C-N> | |
146 | tnoremap <C-\>o <C-\><C-O> | |
147 | endif | |
148 | ||
149 | " move windows any time | |
150 | if 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 | |
156 | endif | |
157 | " insert | |
158 | inoremap <A-h> <C-\><C-N><C-w>h | |
159 | inoremap <A-j> <C-\><C-N><C-w>j | |
160 | inoremap <A-k> <C-\><C-N><C-w>k | |
161 | inoremap <A-l> <C-\><C-N><C-w>l | |
162 | " normal | |
163 | nnoremap <A-h> <C-w>h | |
164 | nnoremap <A-j> <C-w>j | |
165 | nnoremap <A-k> <C-w>k | |
166 | nnoremap <A-l> <C-w>l | |
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 | |
173 | if !has('nvim') && maparg('<C-L>', 'n') ==# '' | |
174 | nnoremap <silent> <C-L> :nohlsearch<C-R>=has('diff')?'<Bar>diffupdate':''<CR><CR><C-L> | |
175 | endif | |
176 | ||
177 | ||
178 | """ package config | |
179 | "" suda | |
180 | " https://github.com/lambdalisue/vim-suda/issues/32#issuecomment-829608925 | |
181 | if ! &diff | |
182 | let g:suda_smart_edit = 1 | |
183 | endif | |
184 | " re-enable backup, swap, undo for suda buffers that we can read | |
185 | " https://github.com/lambdalisue/vim-suda/issues/25 | |
186 | function s:SudaSettingsSave() | |
187 | let b:sev_suda_swapfile = &swapfile | |
188 | let b:sev_suda_undofile = &undofile | |
189 | " fix https://github.com/lambdalisue/vim-suda/issues/87 | |
190 | setlocal noswapfile | |
191 | endfunction | |
192 | function s:SudaSettingsRestore() | |
193 | if filereadable(expand('<afile>')[7:]) | |
194 | if exists('b:sev_suda_swapfile') && b:sev_suda_swapfile | |
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 | |
205 | endif | |
206 | if exists('b:sev_suda_undofile') && b:sev_suda_undofile | |
207 | setlocal undofile | |
208 | endif | |
209 | endif | |
210 | endfunction | |
211 | function s:SudaProcessUndo(cmd) | |
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 | |
260 | execute printf('%s %s/%s', a:cmd, fnameescape(d), fnameescape(f)) | |
261 | break | |
262 | catch | |
263 | continue | |
264 | endtry | |
265 | endfor | |
266 | endfunction | |
267 | autocmd BufReadPre suda://* call s:SudaSettingsSave() | |
268 | autocmd BufReadPost suda://* call s:SudaSettingsRestore() | |
269 | autocmd BufReadPost suda://* call s:SudaProcessUndo('silent! rundo') | |
270 | autocmd BufWritePost suda://* call s:SudaProcessUndo('wundo!') |