aboutsummaryrefslogtreecommitdiff
path: root/vimrc-common
blob: 239cc91c7033985da5c8902167ec45ebfe39a9af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
"=======================================================================================================================
" GENERAL:
" It is likely, that you will want to overwrite these settings with a
" project-local .vimrc file (activated with exrc setting)
"=======================================================================================================================

set exrc                          | " enable exrc, a specific .exrc per project, which can contain usual .vimrc commands
set modeline                      | " set variables specific to a file, like indentation by adding a comment
set textwidth=0                   | " better done with modeline or local exrc and not here
set ts=4 sts=4 sw=4 expandtab     | " better done with a modeline or local exrc
set virtualedit=all               | " virtual edit should be default behaviour, because I don't see any reason against
set nonumber norelativenumber     | " do not show numbers by default, because that causes a performance loss, instead activate them on a file type basis
set ignorecase smartcase          | " search with ignore case by default, but use case sensitive search when one capital char is contained and highlight while typing (even though its slower)
set hlsearch incsearch            | " highlight pattern while entering it (performance wise this isn't that good)
" set cindent cinoptions+=(0        | " indent at parentheses

set path+=**                      | " allow recursive searches for files
let &path = &path.",/usr/lib/modules/".substitute(system('uname -r'), "\n", "", "")."/build/include"

filetype on
filetype plugin on
filetype indent on

syntax on                         | " enable syntax highlighting
syntax sync minlines=60           | " how many preceding lines will be parsed? (has performance impact)

"=======================================================================================================================
" SPELL_CHECKING:
"=======================================================================================================================
let g:spellfile_URL='http://ftp.vim.org/vim/runtime/spell'
" add local user default spell file as primary source for words
let &spellfile=fnamemodify($MYVIMRC, ":p:h")."/spell/spellfile-user.UTF-8.add"

set nospell          | " disable spell checker by default
set spelllang=en,de  | " languages for the spell checker
set spellsuggest=10  | " how many words will z= suggest?
set thesaurus+=~/.vim/thesaurus/php.txt


"=======================================================================================================================
" UNDO:
" Persistent undo behaviour, also keeps undo history between starts
"=======================================================================================================================
if has('persistent_undo')
  if isdirectory('/dev/shm')
    set undodir=/dev/shm/     | " save undo file in memory. That is volatile, but fast and we have GIT for longer lasting undoes
    set directory=/dev/shm/   | " swap file directory to RAM
    set swapfile
  elseif isdirectory('/tmp/')
    set undodir=/tmp/
  endif
  set undofile                  | " preserve undo history when closing and reopening buffers (see :help undo-persistence)
endif


"=======================================================================================================================
" MULTI_BYTE:
"=======================================================================================================================
if has("multi_byte")
  scriptencoding utf-8        | " tell vim that we are using UTF-8 here
  set encoding=utf-8          | " we need default UTF-8 encoding to use cool chars as line break and so on (see below)
  set termencoding=utf-8      | " we just assume that this is supported.

  set fillchars=              | " initialize empty fillchars
  set listchars=              | " initialize empty listchars

  if &term ==# 'linux'
    set fillchars+=vert:\│    | " cool vertical split char
  else
    set fillchars+=vert:\║    | " cool vertical split char
  endif

  set fillchars+=fold:\       | "
  set fillchars+=diff:\       | " a white space gets used here

  set listchars+=extends:»    | " symbols used when using :set list (which displays non-printable chars)
  set listchars+=precedes:«   | " symbols used when using :set list (which displays non-printable chars)

  set listchars+=tab:▏\       | "
  set listchars+=trail:·      | " symbols used when using :set list (which displays non-printable chars)
  " set listchars+=eol:↲      | " symbols used when using :set list (which displays non-printable chars)
  " set listchars+=space:·    | " symbols used when using :set list (which displays non-printable chars)
  "set showbreak+=›            | " symbol used in the beginning of a wrapped line

  " automatically enter list mode when going in insert mode (makes above syntax command temporarily ineffective)
  set nolist  

  autocmd InsertEnter *  set list
  autocmd InsertLeave *  set list&


  autocmd InsertEnter *  set colorcolumn=80,120
  autocmd InsertLeave *  set colorcolumn&


  " set fillchars+=stlnc:\―       | "
end

