-
Notifications
You must be signed in to change notification settings - Fork 1
/
.vimrc
410 lines (333 loc) · 10.4 KB
/
.vimrc
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
" IMPORTANT: Make sure to install the rust tools 'bat', 'ripgrep'
" 'rust_analyzer', 'rust-src', 'xclip'
" :PlugInstall on your first execution
"
"""""""""""""""""
""""" BASIC SETUP
"""""""""""""""""
" Do not start in compatible vi mode
set nocompatible
" Create backup, swap and undo directories if they do not exist
" Backup files are copies of the last saved version of a file
" Swap files contain the current modification of a file
" Undo files let you use the undo command after closing and reopening vim
silent !mkdir -p /tmp/vim/backup
silent !mkdir -p /tmp/vim/swap
silent !mkdir -p /tmp/vim/undo
silent !mkdir -p /tmp/vim/preview
" Create a .vim folder in home dir
silent !mkdir -p ~/.vim
set backupdir=/tmp/vim/backup
set dir=/tmp/vim/swap
set undodir=/tmp/vim/undo
" Create undo files
set undofile
" Add syntax highlighting an file type identification, plugin and indenting
syntax on
filetype plugin indent on
" Add a permanent sign column so that columns does not disappear and reappear
" during diagnostic
set signcolumn=yes
" No error bells
set noerrorbells
" Set length of tab
set tabstop=4 softtabstop=4
" Set number of spaces for each step of indent
set shiftwidth=4
" Use spaces instead of tab character
set expandtab
" Use smart indent
set smartindent
" Use line numbering
set nu
" Also use relative line numbering
set relativenumber
" Do not use word wrap
set nowrap
" Use incremental search, i.e. highlight search results while typing
set incsearch
" If we open a project folder with 'vim .' and there is a .vimrc present, then
" use this as a config file. This allows project specific config files
set exrc
" When finishing search mode we do not want to keep search results highlighted
set nohlsearch
" Start scrolling x lines before the current line reaches the end of the screen
set scrolloff=5
" Keep buffers of multiple files in memory so that you can open a new file without saving the current one
set hidden
" We do not want vimdiff to open in read only mode
set noro
" Set text width to 80 characters
set textwidth=80
" Load local project .nvimrc files
set exrc
"""""""""""""""
" BASIC MAPPING
"""""""""""""""
" Disable weird modes like command history and ex mode
nnoremap q: <nop>
nnoremap Q <nop>
" Escape every mode more simple by mapping Ctrl + J to Escape
nnoremap <C-j> <Esc>
inoremap <C-j> <Esc>
vnoremap <C-j> <Esc>
snoremap <C-j> <Esc>
xnoremap <C-j> <Esc>
cnoremap <C-j> <C-c>
onoremap <C-j> <Esc>
lnoremap <C-j> <Esc>
tnoremap <C-j> <Esc>
" Set space to leader
let mapleader = " "
" We do not want to use arrow keys for navigation
nnoremap <up> <nop>
nnoremap <down> <nop>
nnoremap <left> <nop>
nnoremap <right> <nop>
inoremap <up> <nop>
inoremap <down> <nop>
inoremap <left> <nop>
inoremap <right> <nop>
" We want to go back and forth in buffers easily and simplify closing them
nnoremap <leader>h :bp<CR>
nnoremap <leader>l :bn<CR>
nnoremap <leader>c :bd<CR>
" Toggle between current and recent buffer
nnoremap <leader><leader> <C-^>
" Show current open buffers
nnoremap <leader>, :ls<CR>
" Suspend vim with shortcut (move process to background and then type 'fg' to return to vim)
nnoremap <C-s> :sus<CR>
vnoremap <C-s> :sus<CR>
inoremap <C-s> :sus<CR>
" Quick save
nnoremap <C-w> :w<CR>
inoremap <C-w> <C-o>:w<CR>
" Quick window mode
nnoremap <leader>w <C-w>
" Create new tab and close tab
nnoremap <leader>tn :tabnew<CR>
nnoremap <leader>tc :tabclose<CR>
" Quickly navigate to an open tab with leader key and number
nnoremap <A-1> 1gt
nnoremap <A-2> 2gt
nnoremap <A-3> 3gt
nnoremap <A-4> 4gt
inoremap <A-1> <C-o>1gt
inoremap <A-2> <C-o>2gt
inoremap <A-3> <C-o>3gt
inoremap <A-4> <C-o>4gt
" Movement of characters in line
nnoremap <S-l> xp
nnoremap <S-h> xhhp
" Sometimes, we do not want to fill registers automatically, i.e. we want to
" delete something `d` without putting it in a register so that it
" does not show up when we press `p` afterwards
nnoremap <leader>d "_d
vnoremap <leader>d "_d
" Do not yank selected lines when using put in visual mode
vnoremap p "_dP
" We want to be able to copy from vim to system clipboard
nnoremap <leader>y "+y
vnoremap <leader>y "+y
" Use <Tab> and <S-Tab> to navigate through popup menu
inoremap <expr> <Tab> pumvisible() ? "\<C-n>" : "\<Tab>"
inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"
" Some improvements for brackets and so on
inoremap ( ()<left>
inoremap [ []<left>
inoremap { {}<left>
inoremap < <><left>
inoremap <expr> " strpart(getline('.'), col('.')-1, 1) == "\"" ? "\<Right>" : "\"\"\<Left>"
inoremap <expr> ` strpart(getline('.'), col('.')-1, 1) == "\`" ? "\<Right>" : "\`\`\<Left>"
inoremap <expr> ) strpart(getline('.'), col('.')-1, 1) == ")" ? "\<Right>" : ")"
inoremap <expr> ] strpart(getline('.'), col('.')-1, 1) == "]" ? "\<Right>" : "]"
inoremap <expr> } strpart(getline('.'), col('.')-1, 1) == "}" ? "\<Right>" : "}"
inoremap <expr> > strpart(getline('.'), col('.')-1, 1) == ">" ? "\<Right>" : ">"
" This is our improved return to make correct indentation in '{' and '}'
" brackets
fun! SuperCR()
if strpart(getline('.'), col('.') - 2, 2) == '{}'
return "\<CR>\<ESC>\O"
endif
return "\<CR>"
endfun
autocmd FileType * inoremap <CR> <C-R>=SuperCR()<CR>
" Use spellchecking and add toggle button
set spelllang=en
nnoremap <silent> <F12> :set spell!<cr>
inoremap <silent> <F12> <C-O>:set spell!<cr>
" Activate spell checking by default
set spell
" We want to compile markdown files to pdf when we save them
" We also add a :preview command and map it to F9
autocmd BufWritePost *.md :silent !pandoc <afile>:p -V colorlinks=true -V linkcolor=blue -V urlcolor=blue -V toccolor=gray -o /tmp/vim/preview/<afile>:t:r.pdf
command! Preview !xdg-open /tmp/vim/preview/%:t:r.pdf
map <F9> :Preview<CR><CR>
" Remap some mappings in diff mode
if &diff
" go to next diff
nnoremap <leader>n ]c
" go to previous diff
nnoremap <leader>p [c
" this is for merging
nnoremap <leader>1 :diffget LOCAL<CR>
nnoremap <leader>2 :diffget BASE<CR>
nnoremap <leader>3 :diffget REMOTE<CR>
endif
""""""""""""""""""
""""""""" EXPLORER
""""""""""""""""""
"""" Neovim Explorer
if has('nvim')
nnoremap <leader>e :NvimTreeFindFileToggle<CR>
else
"""" NETRW setting
" Disable the banner
let g:netrw_banner=0
" Make tee view default
let g:netrw_liststyle = 3
" When opening a new file, open it in a large vsplit on the right
let g:netrw_altv = 1
" Set netrw window size
let g:netrw_winsize = 20
" Show preview window in vertical split
let g:netrw_preview = 1
" Create hotkey for toggling explorer window
map <leader>e :20Lex<CR>
endif
"""""""""""""""
""""""" PLUGINS
"""""""""""""""
" We use 'vim-plug' as a plugin manager. The following code automates the installation
let data_dir = has('nvim') ? stdpath('data') . '/site' : '~/.vim'
if empty(glob(data_dir . '/autoload/plug.vim'))
silent execute '!curl -fLo '.data_dir.'/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
autocmd VimEnter * PlugInstall --sync | source ~/.vimrc
endif
" Now we add the plugins we want to use
call plug#begin('~/.vim/plugged')
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'
Plug 'itchyny/lightline.vim'
Plug 'kaarmu/typst.vim'
if has('nvim')
Plug 'neovim/nvim-lspconfig'
Plug 'hrsh7th/cmp-nvim-lsp'
Plug 'hrsh7th/cmp-buffer'
Plug 'hrsh7th/cmp-path'
Plug 'hrsh7th/nvim-cmp'
Plug 'hrsh7th/cmp-vsnip'
Plug 'hrsh7th/vim-vsnip'
Plug 'kyazdani42/nvim-web-devicons'
Plug 'kyazdani42/nvim-tree.lua'
Plug 'nvim-lua/plenary.nvim'
Plug 'sindrets/diffview.nvim'
source ~/.vim/rust/rust-plugins.vim
endif
call plug#end()
" When we lookup files in a directory we want to respect the .gitignore file,
" but if we are not in a git repository we want to list all files
function! GFilesFallback()
let output = system('git rev-parse --show-toplevel') " Is there a faster way?
let prefix = get(g:, 'fzf_command_prefix', '')
if v:shell_error == 0
exec "normal :" . prefix . "GFiles --cached --others --exclude-standard\<CR>"
else
exec "normal :" . prefix . "Files\<CR>"
endif
return 0
endfunction
" Use shortcut for fuzzy finding files by name
nnoremap <silent> <leader>o :call GFilesFallback()<CR>
" Use shorcut for fuzzy searching for expression in all subdirectories
nnoremap <silent> <leader>f :Rg<CR>
"""""""""""""""
""""""" COLORS
"""""""""""""""
" Enable better color support"
if (has("termguicolors"))
set termguicolors
endif
" Set colorscheme
colorscheme molokai
" Use 256-color support for molokai scheme
let g:rehash256 = 1
" Make background transparent
hi Normal guibg=NONE ctermbg=NONE
hi TablineSel guibg=RED ctermbg=RED
" Configure lightline
let g:lightline = {
\ 'colorscheme': 'molokai',
\ 'active': {
\ 'left': [ [ 'mode', 'paste' ], [ 'readonly', 'absolutepath', 'modified' ] ],
\ }
\ }
" We see the mode in lightline, so we deactivate it in vim
set noshowmode
""""""""""""""""""""
""""""" NEOVIM ONLY
""""""""""""""""""""
if has('nvim')
" Github Copilot
inoremap <silent><script><expr> <C-A> copilot#Accept("")
let g:copilot_no_tab_map = v:true
lua << EOF
-- Setup treeview
require('nvim-tree').setup({
disable_netrw = true,
hijack_cursor = true,
sort_by = function(nodes)
table.sort(nodes, function(a, b)
if a.type ~= b.type then
return a.type == "file"
else
return a.name < b.name
end
end)
end
})
-- Setup language server for some other languages
-- Python
require'lspconfig'.pyright.setup{}
-- Typescript
require'lspconfig'.tsserver.setup{}
EOF
" Setup autoformat for some other languages
" Python and Typescript
autocmd BufWritePre *.py lua vim.lsp.buf.format(nil, 1000)
autocmd BufWritePre *.ts lua vim.lsp.buf.format(nil, 1000)
endif
" Rust setup
" Source from file
source ~/.vim/rust/rust.vim
if has('nvim')
lua << EOF
-- Setup Completion
-- See https://github.com/hrsh7th/nvim-cmp#recommended-configuration
local cmp = require("cmp")
cmp.setup({
snippet = {
expand = function(args)
vim.fn["vsnip#anonymous"](args.body)
end,
},
mapping = {
["<S-Tab>"] = cmp.mapping.select_prev_item(),
["<Tab>"] = cmp.mapping.select_next_item(),
["<C-d>"] = cmp.mapping.scroll_docs(-4),
["<C-f>"] = cmp.mapping.scroll_docs(4),
["<C-Space>"] = cmp.mapping.complete(),
["<C-e>"] = cmp.mapping.close(),
["<CR>"] = cmp.mapping.confirm({ select = false }),
},
-- Installed sources
sources = {
{ name = "nvim_lsp" },
{ name = "vsnip" },
{ name = "path" },
{ name = "buffer" },
},
})
EOF
endif