update for migration
[dotfiles_afify.git] / .vimrc
blob3e772c879e51ea00695fc93103866185528feb44
1 "==============================================================================
2 " Main
3 "==============================================================================
4 set nocompatible
5 let mapleader=" "
6 set shell=bash
7 set encoding=utf-8
8 set fileformat=unix
9 set noswapfile
10 set backspace=indent,eol,start
12 "==============================================================================
13 " Plugins
14 "==============================================================================
15 source ~/.vim/comment.vim
17 call plug#begin('~/.vim/plugged')
18 Plug 'airblade/vim-gitgutter'
19 Plug 'maralla/completor.vim'
20 Plug 'vim-syntastic/syntastic'
21 call plug#end()
23 "==============================================================================
24 " Display
25 "==============================================================================
26 colorscheme minimal
27 set list
28 set listchars=tab:>—,nbsp:␣,trail:.
29 set tabstop=4
30 set shiftwidth=4
31 set noexpandtab
32 set splitbelow splitright
33 set wildmenu
34 set wildmode=list:full,full
35 set ignorecase
36 set smartcase
38 autocmd FileType python  set tabstop=4 softtabstop=4 shiftwidth=4 textwidth=79 expandtab autoindent foldmethod=indent
39 autocmd FileType c      setlocal ts=8 sw=8
40 autocmd FilterWritePre * if &diff | set foldcolumn=0 | endif
42 " set noautoindent
43 " set linebreak
44 filetype plugin indent on
46 "==============================================================================
47 " Keymaping
48 "==============================================================================
49 "copy to global clipboard
50 vmap <C-c> y:call system("xclip -i -selection clipboard", getreg("\""))<cr>:call system("xclip -i", getreg("\""))<cr>
52 " replace word with yanked
53 nnoremap <C-p> cw<C-r>0<ESC>
55 " paste
56 nnoremap <C-c> :set invpaste paste?<CR>
57 set pastetoggle=<C-c>
58 set showmode
60 "Move lines up and down with controle + k or j
61 nnoremap <silent> <C-j> :move +1 <CR>
62 nnoremap <silent> <C-k> :move -2 <CR>
64 "Disable keys in Normal mode
65 no <Up>        <Nop>
66 no <Down>      <Nop>
67 no <Left>      <Nop>
68 no <Right>     <Nop>
69 no <Space>     <Nop>
70 no <PageUp>    <Nop>
71 no <PageDown>  <Nop>
72 no <BackSpace> <Nop>
73 no <F1>        <Nop>
74 no q           <Nop>
75 no Q           <Nop>
77 "Disable keys in Insert mode
78 ino <Up>       <Nop>
79 ino <Down>     <Nop>
80 ino <PageUp>   <Nop>
81 ino <PageDown> <Nop>
82 ino <End>      <Nop>
83 ino <Home>     <Nop>
84 ino <F1>       <Nop>
86 " Add new line
87 noremap J o<esc>k
88 noremap K O<esc>j
90 " Go (start|end) of line
91 noremap H ^
92 noremap L $
94 " Split
95 nnoremap <Leader><CR> <Esc>:vsplit
97 nnoremap <silent> <Leader>= :vertical resize +1<CR>
98 nnoremap <silent> <Leader>- :vertical resize -1<CR>
99 nnoremap <silent> <Leader>0 :vertical resize 87<CR>
101 nnoremap <Left>  <C-w>h
102 nnoremap <Down>  <C-w>j
103 nnoremap <Up>    <C-w>k
104 nnoremap <Right> <C-w>l
106 " Move the split
107 nnoremap <Leader><Left>  <C-w>H
108 nnoremap <Leader><Down>  <C-w>J
109 nnoremap <Leader><Up>    <C-w>K
110 nnoremap <Leader><Right> <C-w>L
112 " Search
113 nnoremap <Leader>f :set hlsearch<CR>/^[a-z*].*(.*)$<CR>
114 nnoremap <Leader>o :set hlsearch!<CR>/\%80v.\+/<CR>
115 nnoremap <Leader>s :set hlsearch!<CR>
116 nnoremap * *zz
117 nmap n nzz
119 " Auto close
120 " inoremap ( ()<Left>
121 " inoremap { {}<Left>
122 " inoremap [ []<Left>
123 " inoremap " ""<Left>
124 " inoremap ' ''<Left>
126 " Quit
127 nnoremap <Leader>q :qa!<CR>
129 " Fold
130 nnoremap <Leader>z zM
131 nnoremap <Leader>Z zR
133 " Syntastic
134 nnoremap <Leader>a :SyntasticCheck<CR>
135 nnoremap <Leader>A :SyntasticToggleMode<CR>
136 nnoremap <Leader>n :lnext<CR>
138 " Custom functions
139 nnoremap <Leader>c :call Clean_file()<CR><CR>
141 nnoremap <Leader>r :set rightleft<CR>
142 nnoremap <Leader>R :set norightleft<CR>
144 " Git
145 nnoremap <Leader>g :GitGutterSignsToggle<CR>
146 nmap <Enter> <Plug>(GitGutterPreviewHunk)
148 " surround word
149 nnoremap <Leader>; i"<ESC>lea"<ESC>l
150 nnoremap <Leader>' i"<ESC>A"<ESC>l
151 nnoremap <Leader>[ i[<ESC>lea]<ESC>l
152 nnoremap <Leader>] i{<ESC>lea}<ESC>l
154 " Enter command mode
155 noremap ; :
157 "==============================================================================
158 " Status Line
159 "==============================================================================
160 set laststatus=2
161 set statusline=\ %F%#StatusLine_g#\ %m%#StatusLine_r#\ %r%*%=
162 set statusline+=%y\ %{&encoding}\ %{&fileformat}\ %c
164 " Warnings
165 set statusline+=%#StatusLine_o#
166 set statusline+=%{StatuslineTabWarning()}
167 set statusline+=%{StatuslineTrailingSpaceWarning()}
168 set statusline+=%*
170 "recalculate the tab warning flag when idle and after writing
171 autocmd cursorhold,bufwritepost * unlet! b:statusline_tab_warning
172 autocmd cursorhold,bufwritepost * unlet! b:statusline_trailing_space_warning
174 function! StatuslineTrailingSpaceWarning()
175         if !exists("b:statusline_trailing_space_warning")
176                 let result = search('\s\+$', 'nw') != 0
177                 if result
178                         let b:statusline_trailing_space_warning = ' [TS]'
179                 else
180                         let b:statusline_trailing_space_warning = ''
181                 endif
182         endif
183         return b:statusline_trailing_space_warning
184 endfunction
186 function! StatuslineTabWarning()
187         if !exists("b:statusline_tab_warning")
188                 let b:statusline_tab_warning = ''
189                 if !&modifiable
190                         return b:statusline_tab_warning
191                 endif
192                 let tabs = search('^\t', 'nw') != 0
193                 "find spaces that arent used as alignment in the first indent column
194                 let spaces = search('^ \{' . &ts . ',}[^\t]', 'nw') != 0
195                 if tabs && spaces
196                         let b:statusline_tab_warning =  ' [MI]'
197                 elseif (spaces && !&et) || (tabs && &et)
198                         let b:statusline_tab_warning = '[&et]'
199                 endif
200         endif
201         return b:statusline_tab_warning
202 endfunction
204 "==============================================================================
205 " Linting
206 "==============================================================================
207 let g:syntastic_python_checkers = ["pylint"]
208 let g:syntastic_sh_checkers     = ["shellcheck"]
209 let g:syntastic_c_checkers      = ["splint"]
210 " Moderately strict checking
211 let g:syntastic_c_splint_args   = "-checks +posixlib ./util.c -DVERSION=\\\"VERSION\\\""
213 set statusline+=%#statusline_r#
214 set statusline+=\ %{SyntasticStatuslineFlag()}
215 set statusline+=%*
217 let g:syntastic_check_on_wq              = 0
218 let g:syntastic_auto_loc_list            = 0
219 let g:syntastic_check_on_open            = 1
220 let g:syntastic_enable_highlighting      = 0 "underline word
221 let g:syntastic_always_populate_loc_list = 1
222 let g:syntastic_auto_jump                = 0
224 " Symbols icon
225 let g:syntastic_error_symbol         = ''
226 let g:syntastic_warning_symbol       = ''
227 let g:syntastic_style_error_symbol   = ''
228 let g:syntastic_style_warning_symbol = '!'
230 "==============================================================================
231 " Folding
232 "==============================================================================
233 " set nofen
234 set foldnestmax=1
235 set foldmethod=syntax
237 function! MyFoldText() " {{{
238 let line = getline(v:foldstart)
239 let nucolwidth = &fdc + &number * &numberwidth
240 let windowwidth = winwidth(0) - nucolwidth - 3
241 let foldedlinecount = v:foldend - v:foldstart
242 let onetab = strpart('          ', 0, &tabstop)
243 let line = substitute(line, '\t', onetab, 'g')
244 let line = strpart(line, 0, windowwidth - 2 -len(foldedlinecount))
245 let fillcharcount = 81 - len(line) - len(foldedlinecount)
246 let rest  = windowwidth - fillcharcount
247 return line . repeat(" ",fillcharcount) . foldedlinecount .' ' . repeat(" ", rest)
248 endfunction " }}}
250 set foldtext=MyFoldText()
252 "==============================================================================
253 " File type highlight
254 "==============================================================================
255 setf dosini
256 augroup filetypedetect
257         autocmd BufNewFile,BufRead *.h     set filetype=c
258         autocmd BufNewFile,BufRead *.csv   set filetype=csv
259         autocmd BufNewFile,BufRead *.gpg   set filetype=gpg
260         autocmd BufNewFile,BufRead LICENSE set filetype=text
261         autocmd BufNewFile,BufRead README  set filetype=markdown
262 augroup END
264 "==============================================================================
265 " Auto Completor
266 "==============================================================================
267 let g:completor_auto_trigger = 1
268 let g:completor_clang_binary = '/usr/bin/clang'
269 let g:completor_complete_options = 'menuone,noselect,preview'
271 inoremap <expr> <Tab>   pumvisible() ? "\<C-n>" : "\<Tab>"
272 inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"
274 "==============================================================================
275 " Remove characters function
276 "==============================================================================
277 function! Clean_file()
278         " Save cursor line
279         let save_pos = getpos(".")
280         " Remove spaces from begin & end of line
281         " Remove spaces after tab
282         " Remove tab\n
283         " Remove file last line if empty
284         " Remove more than 2 empty lines
285         " Remove empty comment lines
286         " TODO remove 2 spaces between words
287         %s/ \+$//e |
288         \%s/^ \+//e |
289         \%s/^\/\/$//e |
290         \%s/\t \+/\t/e |
291         \%s/\t\+\n/\r/e |
292         \%s/\($\n\)\+\%$//e |
293         \%s/^\n\{1,\}/\r/e
294         " Return to saved cursor
295         call setpos(".", save_pos)
296         normal !k
297 endfunction
299 "==============================================================================
300 " File Templates
301 "==============================================================================
302 let g:filename      = expand("%:r")
303 let g:filename_ext  = expand("%:e")
304 let g:upperfilename = toupper(filename)
306 autocmd BufNewFile *.sh normal!ggi#!/bin/sh\r
307 autocmd BufNewFile *.py normal!ggi#!/usr/bin/env python3\r
308 autocmd BufNewFile *.h  exe "normal!ggi/* See LICENSE file for copyright and "
309         \"license details. */\r\r#ifndef ".g:upperfilename."_H\r#define "
310         \.g:upperfilename."_H\r\r\r\r\#endif /* ".g:upperfilename."_H */" | normal!kk
311 autocmd BufNewFile *.c  exe "normal!ggi/* See LICENSE file for copyright and license"
312         \"details. */\r\r#include <stdio.h>\r\r/* macros */\r/* typedef */\r/*"
313         \"function declarations */\r/*"
314         \"global variables */\r/* function implementations */\r\rint\rmain(int argc,"
315         \" char *argv[])\r{\r\rreturn 0;\r}" | normal!kk