" ======================================================================================================================
"  SETTINGS:
" ======================================================================================================================
set breakindent               | " Every wrapped line will continue visually indented
set clipboard=unnamedplus     | " makes copy and paste work (autoselectplus might work as well)
set concealcursor=nc          | " limits the display of concealed text to normal and command mode
set conceallevel=2            | " replace escaped chars by their UTF-8 representation (useful for LaTeX)
set confirm                   | " asks 'do you want to save?'
set cpoptions+=P              | " makes :w filename set the current buffer to filename
set hidden                    | " allows switching buffers even if the current buffer contains changes (displays +)
set linebreak                 | " wrap long lines at char 'breakat', not inside words
set mousemodel=popup          | " only in gvim: right click opens a pop-up-menu
set mouse=n                   | " allow mouse in normal mode only, so one can use the terminals c&p feature in insert mode
set noautochdir               | " When on, Vim will change the current working directory
set nostartofline             | " when scrolling: do not move the cursor to column 1
set nowrap                    | " but do not (by default) wrap long lines around
set nrformats+=alpha          | " allows CTRL-A & CTRL-X to increment and decrement letters, not just numbers

if has('nvim') " Neovim?
  set inccommand=nosplit      | " preview substitute and such things in real time
endif

set pumheight=8               | " Determines the maximum number of items to show in the pop-up menu for
set scrolljump=4              | " how many lines get scrolled into view when cursor reaches the screens edge
set scrolloff=4               | " keeps cursor centered
set shiftround                | " indent/un-indent snaps to multiple of shiftwidths
set writedelay=0

" display and performance
set lazyredraw                | " disables redraw during macro execution (improves performance)
set cmdheight=2               | " sets the command line's height
set signcolumn=auto           | " auto=auto hide, yes=always, no=never show the column with error indicators
set nocursorcolumn            | " turn visual cursor column off (improves performance)
set updatetime=80             | " updates the screen more often
set redrawtime=1500           | " Timeout in milliseconds for redrawing the screen (switches syntax off when ssh too slow) / CTRL+L to retry
set notimeout                 | " improves performance but is known to cause problems on slow terminals
set ttimeout ttimeoutlen=150  | " set Esc key timeout in ms-
set showcmd                   | " essential: show keys of combined commands in the lower right corner (BUT SLOW, makes cursor flickering)
set showtabline=2             | " 0: never, 1: only if there are at least two tabs, 2:always
set shortmess+=I              | " don't give the intro message when starting Vim |:intro|.
set wildmenu                  | " use a menu in the command line
set wildmode=longest:full     | " do not preselect any entry and show all possible

" code completion
" set dictionary=/usr/share/dict/cracklib-small
set complete+=d               | " scan current and included files for defined name or macro
set complete+=i               | " scan current and included files for completions
set complete+=k               | " make default completer <C-N> respect the dictionary
set complete-=u               | " scan current and included files
set complete+=i               | " scan current and included files
set complete+=d               | " scan current and included files for defined name or macro
" set complete=d                | " scan current and included files for defined name or macro
set completeopt+=noinsert     | " Do not insert any text for a match until the user selects one
set completeopt+=noselect     | " Do not select a completion from the menu, let the user do that
set tagcase=match             | " tagcase match, because we mostly use ^] to jump around and that variant respects the upper/lower case [followscs, followic, match, ignore]
set tags+=../tags

" code folding...
set nofoldenable              | " disable folding, because we have zi to toggle foldenable :)
set foldclose=all             | " automatically fold, when the cursor leaves the folded area
set foldcolumn=2              | " I think I don't need this second indicator
" set numberwidth=5
" set foldmethod=syntax         | " foldlevel: syntax, indent, manual / foldmethod=syntax makes Vim incredible slow
set foldnestmax=1             | " top level folding only
set foldopen=block,hor,search | " when do we unfold?
set foldtext=printf('%*s%.*S',indent(v:foldstart),'',&textwidth-indent(v:foldstart),substitute(substitute(join(getline(v:foldstart,v:foldend),'\ '),'[[:space:]*/]\\+','\ ','g'),'^[[:space:]*]','','g'))

" works ...
" set foldexpr=match(synIDattr(synID(v:lnum,indent(v:lnum)+1,0),'name'),'Comment')>-1
set foldexpr=!empty(filter(synstack(v:lnum,indent(v:lnum)+1),{_,val->match(synIDattr(val,'name'),'Comment')>-1}))


" vim window behaviour
set splitbelow                | " open new windows below the current one (i find that more intuitive)
set splitright                | " this also works for me and makes better use of the scren space I think
set winminwidth=0             | " (and all other windows, so TODO: watch out)
set winwidth=30               | " keep NERDTreeWindow at least this size


