Xinqi Bao's Git

feat(mise): manage selected interactive tools
[dotfiles.git] / .config / nvim / init.vim
1 syntax enable
2 let mapleader=" "
3
4 " Files and indentation
5 set encoding=utf-8
6 " In order to display Chinese in GBK
7 set fileencodings=utf-8,gbk
8 set fileformat=unix
9 set tabstop=4
10 set softtabstop=4
11 set shiftwidth=4
12 set expandtab
13 set smartindent
14 set backspace=indent,eol,start
15 " Auto added comment leader; see :h formatoptions
16 set formatoptions+=/ro
17
18 " Display and search
19 set background=dark
20 set termguicolors
21 set number
22 set nowrap
23 set signcolumn=auto
24 set smartcase
25 set incsearch
26 set hlsearch
27 set ignorecase
28 set cursorline
29 set conceallevel=0
30 set showtabline=2
31 set laststatus=2 "always show status line
32 set noshowmode
33 set wildmenu
34 set colorcolumn=120
35 highlight ColorColumn ctermbg=0 guibg=lightgrey
36 " Fallback for filetypes without a Tree-sitter parser.
37 set foldmethod=indent
38 set foldlevelstart=99
39 set foldlevel=99
40
41 " Interaction and persistence
42 set mouse=a
43 set autochdir
44 set updatetime=300
45 set noerrorbells
46
47 " Turn backup off
48 set noswapfile
49 set nobackup
50 set nowritebackup
51 set undodir=~/.cache/nvim/undodir
52 set undofile
53
54 " Writes to the unnamed register also writes to the * and + registers. This
55 " makes it easy to interact with the system clipboard
56 if has ('unnamedplus')
57 set clipboard=unnamedplus
58 else
59 set clipboard=unnamed
60 endif
61
62 " File navigation
63 set tags=tags~;,tags~
64 "setting gf (go file) path
65 "change kernel version to your version, for kernel development
66 set path=.,lib;,include;,includes;
67 set path+=/usr/include,/usr/include/c++/*
68 set path+=/usr/local/include
69
70 call plug#begin()
71 " Appearance
72 Plug 'morhetz/gruvbox'
73 Plug 'vim-airline/vim-airline'
74 Plug 'lukas-reineke/indent-blankline.nvim'
75
76 " Navigation
77 Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
78 Plug 'junegunn/fzf.vim'
79 Plug 'preservim/nerdtree'
80 Plug 'christoomey/vim-tmux-navigator'
81 Plug 'mbbill/undotree'
82 Plug 'stevearc/aerial.nvim'
83
84 " Development
85 Plug 'nvim-treesitter/nvim-treesitter'
86 Plug 'neoclide/coc.nvim', {'branch': 'release'}
87 Plug 'zbirenbaum/copilot.lua'
88
89 " Git
90 Plug 'lewis6991/gitsigns.nvim'
91 Plug 'kdheepak/lazygit.nvim'
92
93 " Markdown
94 Plug 'MeanderingProgrammer/render-markdown.nvim'
95 Plug 'iamcco/markdown-preview.nvim', { 'do': 'cd app && npx --yes yarn install' }
96
97 " Optional
98 " execute 'source' fnameescape(expand('<sfile>:p:h') . '/opt/avante.vim')
99 " execute 'source' fnameescape(expand('<sfile>:p:h') . '/opt/leetcode.vim')
100
101 call plug#end()
102
103 " Appearance
104 if has_key(plugs, 'gruvbox')
105 colorscheme gruvbox
106 endif
107
108 if has_key(plugs, 'vim-airline')
109 "tabline with airline
110 ":help airline-tabline
111 let g:airline#extensions#tabline#enabled = 1
112 let g:airline#extensions#tabline#show_buffers = 0
113 let g:airline#extensions#tabline#show_splits = 0
114 let g:airline#extensions#tabline#show_tab_count = 0
115 let g:airline#extensions#tabline#show_close_button = 0
116 endif
117
118 if has_key(plugs, 'indent-blankline.nvim')
119 lua require('ibl').setup()
120 endif
121
122 " Navigation
123 if has_key(plugs, 'fzf.vim')
124 command! -bang -nargs=* GGrep
125 \ call fzf#vim#grep(
126 \ 'git grep --line-number -- ' . fzf#shellescape(<q-args>),
127 \ fzf#vim#with_preview({
128 \ 'dir': systemlist(['git', '-C', expand('%:p:h'), 'rev-parse', '--show-toplevel'])[0]
129 \ }), <bang>0)
130 nnoremap <C-p> :GFiles<CR>
131 nnoremap <C-f> :GGrep<CR>
132 endif
133
134 if has_key(plugs, 'nerdtree')
135 autocmd StdinReadPre * let s:std_in=1
136 "when vim openning with no file, open NERDTree
137 autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif
138 nnoremap <leader>n :NERDTreeToggle<CR>
139 endif
140
141 if has_key(plugs, 'undotree')
142 nnoremap <leader>u :UndotreeToggle<CR>
143 endif
144
145 if has_key(plugs, 'aerial.nvim')
146 lua require('aerial').setup()
147 nnoremap <leader>a <cmd>AerialToggle!<CR>
148 endif
149
150 " Development
151 if has_key(plugs, 'nvim-treesitter')
152 lua require('plugin-config/nvim-treesitter')
153 endif
154
155 if has_key(plugs, "coc.nvim")
156 " Use `:CocDiagnostics` to get all diagnostics of current buffer in location list.
157 nmap <silent> <leader>g[ <Plug>(coc-diagnostic-prev)
158 nmap <silent> <leader>g] <Plug>(coc-diagnostic-next)
159
160 " GoTo code navigation.
161 nmap <silent> <leader>gd <Plug>(coc-definition)
162 nmap <silent> <leader>gr <Plug>(coc-references)
163 nmap <silent> <leader>gy <Plug>(coc-type-definition)
164 nmap <silent> <leader>gi <Plug>(coc-implementation)
165
166 " Highlight the symbol and its references when holding the cursor.
167 autocmd CursorHold * silent call CocActionAsync('highlight')
168
169 " Symbol renaming.
170 nmap <leader>rn <Plug>(coc-rename)
171
172 " Formatting selected code.
173 xmap <leader>f <Plug>(coc-format-selected)
174 nmap <leader>f <Plug>(coc-format-selected)
175
176 " Apply AutoFix to problem on the current line.
177 nmap <leader>qf <Plug>(coc-fix-current)
178
179 " clangd
180 nmap <leader>gh :CocCommand clangd.switchSourceHeader<CR>
181
182 " Use K to show documentation in preview window.
183 nnoremap <silent> <leader>h :call <SID>show_documentation()<CR>
184 function! s:show_documentation()
185 if (index(['vim','help'], &filetype) >= 0)
186 execute 'h '.expand('<cword>')
187 elseif (coc#rpc#ready())
188 call CocActionAsync('doHover')
189 else
190 execute '!' . &keywordprg . " " . expand('<cword>')
191 endif
192 endfunction
193
194 let g:coc_default_semantic_highlight_groups = 1
195 let g:coc_global_extensions = [
196 \'coc-marketplace',
197 \'coc-highlight',
198 \'coc-clangd',
199 \'coc-pyright',
200 \'coc-lua',
201 \'coc-sh',
202 \'coc-cmake',
203 \'coc-xmake',
204 \'coc-json',
205 \'coc-yaml',
206 \'coc-vimlsp',
207 \]
208 endif
209
210 if has_key(plugs, 'copilot.lua')
211 lua require('plugin-config/copilot')
212 endif
213
214 " Git
215 if has_key(plugs, 'gitsigns.nvim')
216 lua require('plugin-config/gitsigns')
217 endif
218
219 if has_key(plugs, 'lazygit.nvim')
220 nnoremap <silent> <leader>lg :LazyGit<CR>
221 endif
222
223 " Optional
224 if has_key(plugs, 'avante.nvim')
225 lua require('plugin-config/avante')
226 endif
227
228 if has_key(plugs, 'leetcode.nvim')
229 lua require('plugin-config/leetcode')
230 endif
231
232 " General mappings
233 nnoremap J :tabprevious<CR>
234 nnoremap K :tabnext<CR>
235 nnoremap <leader>J :tabmove -1<CR>
236 nnoremap <leader>K :tabmove +1<CR>
237 nnoremap <leader>" viw<esc>a"<esc>bi"<esc>lel
238 nnoremap <leader>' viw<esc>a'<esc>bi'<esc>lel
239 nnoremap <silent> <Leader>+ :resize +5<CR>
240 nnoremap <silent> <Leader>- :resize -5<CR>
241 nnoremap <silent> <Leader>v+ :vertical resize +5<CR>
242 nnoremap <silent> <Leader>v- :vertical resize -5<CR>
243
244 " 禁用 <C-a>
245 nnoremap <C-a> <Nop>
246 xnoremap <C-a> <Nop>
247
248 "HighLight trailing whitespace"
249 highlight ExtraWhitespace ctermbg=red guibg=red
250 match ExtraWhitespace /\s\+$/