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
|
" Basic settings.
set number
set noshowmode
set signcolumn=yes
set updatetime=250
set scrolloff=5
" Change directory based on current file.
if !empty($NIX_SHELL)
" Attempt to conflict less with plugins.
autocmd BufEnter * if gitbranch#name() != '' | silent! lcd %:p:h
else
set autochdir
endif
" inotify compat?
set backupcopy=yes
" Old defaults (I think).
set splitright
set splitbelow
" Syntax and indent from filetype.
set nocompatible
filetype plugin indent on
syntax on
" Check for SHIFT_JIS.
set fileencodings=ucs-bom,utf-8,sjis,default,latin1
" 4 spaces autoindent.
set tabstop=4
set shiftwidth=4
set expandtab
set autoindent
" C syntax preference.
let g:c_syntax_for_h=1
let g:c_no_curly_error=1
let g:c_function_pointers=1
set cinoptions=:0,=4,l1,g2,h2,(4,u4,+4,m1,j1,J1
set cindent
" Set different format for mail and text files.
set nowrap nolinebreak nolist formatoptions=l textwidth=0
autocmd FileType mail,text setlocal wrap formatoptions=l textwidth=0
" Ex tab complete that makes sense to me.
set wildmode=longest,list,full
set wildmenu
" Allow distinguishing <C-i> from <Tab> in bindings.
let &t_TI = '\<Esc>[>4;2m'
let &t_TE = '\<Esc>[>4;m'
" Treat shift + backspace as normal backspace.
tnoremap <S-BS> <BS>
" System clipboard on linux.
set clipboard+=unnamedplus
" Session options.
set ssop=skiprtp,blank,buffers,folds,winsize,terminal
set vop-=curdir
if isdirectory(expand('~/c'))
let view_dir = expand('~/c/sessions/nvim/views')
if !isdirectory(view_dir)
call mkdir(view_dir, 'p', 0755)
endif
let &viewdir=view_dir
" Staggered backups.
let backup_dir = '/mnt/store/backup/vim'
if !isdirectory(backup_dir)
let backup_dir = expand('~/c/sessions/nvim/backup//')
if !isdirectory(backup_dir)
call mkdir(backup_dir, 'p', 0755)
endif
endif
let &backupdir=backup_dir
set backup
" Swap file directory.
let swap_dir = expand('~/c/sessions/nvim/swaps//')
if !isdirectory(swap_dir)
call mkdir(swap_dir, 'p', 0755)
endif
let &directory=swap_dir
if has('persistent_undo')
let undo_dir = expand('~/c/sessions/nvim/undo')
if !isdirectory(undo_dir)
call mkdir(undo_dir, 'p', 0755)
endif
let &undodir=undo_dir
set undofile
endif
endif
" Colorscheme.
set termguicolors
colo wal-gui
" Cursor blink.
"set guicursor=a:blinkon1
" Highlight whitespace.
au BufEnter * match ExtraWhitespace /\s\+$/
au InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
au InsertLeave * match ExtraWhitespace /\s\+$/
" Fzf format.
let g:fzf_colors = {
\'fg': ['fg', 'Normal'],
\'bg': ['bg', 'Normal'],
\'hl': ['fg', 'Type'],
\'fg+': ['fg', 'CursorLine', 'CursorColumn', 'Normal'],
\'bg+': ['bg', 'CursorLine', 'CursorColumn'],
\'hl+': ['fg', 'Type'],
\'info': ['fg', 'PreProc'],
\'border': ['fg', 'Operator'],
\'prompt': ['fg', 'Identifier'],
\'pointer': ['fg', 'Type'],
\'marker': ['fg', 'Keyword'],
\'spinner': ['fg', 'String'],
\'header': ['fg', 'Type']
\}
let g:fzf_preview_window = ['right,55%']
" This is redundant but I want to keep FZF_DEFAULT_OPTS here.
let $FZF_DEFAULT_OPTS = '--preview-window=border-sharp'
let g:fzf_layout = {
\'window': { 'width': 0.75, 'height': 0.60, 'rounded': 0 },
\}
function! StatuslineMode() abort
if mode() == nr2char(22)
return mode()
else
return '-'.mode()
endif
endfunction
function! StatuslineGit()
let l:branchname = gitbranch#name()
return strlen(l:branchname) > 0 ? ' '.l:branchname.' ' : ' '
endfunction
function! StatuslineLsp() abort
return luaeval('get_lsp_name()')
endfunction
" Custom statusline.
function! Statusline() abort
let focused = g:statusline_winid == win_getid(winnr())
let status = focused ? '%#StatusLine#' : '%#StatusLineNC#'
let status .= '%{StatuslineGit()}'
let status .= ' %f'
let status .= ' vim@%{hostname()}'
let status .= '%='
let status .= ' %y'
let status .= '%{StatuslineLsp()}'
let status .= ' %{&fileencoding}'
let status .= '[%{&fileformat}]'
let status .= '%3p%%'
let status .= ' %3l:%-3c'
return status
endfunction
set statusline=%!Statusline()
" Make shift + nav keys no-ops.
nmap H <nop>
nmap J <nop>
nmap K <nop>
nmap L <nop>
" Normal mode tab no-op.
nmap <Tab> <nop>
" ctrl + c no-op.
nmap <C-c> <nop>
" Disable F1 for help.
nmap <F1> <nop>
" Disable q: for histroy.
nmap q: <nop>
" Simplify split movement.
nmap <silent> <C-h> :wincmd h<CR>
nmap <silent> <C-j> :wincmd j<CR>
nmap <silent> <C-k> :wincmd k<CR>
nmap <silent> <C-l> :wincmd l<CR>
nmap <silent> <C-o> :vsplit<CR>
" Make ctrl + '.' and ',' map to previous buffer.
nmap <C-,> <C-^>
nmap <C-.> <C-^>
" Swap ctrl + f and b.
"nnoremap <C-b> <C-f>
"nnoremap <C-f> <C-b>
" Toggle colorcolumn.
nmap <silent> C :execute 'set colorcolumn=' . (&colorcolumn == '' ? '101' : '')<CR>
" Personally intuitive pop-up menu binds.
inoremap <expr> <CR> pumvisible() ? '<C-y><CR>' : '<CR>'
inoremap <expr> <Up> pumvisible() ? '<C-p>' : '<Up>'
inoremap <expr> <Down> pumvisible() ? '<C-n>' : '<Down>'
" Lsp Binds.
nmap <C-d> <C-]>
" Fzf binds.
nmap <silent> \ :Buffers<CR>
nmap <silent> <C-o> :GGrep<CR>
nmap <silent> <C-\> :GFiles<CR>
" Only Neogit bind, handle all further operations from this menu.
nmap <silent> Y :Neogit<CR>
nmap <silent> L :lua neogit_log_current()<CR>
" Undotree.
nmap <silent> U :UndotreeToggle<CR>
" Toggle hex color highlighting.
nnoremap <silent> # :CccHighlighterToggle<CR>
command Tabify set noexpandtab | %retab! | set expandtab
command Untabify set expandtab | %retab!
" https://stackoverflow.com/a/37040415
function! SyntaxGroup()
let l:s = synID(line('.'), col('.'), 1)
echo synIDattr(l:s, 'name') . ' -> ' . synIDattr(synIDtrans(l:s), 'name')
endfun
" https://vi.stackexchange.com/a/456
function! TrimWhitespace()
let l:save = winsaveview()
keeppatterns %s/\s\+$//e
call winrestview(l:save)
endfun
" https://github.com/junegunn/fzf.vim?tab=readme-ov-file#example-git-grep-wrapper
command! -bang -nargs=* GGrep
\ call fzf#vim#grep(
\ 'git grep --line-number -- '.fzf#shellescape(<q-args>),
\ fzf#vim#with_preview({'dir': systemlist('git rev-parse --show-toplevel')[0]}), <bang>0)
" https://vim.fandom.com/wiki/Replace_a_builtin_command_using_cabbrev
function! CommandCabbr(abbreviation, expansion)
execute 'cabbr ' . a:abbreviation . ' <C-R>=getcmdpos() == 1 && getcmdtype() == ":" ? "' . a:expansion . '" : "' . a:abbreviation . '"<CR>'
endfunction
command! -nargs=+ CommandCabbr call CommandCabbr(<f-args>)
CommandCabbr no nohlsearch
call Juliana#Init() " Custom insert mode tab behavior inherited from Emacs.
inoremap <silent><Tab> <C-R>=Juliana#CompleteTab('START')<CR>
\<C-R>=Juliana#CompleteTab('TAB')<CR>
\<C-R>=Juliana#CompleteTab('ALIGN')<CR>
\<C-R>=Juliana#CompleteTab('NEXT')<CR>
inoremap <silent><S-Tab> <C-R>=Juliana#CompleteSTab('PREV')<CR>
lua << EOF
function get_lsp_name()
local active_clients = vim.lsp.get_clients({bufnr = vim.api.nvim_get_current_buf()})
if next(active_clients) == nil then
return ''
else
str = '<'
for _, v in pairs(active_clients) do
str = str .. v.name .. '+'
end
return str:sub(1, -2) .. '>'
end
end
buf_is_big = function(bufnr)
local max_filesize = 100 * 1024 -- 100KB
local ok, stats = pcall(vim.loop.fs_stat, vim.api.nvim_buf_get_name(bufnr))
if ok and stats and stats.size > max_filesize then
return true
else
return false
end
end
-- LSP configs.
if os.getenv('NIX_SHELL') ~= nil then
vim.diagnostic.config({ virtual_text = true })
vim.lsp.config('clangd', {
cmd = {
'clangd',
'-j=4',
'--log=error',
'--malloc-trim',
'--background-index',
'--clang-tidy',
'--completion-style=bundled',
'--pch-storage=memory',
'--header-insertion=never',
'--header-insertion-decorators=0',
'--query-driver=gcc'
},
capabilities = {
textDocument = {
semanticHighlightingCapabilities = {
semanticHighlighting = true
},
completion = {
completionItem = {
snippetSupport = false
}
}
}
},
})
vim.lsp.enable({'clangd', 'ruff', 'ty'})
-- https://github.com/neovim/nvim-lspconfig/issues/2626#issuecomment-2117022664
-- https://github.com/neovim/nvim-lspconfig/issues/2508
vim.api.nvim_create_autocmd('BufReadPre', {
callback = function(t)
if buf_is_big(t.buf) then
vim.o.eventignore = 'FileType'
vim.schedule(function()
vim.o.eventignore = ''
print('Dropped FileType events because the buffer is too big')
end)
end
end
})
require('inc_rename').setup({})
vim.keymap.set('n', '<C-n>', function()
return ':IncRename ' .. vim.fn.expand('<cword>')
end, { expr = true })
end
-- Set sessions folder and autowrite.
if vim.fn.isdirectory(vim.fn.expand('~/c')) then
require('mini.sessions').setup({
autoread = false,
autowrite = true,
directory = '~/c/sessions'
})
end
-- Completion.
require('mini.completion').setup({
window = {
info = { border = 'none' },
signature = { border = 'none' }
}
})
-- Don't use tabs in Neogit.
require('neogit').setup({
disable_hint = true,
kind = 'vsplit',
commit_editor = { kind = 'split' },
commit_select_view = { kind = 'vsplit' },
commit_view = { kind = 'vsplit' },
log_view = { kind = 'split' },
reflog_view = { kind = 'split' },
preview_buffer = { kind = 'split' },
popup = { kind = 'split' },
refs_view = { kind = 'split' },
-- Allow basic movement in Neogit without mapping conflicts.
mappings = {
popup = { ['l'] = false, ['L'] = 'LogPopup', ['d'] = false },
status = { ['$'] = false, ['#'] = 'CommandHistory' }
},
integrations = {}
})
-- Doesn't work consistently.
function neogit_log_current()
require("neogit").action("log", "log_current", { "--", vim.fn.expand("%:p") })()
end
-- Preview colors in #<hex> format.
require('ccc').setup({ highlighter = { auto_enable = false, lsp = true } })
EOF
|