-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvimrc
280 lines (232 loc) · 10.3 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
" ------------------------------ ------------------------------
" -------------------Map the leader key first-----------------
let mapleader = "\<space>" " the easiest button to hit
if !has('nvim')
runtime plugins.vim
endif
" 2. BASIC VIM CONFIGURATIONS {{{
" ###############################################################################
" ## ##
" ## 2. BASIC VIM CONFIGURATIONS ##
" ## ##
" ###############################################################################
" Absolute essentials!: Escape mapping, enable mouse {{{
" See https://github.com/vscode-neovim/vscode-neovim#composite-escape-keys
" for mapping jk to Esc in vscode-neovim
if !exists('g:vscode')
inoremap <silent> jk <Esc><Right>
endif
set mouse=a " enable mouse in all modes
if !has('nvim')
set ttymouse=xterm2 " allow mouse through tmux, ssh
endif
" let mapleader = "\<space>" " is set above in PLUGINS section
" }}}
" Appearance: Ruler, cursor and visualising chars {{{
set showcmd " display last command (default in nvim)
set termguicolors
" ----------------------------Ruler----------------------------
set number " enable numbers on the left ...
set relativenumber " ... but make current line 0
" ----------------------------Cursor---------------------------
set cursorline " highlight current line
if !exists('g:vscode')
set scrolloff=8 " keep cursor centered (8 lines from top and bottom edges)
set sidescrolloff=4 " keep cursor centered (4 lines from left and right edges)
endif
let &t_SI = "\e[5 q" " change to i-beam cursor when entering insert mode
let &t_EI = "\e[2 q" " change to block cursor when leaving insert mode
" -------------------Visualize hidden chars--------------------
set list
set listchars=tab:\|\ ,trail:·,nbsp:⎵ " tabs, non-breaking spaces, trailing spaces
if !has('g:vscode')
augroup visualise_chars
autocmd!
" only show eol when in insert mode
autocmd ModeChanged n:[vV] set listchars+=eol:↲
autocmd ModeChanged [vV]:n set listchars-=eol:↲
augroup end
endif
if !has('nvim') " make these chars display in gray instead of blue
highlight SpecialKey ctermfg=244 guifg=#808080
endif
" }}} Appearance: Ruler, cursor and visualising chars
" Editing: Search, splits and indentation {{{
" --------------------------Searching--------------------------
set wildmenu " enable command line completion (default in nvim)
set wildmode=longest:full,full " don't default to 1st option e.g. `:e` (like bash)
set wildignorecase " ignore case when searching
set ignorecase " searches are case insensitive
set smartcase " search case sensitive if contains an uppercase letter
set incsearch " incremental search (default in nvim)
set hlsearch " highlight text while searching (default in nvim)
if has('nvim')
set inccommand=nosplit " visual feedback while substituting
endif
" Allow quick clearing of highlighted search results
" Using <Esc><Esc> as a vim mapping is glitchy, see online for reasons
nnoremap <leader><Esc> :nohlsearch<CR>
autocmd! InsertEnter * call feedkeys("\<Cmd>noh\<cr>" , 'n') " close hlsearch when typing (idea: siuoly)
set wildmenu " enable command line completion (default in nvim)
set wildmode=longest:full,full " don't default to 1st option e.g. `:e` (like bash)
" ---------------------------Buffers---------------------------
set splitbelow " natural splits - :sp places new win below
set splitright " natural splits - :vsp places new win right
" -----------------------Tabs and spaces-----------------------
" set backspace=indent,eol,start " allow backspacing over everything in insert mode
set tabstop=4
set shiftwidth=4 " use 4 spaces width for indents
set expandtab " insert 4 spaces when tab pressed
" ---------------------------Others---------------------------
set pumheight=10 " limit popup menu height to 10
if has('nvim') " vim and nvim uses different undofiles
set undofile " enable persistent undo
set undodir=/tmp " undo temp file directory
endif
set foldmethod=marker
" }}} Editing: Search, splits and indentation
" }}} 2. BASIC VIM CONFIGURATIONS
" 3. KEYMAPPINGS {{{
" ###############################################################################
" ## ##
" ## 3. KEYMAPPINGS ##
" ## ##
" ###############################################################################
" Use this function to create keymaps with descriptions for neovim
" while remaining compatible with vim
function s:nnoremap(key, cmd, desc)
if has('nvim')
call luaeval("require('utils.keymap').map(_A[1], _A[2], { desc = _A[3] })", [a:key, a:cmd, a:desc])
else
execute 'nnoremap ' . a:key . ' ' . a:cmd
endif
endfunction
" Let the Alt key be mappable (only need fix for vim)
if !has('nvim')
execute "set <A-j>=\ej"
execute "set <A-k>=\ek"
endif
" Basic keymappings {{{
" context-based j and k - gj usually, but j if count is given
nnoremap <expr> j v:count == 0 ? 'gj' : 'j'
nnoremap <expr> k v:count == 0 ? 'gk' : 'k'
nnoremap gj j
nnoremap gk k
" make Y more consistent - yank till end (like D and C)
noremap Y y$
" Don't use Ex mode, use Q for macros (idea: jesseliete)
nnoremap Q @qj
xnoremap Q :norm @q<CR>
" Do not show stupid `how did we get here?` window
"nnoremap q: :echo 'No more Ex mode!'<CR>
" Ctrl + Backspace - delete previous word
" Use Ctrl+Shift+w because Ctrl+w does not work for Telescope
inoremap <C-BS> <C-S-w>
cnoremap <C-BS> <C-S-w>
" center searches and open folds
" this does not play nice with vscode-neovim, we add a fix in settings.json
nmap n nzzzv
nmap N Nzzzv
" Easy access to the clipboard register (idea: ThePrimeagen)
nmap <leader>y "+y
vmap <leader>y "+y
nmap <leader>Y "+Y
nmap <leader>d "+d
vmap <leader>d "+d
nmap <leader>D "+D
nmap <leader>p "+p
vmap <leader>p "+p
nmap <leader>P "+P
" :w looks simple at first but it's not
if !exists('g:vscode')
call s:nnoremap('<leader>w', ':w<CR>', 'Write to buffer')
else
" remapped in keymaps.lua if vscode exists
endif
" cd vim into the directory of the current buffer
call s:nnoremap('<leader>c', ':cd %:p:h<CR>', 'cd into buffer directory')
" Tabs to switch buffers (not a useful remap)
" if !exists('g:vscode')
" nnoremap <Tab> :bnext<CR>
" nnoremap <S-Tab> :bprevious<CR>
" else
" nnoremap <Tab> :tabnext<CR>
" nnoremap <S-Tab> :tabprevious<CR>
" endif
" }}} Basic keymappings
" [e]dit/[s]ource my [v]imrc {{{
nnoremap <leader>ve :vsplit $MYVIMRC<CR>
if !exists('g:vscode')
nnoremap <leader>vs :silent! call win_execute(win_findbuf(bufnr($MYVIMRC))[0], 'w') <bar> source $MYVIMRC<CR>
else
nnoremap <leader>vs :source $MYVIMRC<CR>
endif
" }}}
" [s]ubstitutions {{{
if !exists('g:vscode')
nnoremap <leader>s :%s//g<Left><Left>
else
" remapped in keymaps.lua if vscode exists
endif
vnoremap <leader>s "zy:%s/<C-r>z//g<Left><Left>
" }}}
" Splits controls {{{
" Switching between splits (ctrl + h,j,k,l)
" We choose ctrl over alt because vim-tmux-navigator defaults to ctrl+hjkl
" Use map over noremap to trigger vscode-neovim's default maps (if vscode)
map <C-h> <C-w>h
map <C-j> <C-w>j
map <C-k> <C-w>k
map <C-l> <C-w>l
tnoremap <C-h> <C-\><C-n><c-w>h
tnoremap <C-j> <C-\><C-n><c-w>j
tnoremap <C-k> <C-\><C-n><c-w>k
tnoremap <C-l> <C-\><C-n><c-w>l
" Resizing splits (ctrl + arrows keys)
map <C-Left> <C-w><
map <C-Right> <C-w>>
map <C-Up> <C-w>+
map <C-Down> <C-w>-
" }}}
" Moving v-block selection (alt + j,k) {{{
" with vscode-neovim, vscode has a native way to move lines that less laggy, we override this in settings.json
vnoremap <A-j> :m '>+1<CR>gv=gv
vnoremap <A-k> :m '<-2<CR>gv=gv
" }}}
" Others {{{
" Quickly write as sudo (Doesn't work on nvim! waiting for a fix...)
if !has('nvim')
noremap <leader>W :w !sudo tee %:p >/dev/null<CR>
endif
" TO DO: only do subst within selected visual-line lines
" vmap <expr> <leader>q mode() == 'v' ? "j" : "k"
" }}}
" }}} 3. KEYMAPPINGS
" 4. THINGS THAT SHOULD BE PLUGINS {{{
" ###############################################################################
" ## ##
" ## 4. THINGS THAT SHOULD BE PLUGINS ##
" ## ##
" ###############################################################################
" A useful function
"
" explore the update command (:h update)
command WriteIfModified if getbufinfo(bufnr('%'))[0].changed | w | endif
function WriteIfModified()
if getbufinfo(bufnr('%'))[0].changed
" return execute('w')->split('\n')[0]
return split(execute('w'), '\n')[0]
endif
return ''
endfunction
" }}} 4. THINGS THAT SHOULD BE PLUGINS
" In later neovim versions, these will be deprecated, use vim.lsp.diagnostic.config()
" instead. See :h diagnostic-signs
" nf-fa-times_circle
sign define DiagnosticSignError text= texthl=DiagnosticSignError linehl= numhl=
" nf-fa-warning
sign define DiagnosticSignWarn text= texthl=DiagnosticSignWarn linehl= numhl=
" nf-fa-info_circle
sign define DiagnosticSignInfo text= texthl=DiagnosticSignInfo linehl= numhl=
" nf-fa-lightbulb
sign define DiagnosticSignHint text= texthl=DiagnosticSignHint linehl= numhl=