summaryrefslogtreecommitdiff
path: root/.vimrc
blob: a79facb29db7994acd04c206db67857e10287cc6 (plain) (blame)
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
" Plugin Manager {{{
	call plug#begin('~/.vim/plugged')
	
	""" Customization
	Plug 'morhetz/gruvbox', {'rtp': 'vim'} " Color theme
	Plug 'itchyny/lightline.vim'           " Statusline and tabline for Vim
	Plug 'ryanoasis/vim-devicons'          " Icons on stuff like NERDTree
	
	""" Quality of life
	Plug 'tomtom/tcomment_vim'    " Toggle comments (gc, gcc)
	Plug 'Raimondi/delimitMate'   " Autocomplete brackets and quotes
	Plug 'preservim/nerdtree'     " Browse directories (:NERDTree)
	Plug 'mbbill/undotree'        " Easily interact with undo history
	Plug 'mg979/vim-visual-multi' " Multiple line cursor (and other stuff)
	Plug 'godlygeek/tabular'      " Line up text by a given character (:Tabularize /CHAR)
	Plug 'tpope/vim-eunuch'       " Easy UNIX shell commands
	Plug 'alvan/vim-closetag'     " Automatically add HTML closing tags
	Plug 'kien/tabman.vim'        " Show open buffers
	Plug 'ervandew/supertab'      " Makes <Tab> be nicer to use with Omnicompletion
	
	""" Software development
	Plug 'dense-analysis/ale'                   " Linting with LSP
	Plug 'honza/vim-snippets'                   " Ready snippets for some languages
	Plug 'editorconfig/editorconfig-vim'        " Support for EditorConfig
	Plug 'zivyangll/git-blame.vim'              " Show who last edited a line
	Plug 'wakatime/vim-wakatime'                " Time tracking via wakatime.com
	
	""" Language integration
	Plug 'dNitro/vim-pug-complete', { 'for': ['jade', 'pug'] } " Pug: Omnicompletion integration
	Plug 'digitaltoad/vim-pug'                                 " Pug: Filetype detection, identation, syntax highlighting
	Plug 'bfrg/vim-cpp-modern'                                 " C/C++: Better syntax highlighting
	Plug 'itchyny/vim-haskell-indent'                          " haskell: Adds identation
	Plug 'vim-scripts/syntaxhaskell.vim'                       " haskell: Better syntax highlighting
	
	call plug#end()
" }}}

