Xinqi Bao's Git

b138edb31e1a99416e5e6832318262bdfa2a6926
[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 'tpope/vim-fugitive'
91 Plug 'lewis6991/gitsigns.nvim'
92 Plug 'kdheepak/lazygit.nvim'
93
94 " Markdown
95 Plug 'MeanderingProgrammer/render-markdown.nvim'
96 Plug 'iamcco/markdown-preview.nvim', { 'do': 'cd app && npx --yes yarn install' }
97
98 " Optional
99 " execute 'source' fnameescape(expand('<sfile>:p:h') . '/opt/avante.vim')
100 " execute 'source' fnameescape(expand('<sfile>:p:h') . '/opt/leetcode.vim')
101
102 call plug#end()
103
104 " Appearance
105 if has_key(plugs, 'gruvbox')
106 colorscheme gruvbox
107 endif
108
109 if has_key(plugs, 'vim-airline')
110 "tabline with airline
111 ":help airline-tabline
112 let g:airline#extensions#tabline#enabled = 1
113 let g:airline#extensions#tabline#show_buffers = 0
114 let g:airline#extensions#tabline#show_splits = 0
115 let g:airline#extensions#tabline#show_tab_count = 0
116 let g:airline#extensions#tabline#show_close_button = 0
117 endif
118
119 if has_key(plugs, 'indent-blankline.nvim')
120 lua require('ibl').setup()
121 endif
122
123 " Navigation
124 if has_key(plugs, 'fzf.vim')
125 command! -bang -nargs=* GGrep
126 \ call fzf#vim#grep(
127 \ 'git grep --line-number -- ' . fzf#shellescape(<q-args>),
128 \ fzf#vim#with_preview({
129 \ 'dir': systemlist(['git', '-C', expand('%:p:h'), 'rev-parse', '--show-toplevel'])[0]
130 \ }), <bang>0)
131 nnoremap <C-p> :GFiles<CR>
132 nnoremap <C-f> :GGrep<CR>
133 endif
134
135 if has_key(plugs, 'nerdtree')
136 autocmd StdinReadPre * let s:std_in=1
137 "when vim openning with no file, open NERDTree
138 autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif
139 nnoremap <leader>n :NERDTreeToggle<CR>
140 endif
141
142 if has_key(plugs, 'undotree')
143 nnoremap <leader>u :UndotreeToggle<CR>
144 endif
145
146 if has_key(plugs, 'aerial.nvim')
147 lua require('aerial').setup()
148 nnoremap <leader>a <cmd>AerialToggle!<CR>
149 endif
150
151 " Development
152 if has_key(plugs, 'nvim-treesitter')
153 lua require('plugin-config/nvim-treesitter')
154 endif
155
156 if has_key(plugs, "coc.nvim")
157 " Use `:CocDiagnostics` to get all diagnostics of current buffer in location list.
158 nmap <silent> <leader>g[ <Plug>(coc-diagnostic-prev)
159 nmap <silent> <leader>g] <Plug>(coc-diagnostic-next)
160
161 " GoTo code navigation.
162 nmap <silent> <leader>gd <Plug>(coc-definition)
163 nmap <silent> <leader>gr <Plug>(coc-references)
164 nmap <silent> <leader>gy <Plug>(coc-type-definition)
165 nmap <silent> <leader>gi <Plug>(coc-implementation)
166
167 " Highlight the symbol and its references when holding the cursor.
168 autocmd CursorHold * silent call CocActionAsync('highlight')
169
170 " Symbol renaming.
171 nmap <leader>rn <Plug>(coc-rename)
172
173 " Formatting selected code.
174 xmap <leader>f <Plug>(coc-format-selected)
175 nmap <leader>f <Plug>(coc-format-selected)
176
177 " Apply AutoFix to problem on the current line.
178 nmap <leader>qf <Plug>(coc-fix-current)
179
180 " clangd
181 nmap <leader>gh :CocCommand clangd.switchSourceHeader<CR>
182
183 " Use K to show documentation in preview window.
184 nnoremap <silent> <leader>h :call <SID>show_documentation()<CR>
185 function! s:show_documentation()
186 if (index(['vim','help'], &filetype) >= 0)
187 execute 'h '.expand('<cword>')
188 elseif (coc#rpc#ready())
189 call CocActionAsync('doHover')
190 else
191 execute '!' . &keywordprg . " " . expand('<cword>')
192 endif
193 endfunction
194
195 let g:coc_default_semantic_highlight_groups = 1
196 let g:coc_global_extensions = [
197 \'coc-marketplace',
198 \'coc-highlight',
199 \'coc-clangd',
200 \'coc-pyright',
201 \'coc-lua',
202 \'coc-sh',
203 \'coc-cmake',
204 \'coc-xmake',
205 \'coc-json',
206 \'coc-yaml',
207 \'coc-vimlsp',
208 \]
209 endif
210
211 if has_key(plugs, 'copilot.lua')
212 lua require('plugin-config/copilot')
213 endif
214
215 " Git
216 if has_key(plugs, 'gitsigns.nvim')
217 lua require('plugin-config/gitsigns')
218 endif
219
220 if has_key(plugs, 'lazygit.nvim')
221 nnoremap <silent> <leader>lg :LazyGit<CR>
222 endif
223
224 " Optional
225 if has_key(plugs, 'avante.nvim')
226 lua require('plugin-config/avante')
227 endif
228
229 if has_key(plugs, 'leetcode.nvim')
230 lua require('plugin-config/leetcode')
231 endif
232
233 " General mappings
234 nnoremap J :tabprevious<CR>
235 nnoremap K :tabnext<CR>
236 nnoremap <leader>J :tabmove -1<CR>
237 nnoremap <leader>K :tabmove +1<CR>
238 nnoremap <leader>" viw<esc>a"<esc>bi"<esc>lel
239 nnoremap <leader>' viw<esc>a'<esc>bi'<esc>lel
240 nnoremap <silent> <Leader>+ :resize +5<CR>
241 nnoremap <silent> <Leader>- :resize -5<CR>
242 nnoremap <silent> <Leader>v+ :vertical resize +5<CR>
243 nnoremap <silent> <Leader>v- :vertical resize -5<CR>
244
245 " 禁用 <C-a>
246 nnoremap <C-a> <Nop>
247 xnoremap <C-a> <Nop>
248
249 "HighLight trailing whitespace"
250 highlight ExtraWhitespace ctermbg=red guibg=red
251 match ExtraWhitespace /\s\+$/