" vim session handling and restore behaviour
set viminfo+=%                | " restore buffer list
set sessionoptions=
set sessionoptions+=buffers
set sessionoptions+=curdir
set sessionoptions+=folds
set sessionoptions+=resize
set sessionoptions+=slash
set sessionoptions+=tabpages
set sessionoptions+=unix
set sessionoptions+=winpos
set sessionoptions+=winsize

" set nocindent smartindent     | " use smart indent rather then cindent
set noautoindent
set nosmartindent

set noshiftround              | " indent/un-indent sna=ps to multiple of shiftwidths
set noequalalways             | " do not evenly size windows when opening new or closing old
set nocursorline              | " turn visual cursor line off (improves performance)

"=======================================================================================================================
if has("autocmd")
  " use the shada/viminfo file to return the cursor to where it was...
  autocmd BufReadPost        *  call setpos(".", getpos("'\""))
  autocmd BufWinEnter        *  if &previewwindow | setlocal nonumber nolist signcolumn=no filetype=c nobuflisted | endif

  " Workaround: Allows Vim to yank text within wayland
  " XDG_SESSION_TYPE is only set if a login manager was used
  " WAYLAND_DISPLAY should always work
  if ($XDG_SESSION_TYPE ==# 'wayland' || ! empty($WAYLAND_DISPLAY)) && !has('nvim')
    autocmd TextYankPost       * call system("wl-copy", getreg('+'))
  endif 

  autocmd TextYankPost       *  echo '> text yanked to '.
        \ (get(v:event,'regname') == ''
        \ ? 'default register'
        \ : 'register '.get(v:event,'regname'))


  autocmd InsertLeave * silent! call matchadd('Convention', ' \+$', -1, 101, { 'conceal': '⟶' })
  autocmd InsertEnter * silent! call matchdelete(101)


  "====================================================================================================================
  augroup FILETYPES
    " indent within <script> and <style> (default is a zero indent)
    let g:html_indent_script1                    = "inc"
    let g:html_indent_style1                     = "inc"

    " let g:loaded_ruby_provider                   = 1 " disable ruby support
    " let g:loaded_python_provider                 = 1 " disable python 3

    let g:LatexBox_latexmk_preview_continuously  = 1
    let g:LatexBox_viewer                        = "evince"

    autocmd FileType python               setlocal keywordprg=pydoc
    autocmd FileType vim                  setlocal keywordprg=:help |.
    autocmd FileType conf                 setlocal isfname-==
    autocmd Filetype css                  command! CSSsort :g/{/+1;/}/-1 sort
    autocmd Filetype html,markdown,xml    iabbrev </ </<C-X><C-O>

    autocmd Filetype html,htmldjango,xml
          \ :nnoremap
          \ <M-Down>
          \ :call search('^ *<', 'e')<CR>:nohlsearch<CR>|
          \ :nnoremap
          \ <M-Up>
          \ :call search('^ *<', 'eb')<CR>:nohlsearch<CR>|
          \ :nnoremap
          \ <leader>=
          \ vat:'<,'>!tidy -xml --wrap 0 --sort-attributes alpha 2>/dev/null<CR>vat=

    if filereadable("/usr/bin/vendor_perl/ack")
      autocmd FileType c,cpp  set grepprg=/usr/bin/vendor_perl/ack\ --type=cc\ --nogroup\ --column\ $*
      autocmd FileType c,cpp  set grepformat=%f:%l:%c:%m
    endif
  augroup END
endif

" ======================================================================================================================
augroup CUSTOM_COMMANDS
  command! Vimls
        \ call setloclist(0, map(getbufinfo({'buflisted':1}),
        \ "{'bufnr': v:val.bufnr,
        \   'lnum': v:val.lnum,
        \   'text': '='.printf('%*s, % 3d: %s [%s]', winwidth(0) / 2, '', v:val.bufnr, v:val.name, getbufvar(v:val.bufnr, '&buftype')),
        \   'pattern': 'not loaded'}
        \ "))
  command! Qfls call setqflist([],'r',{ 'title':'listed buffers', 'items':map(getbufinfo({"buflisted":1}), '{"bufnr":get(v:val,"bufnr"),"lnum":get(v:val,"lnum"),"module":printf("%-*s",winwidth(0)-4,fnamemodify(get(v:val,"name"),":t"))}') })
  command! Ctoggle
        \ if(get(getqflist({'winid':1}), 'winid') == win_getid())|cclose|else|botright copen|endif
  command! Ltoggle
        \ if(get(getloclist(0, {'winid':1}), 'winid') == win_getid())|lclose|else|lopen|endif
  command! BuffersToArg :exec ':args '.join(map(range(0, bufnr('$')), 'fnameescape(fnamemodify(bufname(v:val), ":."))'))
  command! BufToArg :argadd %:.
  command! Gbranch call setqflist([], 'r', {'title':'Git branch selector','items':map(systemlist("git branch"), {_, p->{'filename':'branch','module': fnamemodify(p, ':.')}})})
  " the following command opens a preview-window and shows the declaration of
  " the function under the cursor. It also highlights the word to make it easier
  " to spot within a great file
  command! Helpme au! CursorHold * nested let @/=expand('<cword>')|exe "silent! psearch ".expand("<cword>")
  command! FindInAllBuffers  cex [] | bufdo vimgrepadd //g % | cw
augroup END

"=======================================================================================================================
augroup KEYBOARD_MAPPING
  " map CTRL-PageUp/Down to next/previous buffer
  " and Shift-PageUp/Down to next/previous arglist file
  nnoremap <C-PageUp>    :bn<CR>
  nnoremap <C-PageDown>  :bp<CR>
  nnoremap <S-PageUp>    :N<CR>
  nnoremap <S-PageDown>  :n<CR>


  nnoremap <F4>          :wincmd c<CR>

  nnoremap <F5>          :make!<CR>
  nnoremap <F6>          :silent syntax sync fromstart<CR>:nohlsearch<CR>:silent match<CR>:silent 2match<CR>:silent 3match<CR>
  nnoremap <F7>          :Ltoggle<CR>
  nnoremap <F8>          :Ctoggle<CR>

  nnoremap <F9>          :TagbarToggle<CR>
  nnoremap <F12>         :Vimls<CR>:Ltoggle<CR>

  " close current buffer with <leader>q...
  nnoremap <leader>q           :bp<bar>sp<bar>bn<bar>bd<CR>.
  nnoremap <leader>r           :syntax sync fromstart

  nnoremap <silent> <A-Up>     :wincmd k<CR>
  nnoremap <silent> <A-Down>   :wincmd j<CR>
  nnoremap <silent> <A-Left>   :wincmd h<CR>
  nnoremap <silent> <A-Right>  :wincmd l<CR>


  inoremap <C-S>               <C-O>:w<CR>

  " exec current line as a command, insert output of command (from: https://youtu.be/MquaityA1SM?t=35m45s)
  nnoremap <leader>Q     !!$SHELL<CR>
  " google the word under the cursor
  nnoremap <leader>g     :execute ":!xdg-open https://google.de/search?q=".expand("<cword>")<CR>
  nnoremap <leader>u     :execute ":!xdg-open ".expand("<cWORD>")<CR>
  " display highlight group under the cursor
  nnoremap <leader>h     :echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')<CR>


  " ======================================================================================================================
  " SHORTCUTS: custom shortcuts
  " inoremap <C-Space> <C-x><C-o>
  " inoremap <C-@> <C-Space>

  " Bind CTRL+Backspace to vim's version (CTRL+W) in " <CR> insert mode (only works with gvim)
  inoremap <C-Backspace>  <C-W>

  " INDENTATION: allows un-indenting a selected block and keeps selection
  vnoremap <  <gv
  vnoremap >  >gv

  " make shift-home select to the beginning of the line
  nnoremap <s-home>  v^
  nnoremap <s-end>   v$

  nnoremap <s-down>  vj
  vnoremap <s-down>  j
  nnoremap <s-up>    vk
  vnoremap <s-up>    k

  " if the wildmenu is configured to to display suggestions vertically, then
  " reconfigure the cursor keys to work in that direction
  if &wildoptions =~# 'pum'
    cnoremap <expr> <up>   pumvisible() ? "<C-p>" : "\<up>"
    cnoremap <expr> <down> pumvisible() ? "<C-n>" : "\<down>"
  endif


  " INSERT_MODE_MAPPINGS:
  " default copy&paste insert key binding (just in insert mode, so it doesn't conflict
  " with visual block mode)- would have been nice, but collides with c-w for digraphs
  " inoremap <C-V> <C-R>+

  " NEOVIM_SPECIFIC:
  if has('nvim') " only neovim...
    " shortcut \t opens a terminal in a horizontal split
    nnoremap <leader>t :new +terminal<CR>
  endif
augroup END

"=======================================================================================================================
" v modeline, do not chnage v
" vim: noai:ts=2:sw=2:sts=2 iskeyword+=\:,\<,\>,\-,\& number
..