" General {{{
	" Color scheme {{{
		colorscheme gruvbox
		let g:gruvbox_contrast_dark = 'hard'
		set background=dark " Setting dark mode
	" }}}

	" Character visuals {{{
		set list " Enabled customization of white space characters, so spaces, tabs, EOL, etc. (:h 'list')
		
		" Show tabs as a | with three spaces
		" DO NOT remove the trailing space in the next line!
		set listchars=tab:│\ ,extends:>,precedes:<

		autocmd BufReadPre,FileReadPre * call NoWrapOnLong()
		function! NoWrapOnLong()
			let maxline = system("wc -L " . bufname("%") . " | awk '{print $1}'")
			if maxline > 840
				set nowrap
			endif
		endfunction
	" }}}

	" Backspace and cursor {{{
		set cul                                     " Highlight current cursor line
		autocmd InsertEnter,InsertLeave * set cul!  " Don't highlight current line, when in insert mode
		set backspace=indent,eol,start              " Better backspace functionality
		
		set number      " Show line numbers to the left
		set scrolloff=0 " Don't keep any screen lines above or below the cursor
		set mouse=a     " Mouse support

		set showmatch " Cursor jumps to matching opening bracket automatically
		
		" Thin cursor in insert mode in Gnome-Terminal (version >=3.16 ; also works with Terminator)
		if has("autocmd")
		  au VimEnter,InsertLeave * silent execute '!echo -ne "\e[1 q"' | redraw!
		  au InsertEnter,InsertChange *
		    \ if v:insertmode == 'i' | 
		    \   silent execute '!echo -ne "\e[5 q"' | redraw! |
		    \ elseif v:insertmode == 'r' |
		    \   silent execute '!echo -ne "\e[3 q"' | redraw! |
		    \ endif
		  au VimLeave * silent execute '!echo -ne "\e[ q"' | redraw!
		endif
	" }}}

	" Indentation {{{
		set tabstop=4    " Show tabs as 4 wide
		set shiftwidth=4 " Indent with 4 spaces
		
		autocmd BufRead,BufNewFile *.component.css set filetype=css
		autocmd FileType css,ts setlocal ts=2 sw=2 sts=0 expandtab " Transform tabs in CSS and TS into 2 spaces
		autocmd FileType lisp,scheme,haskell setlocal expandtab
	" }}}

	" Folding {{{
		filetype plugin indent on
		syntax on                                " Syntax highlighting
		autocmd FileType * set foldmethod=syntax " Syntax folding for all
		autocmd FileType vim,text,sh,zsh,xdefaults setlocal foldmethod=marker " Overrides folding to marker for types

		set foldlevel=99 " Open all folds by default
		autocmd FileType vim,sh,zsh,xdefaults setlocal foldlevel=0 " Close all folds by defualt on filetypes

		set foldtext=MyFoldText() " Custom text for a fold
		function MyFoldText()
			let line      = substitute(getline(v:foldstart), "\t", repeat(" ", shiftwidth(0)), "") "  Gets the first fold line and replace tabs with spaces (as many as shiftwidth is set to)
			let linecount = v:foldend - v:foldstart                                                "  Calculates amount of folded lines
			return line . repeat(" ", winwidth('%') - strlen(line) - 10 - strlen(linecount))   
						\ . "  "  . linecount .  "  "                                            "  Shows our line, then a lot of spaces, and at the very end we have line number and arrows
		endfunction
	" }}}

	" Mappings {{{
		" Language mapping for (bulgarian) cyrillic characters to english
		set langmap=АA,аa,БB,бb,ВW,вw,ГG,гg,ДD,дd,ЕE,еe,ЖV,жv,ЗZ,зz,ИI,иi,ЙJ,йj,КK,кk,ЛL,лl,МM,мm,НN,нn,ОO,оo,ПP,пp,РR,рr,СS,сs,ТT,тt,УU,уu,ФF,фf,ХH,хh,ЦC,цc,Ч~,ч`,Ш{,ш[,Щ},щ],ЪY,ъy,ЬX,ьx,Ю\|,ю\\,ЯQ,яq

		" Don't get into insert mode after adding a line with o/O
		nnoremap o o<Esc>
		nnoremap O O<Esc>
		
		" Ctrl-Backspace deletes the previous word in insert mode.
		inoremap <C-h>  <C-w>
		inoremap <C-BS> <C-w>
		
		" Ctrl+s; Ctrl+c, Ctrl+v, Ctrl+x; Ctrl+z, Ctrl+r; Ctrl+a bindings that work on GUI applications
		" Partly taken from: https://gist.github.com/jshih/3423345
		imap <C-s> <ESC>:w<CR>a
		nmap <C-S> <ESC>:w<CR>
		"imap <C-S-z> <C-r> An impossible dream
		vmap <C-c> "+yi<Esc>
		vmap <C-v> c<ESC>"+p<Esc>
		imap <C-v> <ESC>"+pa
		vmap <C-x> "+c<Esc>
		imap <C-z> <ESC>ui
		imap <C-r> <ESC><C-r>i
		map  <C-a> ggVG
		
		" Show all buffers and be able to just type in the number of the buffer
		map <C-f> :set nomore<Bar>:ls<Bar>:set more<CR>:b<Space>
		
		
		" Move a line up and down with Alt+k and Alt-j
		execute "set <M-j>=\ej"
		nnoremap <M-j> <ESC>:m+1<CR>
		execute "set <M-k>=\ek"
		nnoremap <M-k> <ESC>:m-2<CR>
		
		
		" Make session file
		nnoremap <F1> <ESC>:mksession! .vim-session<CR>
		" Show who edited the current line from git history
		nnoremap <F8> :<C-u>call gitblame#echo()<CR>
		" Open lazygit in a new tab with =
		nnoremap = :tab ter ++close lazygit<CR>
	" }}}

	" Command abbreviations {{{
		" Cyrillic (Bulgarian yawerty layout) support
		ca в w
		ca ва wa
		ca ь x
		ca ьа xa
		
		" Make x and xa just save and quit without saving. This allows for closing all tabs and terminals.
		ca x  w <bar> q!
		ca xa wa <bar> qa!
		
		" Open a terminal vim tab with tt and open a blank tab with te
		ca tt tab ter
		ca te tabe
	" }}}

	" Omni completion {{{
		" Omni completion supports C, HTML, CSS, JavaScript, PHP, Python, Ruby, SQL, XML
		set omnifunc=syntaxcomplete#Complete " Completion for all supported languages
		
		" Do omni completion from Ctrl+Space
		inoremap <C-@> <C-X><C-O>
		
		" Thanks to: https://vim.fandom.com/wiki/Make_Vim_completion_popup_menu_work_just_like_in_an_IDE
		" set completeopt=longest,menuone
		" [ The following are disabled, because they conflict with delimitMate's autobracket feature ]
		" inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"
		" inoremap <expr> <C-n> pumvisible() ? '<C-n>' : '<C-n><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'

		" "Disables" arrow navigation in completion menu
		inoremap <expr> <up> pumvisible() ? '<c-y><up>' : '<up>'
		inoremap <expr> <down> pumvisible() ? '<c-y><down>' : '<down>'

		" Pressing tab goes from top to bottom
		let g:SuperTabContextDefaultCompletionType = "<c-n>"
	" }}}

	" Other {{{
		autocmd BufRead,BufNewFile * set tw=0 " Sets textwidth to 0 for all files (set with autocmd since just doing "set tw=0" can be overridden)
		set signcolumn=number                 " Show signs and numbers on the same column
	" }}}
" }}}

" Custom functionality {{{
	" Spell check mode {{{
		" When you press F6 it will toggle a "spell check mode", 
		" spell is activated and the colour scheme is changed
		nmap <F6> :call SpellCheckModeToggle()<CR>
		
		function! SpellCheckModeToggle()
			if g:colors_name == 'gruvbox'
				set spell
				colorscheme darkblue
			else
				set nospell
				colorscheme gruvbox
			endif
		endfunction
		
		" Scrolling (shows history) in terminal (except in lazygit)
		" Scroll up to activate it, and press a to deactivate it
		" Slightly modified version of: https://github.com/vim/vim/issues/2490#issuecomment-393973253
		tmap <silent> <ScrollWheelUp> <c-w>:call EnterNormalMode()<CR>

		function! ExitNormalMode()
			unmap <buffer> <silent> <RightMouse>
			call feedkeys("a")
		endfunction
		
		function! EnterNormalMode()
			if @% == '!lazygit'
				tunmap <silent> <ScrollWheelUp>
			elseif &buftype == 'terminal' && mode('') == 't'
				call feedkeys("\<c-w>N")
				call feedkeys("\<c-y>")
				map <buffer> <silent> <RightMouse> :call ExitNormalMode()<CR>
			endif
		endfunction
	" }}}

	" Code terminal {{{
		" Program in which build actions are executed. If no value, build commands are executes as bash commands.
		" This is only really useful in languages with interpreters
		let g:codeenvs = {
		\	'scheme' : 'racket',
		\	'lisp'   : 'rlwrap sbcl --noinform',
		\ }

		" The following two dictionaries support certain substitutions:
		" %F - location of current file (relative path)
		" %D - directory of current file (full path)

		" Building the current file only
		let g:codebuildsingle = {
		\	'scheme'  : '(enter! "%F")',
		\	'cpp'     : "g++ -g -pedantic '%F' && ./a.out",
		\	'yacc'    : 'bison -t -d %F',
		\	'lex'     : 'flex %F',
		\	'haskell' : 'runhaskell "%F"',
		\	'lisp'    : '(load "%F")',
		\ }
		" Building all files in the directory (and subdirectories) of the current file
		let g:codebuildproject = {
		\	'cpp' : "g++ -g -pedantic '%D/'**/*.cpp && ./a.out"
		\ }

		noremap <F3> :call CodeTerminal(g:codebuildsingle)<CR>
		noremap <F4> :call CodeTerminal(g:codebuildproject)<CR>
		inoremap <F3> <C-O>:call CodeTerminal(g:codebuildsingle)<CR>
		inoremap <F4> <C-O>:call CodeTerminal(g:codebuildproject)<CR>
		" We assume that the last accessed window is the one with the source code, otherwise it gets complicated
		tnoremap <F3> <C-W><C-P>:call CodeTerminal(g:codebuildsingle)<CR><C-W><C-P>
		tnoremap <F4> <C-W><C-P>:call CodeTerminal(g:codebuildproject)<CR><C-W><C-P>


		let t:codetermbufnr = -1
		let t:codetermft    = ""
		let t:codetermhadenv = 0
		function! OpenCodeTerminal()
			if !bufexists(t:codetermbufnr)
				term
				" Latest buffer is the terminal buffer we just opened
				let t:codetermbufnr = bufnr("$")
				" We go back to the file from which this was called
				wincmd p
				let t:codetermft = ""
				let t:codetermhadenv = 0
			endif

			if &filetype != t:codetermft
				if t:codetermhadenv
					" This is kinda bad, since certain environments might not close with this only
					call term_sendkeys(t:codetermbufnr, "\<C-D>")
					" The sleep is kinda bad, but it fixes the next term_sendkeys not working properly
					sleep 500m
				endif

				let t:codetermft = &filetype
				if has_key(g:codeenvs, &filetype)
					call term_sendkeys(t:codetermbufnr, g:codeenvs[&filetype] . "\<CR>")
					let t:codetermhadenv = 1
				else
					let t:codetermhadenv = 0
				endif
			endif
		endfunction

		function! CodeTerminal(builddict)
			call OpenCodeTerminal()

			if has_key(a:builddict, &filetype)
				let buildcomm = a:builddict[&filetype] . "\<CR>"
				let buildcomm = substitute(buildcomm, "%F", @%, "")
				let buildcomm = substitute(buildcomm, "%D", expand('%:p:h'), "")
				call term_sendkeys(t:codetermbufnr, buildcomm)
				echo "[CodeTerminal] Sent build command!"
			else
				echo "[CodeTerminal] No value in build dictionary!"
			endif
		endfunction
	" }}}
" }}}

" Plugins {{{
	" DelimitMate {{{
		let delimitMate_expand_cr = 1
		" Don't autocomplete diamond brackets in HTML (compatibility with closetag plugin)
		autocmd FileType html let b:delimitMate_matchpairs='(:),[:],{:}'
	" }}}

	" NERDTree {{{
		" Toggle NERDTree with Tab
		nmap <Tab> :NERDTreeToggle<CR>

		let NERDTreeCustomOpenArgs = {'file':{'where':'t'}} " Open file in new tab, doesn't work only for double-click
		let NERDTreeShowHidden     = 1
		let NERDTreeWinPos         = "right"
		let NERDTreeIgnore         = ['\.swp$', '\~$'] " Ignore file, ending with .swp and ~
	" }}}

	" Undotree {{{
		" Toggle undotree with F5
		nmap <F5> :UndotreeToggle<CR>  
		
		let g:undotree_WindowLayout       = 2
		let g:undotree_ShortIndicators    = 1 " e.g. using 'd' instead of 'days' to save some space.
		let g:undotree_SetFocusWhenToggle = 1 " if set, let undotree window get focus after being opened, otherwise focus will stay in current window.
		let g:undotree_TreeNodeShape      = '*'
		let g:undotree_DiffCommand        = "diff"
	" }}}

	" Tabman {{{
		let g:tabman_toggle = '<S-Tab>'
		let g:tabman_side   = 'right'
	" }}}

	" SuperTab {{{
		let g:SuperTabDefaultCompletionType = "context"
	" }}}

	" Lightline {{{
		" Settings {{{
			set laststatus=2

			if !has('gui_running')
				set t_Co=256
			endif

			let g:lightline = { 
				\ 'enable': { 
				\   'statusline': 1, 
				\   'tabline': 1 
				\ },
				\ 'colorscheme': 'wombat',
				\ 'active': {
				\   'left': [ [ 'mode', 'paste' ],
				\             [ 'gitbranch', 'filename', 'charvaluehex', 'readonly', 'modified' ] ],
				\   'right': [ [ 'lineinfo' ],
				\              [ 'fileencoding', 'filetype'] ]
				\ },
				\ 'component': {
				\   'charvaluehex': '0x%B',
				\ },
				\ 'component_function': {
				\   'gitbranch': 'LightlineFugitive',
				\   'readonly': 'LightlineReadonly',
				\   'modified': 'LightlineModified',
				\   'filetype': 'LightlineFileType'
				\ },
				\ 'tabline': {
				\   'right': [[]]
				\ },
				\ 'tab': {
				\   'active': [ 'iconFilename','iconReadonly', 'iconModified' ],
				\   'inactive': [ 'iconFilename', 'iconReadonly', 'iconModified' ]
				\ },
				\ 'tab_component_function': {
				\   'iconFilename': 'LightlineTabFilename',
				\   'iconReadonly': 'LightlineTabReadonly',
				\   'iconModified': 'LightlineTabModified'
				\ },
				\ 'separator': { 'left': '', 'right': '' },
				\ 'subseparator': { 'left': '', 'right': '' },
				\ 'tabline_separator': { 'left': '', 'right': '' },
				\ 'tabline_subseparator': { 'left': '' }
			\ }
		" }}}

		" Function overrides {{{
			" Most of the following functions are just modification of source: 
			" Examples from doc file - https://github.com/itchyny/lightline.vim/blob/53176a0b75d6389d775d7bce0d494e58fc654f38/doc/lightline.txt#L583
			" Tab source code - https://github.com/itchyny/lightline.vim/blob/master/autoload/lightline/tab.vim

			function! LightlineFugitive()
				if exists('*FugitiveHead')
					let branch = FugitiveHead()
					return branch !=# '' ? ' '.branch : ''
				endif
					return ''
			endfunction

			function! LightlineReadonly()
				return &ft !~? 'help\|vimfiler' && &readonly ? '' : ''
			endfunction

			function! LightlineModified()
				return &ft =~ 'help\|vimfiler' ? '' : &modified ? '' : &modifiable ? '' : ''
			endfunction

			function! LightlineFileType()
				return strlen(&filetype) ? WebDevIconsGetFileTypeSymbol() . ' ' . &filetype : ''
			endfunction

			" Adds the file type icon in tabs and limit their width
			function! LightlineTabFilename(n) abort
				let buflist = tabpagebuflist(a:n)
				let winnr   = tabpagewinnr(a:n)
				let _       = (expand('#'.buflist[winnr - 1].':t') !=# '' ? expand('#'.buflist[winnr - 1].':t') : '[No Name]')

				" Limit the width of tabs, so they don't go out of the screen
				let tabNameLengthMax = &columns/((tabpagenr('$') > 0 ? tabpagenr('$') : 0) + 5)

				return WebDevIconsGetFileTypeSymbol(_) . ' ' . _[0:tabNameLengthMax]
			endfunction

			function! LightlineTabReadonly(n) abort
				let winnr = tabpagewinnr(a:n)
				return gettabwinvar(a:n, winnr, '&readonly') ? '' : ''
			endfunction

			function! LightlineTabModified(n) abort
				let winnr = tabpagewinnr(a:n)
				return gettabwinvar(a:n, winnr, '&modified') ? '' : gettabwinvar(a:n, winnr, '&modifiable') ? '' : ''
			endfunction
		" }}}
	" }}}

	" ALE {{{
		map <F2> <ESC>:ALERename<CR>
		map gd <ESC>:ALEGoToDefinition<CR>
		map gi <ESC>:ALEHover<CR>
		map ge <ESC>:ALEDetail<CR>

		set omnifunc=ale#completion#OmniFunc
		let g:ale_completion_enabled = 1

		let g:ale_floating_preview = 1 " Use floating window
		let g:ale_floating_window_border = ['│', '─', '╭', '╮', '╯', '╰'] " Nicer borders
		
		let g:ale_typescript_tsserver_use_global = 1 " Use global tsserver package
	" }}}
" }}}