summaryrefslogtreecommitdiffhomepage
path: root/vimrc
blob: ef4aca7138675bd5e1156d58954663245c8cc339 (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
" When creating a custom .vimrc we need to re-enable the defaults
unlet! skip_defaults_vim
source $VIMRUNTIME/defaults.vim

" Set utf-8 encoding early to prevent issues later on
set encoding=utf-8
set fileencoding=utf-8

" ----- CUSTOM CONFIGURATION DIRECTORY ----- "
"  If you don't care about changing the vim config directory then you can
"  safely ignore this section

" If you wish to use .config/vim directory instead make sure to add the
" following to your .bashrc:
" 	export VIMINIT="source ~/.config/vim/vimrc"
" and place this vimrc into `~/.config/vim/vimrc`.
" Otherwise Vim will configure itself automatically using default directories
if "source ~/.config/vim/vimrc" == $VIMINIT
	" Where to check for vimplug later on
	let g:vimplug_dir = "~/.config/vim/autoload/plug.vim"
	" Used for moving .vim into .config/vim
	set viminfo+=n~/.config/vim/viminfo
	" Create new runtime paths to .config/vim and enforce order
	let rtp=&runtimepath
	set runtimepath=~/.config/vim
	let &runtimepath.=','.rtp.',~/.config/vim/after'
	" Move .viminfo files to .config/vim as well
	set viminfo+=n~/.config/vim/viminfo
	" Write view files to .config/vim
	set viewdir=~/.config/vim/view
else
	" Where to check for vimplug later on
	let g:vimplug_dir = "~/.vim/autoload/plug.vim"
endif

" ----- VANILLA CONFIG ----- "

" -- CUSTOM SHORTCUTS -- "

" Space is used as the 'modifier'/'leader' key
let mapleader = " "

" For switching/opening buffers:
nnoremap <leader>e :e **/*

" For switching buffers next/previous:
nnoremap <C-l> :bn<CR>
nnoremap <C-h> :bp<CR>

" For cycling windows or jump to window:
" 	removed because conflicted with vimwiki bindings
" nnoremap <s-cr> <c-w>w
" nnoremap <number><S-CR> <number><c-w>w

" Moving a line to a different spot
" 	Dont really use this and not sure what to map it to
" nnoremap <leader>k :m .-2<CR>==
" nnoremap <leader>j :m .+1<CR>==
" inoremap <leader>j <Esc>:m .+1<CR>==gi
" inoremap <leader>k <Esc>:m .-2<CR>==gi
" vnoremap <leader>j :m '>+1<CR>gv=gv
" vnoremap <leader>k :m '<-2<CR>gv=gv

" No longer need to press shift to press : in normal and visual mode
nnoremap ; :
vnoremap ; :

" `o` and `O` now only adds new line without going into insert mode
" 	TODO: broken in vimwiki
nnoremap o o<esc>
nnoremap O O<esc>

" -- CUSTOM COMMANDS -- "
" Toggle spell check
let g:spellcheck_is_enabled = 0
function! ToggleSpellCheck()
	if g:spellcheck_is_enabled
		let g:spellcheck_is_enabled = 0
		set nospell
		set statusline=Spell\ Check\ Disabled
	else
		let g:spellcheck_is_enabled = 1
		set spell spelllang=en_ca
		set statusline=Spell\ Check\ Enabled
	endif
endfunction
command ToggleSpellCheck call ToggleSpellCheck()
" Enable Spellcheck automatically for relevant files
function! EnableSpellCheck()
	let g:spellcheck_is_enabled = 1
	set spell spelllang=en_ca
endfunction
augroup enable_spellcheck_when_relevant
	autocmd!
	autocmd VimEnter,BufNewFile,BufRead *.md,*.mdown,*.tex,
				\*.txt,*.wiki call EnableSpellCheck()
augroup END

" Toggle Autoindenting Code on filesave
nnoremap g= :let b:PlugView=winsaveview()<CR>gg=G:call winrestview(b:PlugView) <CR>
function! ToggleAutoIndent()
	if !exists('#autoindent_on_save#BufWritePre')
		augroup autoindent_on_save
			autocmd!
			autocmd BufWritePre * :normal g=
			set statusline=AutoIndent\ Enabled
		augroup END
	else
		augroup autoindent_on_save
			autocmd!
			set statusline=AutoIndent\ Disabled
		augroup END
	endif
endfunction
command ToggleAutoIndent call ToggleAutoIndent()
call ToggleAutoIndent()
" Remove autoindentation if you open a regular text file
" TODO: doesn't work for some reason
function! DisableAutoIndent()
	augroup autoindent_on_save
		autocmd!
		set statusline=AutoIndent\ Disabled
	augroup END
endfunction
augroup remove_indent_text_files
	autocmd!
	autocmd VimEnter,BufNewFile,BufRead *.md,*.mdown, " *.tex,
				\*.txt,*.wiki call DisableAutoIndent()
augroup END

" -- OTHER -- "

" Show relative line numbers on sidebar
set nu
set relativenumber

" Always shows at least 2 lines around the cursor
set scrolloff=2

" Makes `set list` visually more clear to show invisible chars
set listchars=eol:¬,tab:>·,trail:~,extends:>,precedes:<,space:‗

" Vim clipboard and System clipboard are shared
" (Requires vim to be compiled to support this to work)
" (On Arch this means you need GVim installed)
set clipboard=unnamedplus

" Wrap to different line when reaching end or beginning of a line
set whichwrap=b,s,<,>,[,]

" Keep indent when line wraps
set breakindent
set showbreak=\ \ ·

" Don't split words when wrapping
set linebreak

" Faster Drawing
set ttyfast

" Enable folding of code according to the syntax of the language
set foldmethod=syntax
"	alternative: :set foldmethod=indent
" Remember folds when switching buffers
augroup remember_folds
	autocmd!
	autocmd BufWinLeave ?* mkview | filetype detect
	autocmd BufWinEnter ?* silent loadview | filetype detect
augroup END

function! MakeSession() 
	let cwd = getcwd()
	let filename = cwd . '/.vim'
	exe "mksession! " . filename
	exe "tabedit! " . filename
	exe "silent g:^cd :d"
	exe "silent g:^lcd :d"
	" exe "silent %s:\V" . cwd . "/::ge"
	" ^ Don’t work because getcwd() expand the ~ while mksession does not !
	exe "silent %s?\\v \\~=/.+/? ?g"
	" ^ backslash need to be protected
	exe "x"
endfunction

" Prefer to split below
set splitbelow


" ----- PLUGIN INITIALIZATION ----- "

" Install VimPlug if it isn't installed
" It will also install all the plugins in this case
if empty(glob(g:vimplug_dir))
	if "source ~/.config/vim/vimrc" == $VIMINIT
		silent !curl -fLo ~/.config/vim/autoload/plug.vim --create-dirs
					\ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
	else
		silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
					\ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
	endif
	autocmd VimEnter * PlugInstall --sync
endif

" Start of adding Vim plugins
if "source ~/.config/vim/vimrc" == $VIMINIT
	call plug#begin('~/.config/vim/plugged')
else
	call plug#begin('~/.vim/plugged')
endif
" Airline UI for top and bottom bars
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
" Execute Projects and Terminal commands asynchronously
Plug 'skywind3000/asyncrun.vim'
" Searches for custom .vimrc files for specific projects
Plug 'krisajenkins/vim-projectlocal'
" Code Completion
Plug 'neoclide/coc.nvim', {'branch': 'release'}
" Markdown Preview
Plug 'iamcco/markdown-preview.nvim', { 'do': { -> mkdp#util#install() }, 
			\'for': ['markdown', 'vim-plug']}
" Latex Compiling and Preview
Plug 'lervag/vimtex'
" Vimwiki for writing down notes
Plug 'vimwiki/vimwiki'
" Autogenerate Markdown Table of Contents
Plug 'mzlogin/vim-markdown-toc'
" Sophisticated Multi-Line Editing
Plug 'mg979/vim-visual-multi', {'branch': 'master'}

" Live Preview of HTML/CSS/JS
" 	outdated parser, but plugin in development
Plug 'turbio/bracey.vim'
call plug#end()
" End of adding Vim plugins

" ----- PLUGIN CONFIG ----- "
" The following is plugin configuration as well as
" vanilla vim settings tailored for the plugins installed

" Search for first custom .vimrc Vim can find to load
" It does this by recursively looking up directories until it finds one
let g:projectlocal_project_markers = ['.vimrc']
" When executing a project or terminal command
" automatically open a new Vim Window of size 7
let g:asyncrun_open = 7


" Sets Airline Theming
let g:airline_theme='distinguished'
let g:airline#extensions#tabline#enabled=1
" Commands to change dark and light themes
command LightTheme AirlineTheme silver
command DarkTheme AirlineTheme distinguished

" Makes popup text readable
hi Pmenu ctermbg=black ctermfg=white

" Vimwiki configs
" change symbols used for checkboxes
let g:vimwiki_listsyms = ' ~✓' "✗○●
" Makes code completion suggestions work with vimwiki files
augroup ft_vimwiki
	au!
	au BufRead,BufNewFile *.wiki set filetype=vimwiki
	au FileType vimwiki inoremap <silent> <buffer> <expr> <CR> complete_info()['selected'] > -1 ? "\<CR>" : "<Esc>:VimwikiReturn 1 5<CR>"
	au FileType vimwiki inoremap <silent> <buffer> <expr> <S-CR> complete_info()['selected'] > -1 ? "\<S-CR>" : "<Esc>:VimwikiReturn 2 2<CR>"
augroup END

" Coc configs
" Fix autocompletion adding a newline while in visual multi mode
augroup coc_autocomplete_newline
	au!
	autocmd User visual_multi_mappings  imap <buffer><expr> <CR> pumvisible() ? "\<C-Y>" : "\<Plug>(VM-I-Return)"
augroup END

" what program should vimtex use to show live edits
let g:vimtex_view_method = 'zathura'
" what flavour vimtex should use for autocomplete
let g:tex_flavor = 'latex'
" function for counting words in a latex file
function! WC()
	let filename = expand("%")
	let cmd = "detex " . filename . " | wc -w | perl -pe 'chomp; s/ +//;'"
	let result = system(cmd)
	echo result . " words"
endfunction
" the command for calling the function
command WordCount call WC()

" Make vimwiki use Markdown
let g:vimwiki_list = [{'path': '~/vimwiki/',
			\ 'syntax': 'markdown', 'ext': '.md'}]