Skip to content

Instantly share code, notes, and snippets.

@hujianxin
Last active June 20, 2025 08:54
Show Gist options
  • Save hujianxin/30b8edd63f944bfda982700ba73c2370 to your computer and use it in GitHub Desktop.
Save hujianxin/30b8edd63f944bfda982700ba73c2370 to your computer and use it in GitHub Desktop.
.vimrc
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Vim-Plug Auto Installation
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Automatically install vim-plug if not present
if has('win32') || has('win64')
let plug_path = expand('~/vimfiles/autoload/plug.vim')
elseif has('nvim')
let plug_path = expand('~/.local/share/nvim/site/autoload/plug.vim')
else
let plug_path = expand('~/.vim/autoload/plug.vim')
endif
if empty(glob(plug_path))
if executable('curl')
silent execute '!curl -fLo ' . plug_path . ' --create-dirs '
\ . 'https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
else
echoerr 'curl is required to install vim-plug. Please install curl and restart Vim/Neovim.'
endif
endif
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Plugin Management
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
call plug#begin('~/.vim/plugged')
" Interface enhancements
Plug 'vim-airline/vim-airline' " Lean & mean status/tabline for Vim, with support for a lot of plugins
Plug 'tomasiser/vim-code-dark' " VS Code-like color scheme
" Search and navigation
Plug 'easymotion/vim-easymotion' " Fast navigation with minimal keystrokes
" Editing improvements
Plug 'tpope/vim-commentary' " Easy comment/uncomment
Plug 'neoclide/coc.nvim', {'branch': 'release'} " Intelligent code completion
" Git integration
Plug 'airblade/vim-gitgutter' " Show git diff in the sign column
" Text editing enhancements
Plug 'jiangmiao/auto-pairs' " Auto close brackets, quotes, etc.
Plug 'andymass/vim-matchup' " Enhanced % matching
Plug 'junegunn/vim-easy-align' " Easy alignment of text
Plug 'tpope/vim-repeat' " Enable repeating supported plugin maps
Plug 'tpope/vim-unimpaired' " Pairs of handy bracket mappings
call plug#end()
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => General Settings
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set nocompatible " Use Vim defaults instead of Vi
syntax enable " Enable syntax highlighting
filetype plugin indent on " Enable file type detection and plugins
" Set leader key to space (easier to reach)
let mapleader = " "
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Backup and Persistent Undo
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Save backup files
set backup
set writebackup
set backupdir=~/.vim/backup// " Directory for backup files
set directory=~/.vim/swap// " Directory for swap files
" Persistent undo
if has('persistent_undo')
set undofile " Enable persistent undo
set undodir=~/.vim/undo// " Directory for undo files
set undolevels=1000 " Maximum number of changes that can be undone
set undoreload=10000 " Maximum number lines to save for undo on buffer reload
endif
" Create necessary directories if they don't exist
if !isdirectory(expand('~/.vim/backup'))
silent !mkdir -p ~/.vim/backup
endif
if !isdirectory(expand('~/.vim/swap'))
silent !mkdir -p ~/.vim/swap
endif
if !isdirectory(expand('~/.vim/undo'))
silent !mkdir -p ~/.vim/undo
endif
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => User Interface
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set number " Show line numbers
set cursorline " Highlight current line
set showmatch " Highlight matching brackets
set laststatus=2 " Always show status line
" set noshowmode " Don't show mode (airline handles this)
" Color scheme
set t_Co=256
set t_ut=
colorscheme codedark " Set VS Code-like colors
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Text Editing
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Indentation
set autoindent " Copy indent from current line when starting a new line
set smartindent " Smart autoindenting when starting a new line
set expandtab " Use spaces instead of tabs
set tabstop=2 " Number of spaces that a <Tab> counts for
set shiftwidth=2 " Number of spaces to use for each step of (auto)indent
set softtabstop=2 " Number of spaces that a <Tab> counts for while editing
" Search
set ignorecase " Ignore case in search patterns
set smartcase " Override ignorecase if search pattern has upper case
set incsearch " Show search matches as you type
set hlsearch " Highlight all search matches
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Completion and Performance (CoC.nvim)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" https://raw.githubusercontent.com/neoclide/coc.nvim/master/doc/coc-example-config.vim
" May need for Vim (not Neovim) since coc.nvim calculates byte offset by count
" utf-8 byte sequence
set encoding=utf-8
" Some servers have issues with backup files, see #649
set nobackup
set nowritebackup
" Having longer updatetime (default is 4000 ms = 4s) leads to noticeable
" delays and poor user experience
set updatetime=300
" Always show the signcolumn, otherwise it would shift the text each time
" diagnostics appear/become resolved
set signcolumn=yes
" Use tab for trigger completion with characters ahead and navigate
" NOTE: There's always complete item selected by default, you may want to enable
" no select by `"suggest.noselect": true` in your configuration file
" NOTE: Use command ':verbose imap <tab>' to make sure tab is not mapped by
" other plugin before putting this into your config
inoremap <silent><expr> <TAB>
\ coc#pum#visible() ? coc#pum#next(1) :
\ CheckBackspace() ? "\<Tab>" :
\ coc#refresh()
inoremap <expr><S-TAB> coc#pum#visible() ? coc#pum#prev(1) : "\<C-h>"
" Make <CR> to accept selected completion item or notify coc.nvim to format
" <C-g>u breaks current undo, please make your own choice
inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm()
\: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"
function! CheckBackspace() abort
let col = col('.') - 1
return !col || getline('.')[col - 1] =~# '\s'
endfunction
" Use <c-space> to trigger completion
if has('nvim')
inoremap <silent><expr> <c-space> coc#refresh()
else
inoremap <silent><expr> <c-@> coc#refresh()
endif
" Use `[g` and `]g` to navigate diagnostics
" Use `:CocDiagnostics` to get all diagnostics of current buffer in location list
nmap <silent><nowait> [g <Plug>(coc-diagnostic-prev)
nmap <silent><nowait> ]g <Plug>(coc-diagnostic-next)
" GoTo code navigation
nmap <silent><nowait> gd <Plug>(coc-definition)
nmap <silent><nowait> gy <Plug>(coc-type-definition)
nmap <silent><nowait> gi <Plug>(coc-implementation)
nmap <silent><nowait> gr <Plug>(coc-references)
" Use K to show documentation in preview window
nnoremap <silent> K :call ShowDocumentation()<CR>
function! ShowDocumentation()
if CocAction('hasProvider', 'hover')
call CocActionAsync('doHover')
else
call feedkeys('K', 'in')
endif
endfunction
" Highlight the symbol and its references when holding the cursor
autocmd CursorHold * silent call CocActionAsync('highlight')
" Symbol renaming
nmap <leader>rn <Plug>(coc-rename)
augroup mygroup
autocmd!
" Setup formatexpr specified filetype(s)
autocmd FileType typescript,json setl formatexpr=CocAction('formatSelected')
augroup end
" Applying code actions to the selected code block
" Example: `<leader>aap` for current paragraph
xmap <leader>a <Plug>(coc-codeaction-selected)
nmap <leader>a <Plug>(coc-codeaction-selected)
" Remap keys for applying code actions at the cursor position
nmap <leader>ac <Plug>(coc-codeaction-cursor)
" Remap keys for apply code actions affect whole buffer
nmap <leader>as <Plug>(coc-codeaction-source)
" Apply the most preferred quickfix action to fix diagnostic on the current line
nmap <leader>qf <Plug>(coc-fix-current)
" Remap keys for applying refactor code actions
nmap <silent> <leader>re <Plug>(coc-codeaction-refactor)
xmap <silent> <leader>r <Plug>(coc-codeaction-refactor-selected)
nmap <silent> <leader>r <Plug>(coc-codeaction-refactor-selected)
" Run the Code Lens action on the current line
nmap <leader>cl <Plug>(coc-codelens-action)
" Map function and class text objects
" NOTE: Requires 'textDocument.documentSymbol' support from the language server
xmap if <Plug>(coc-funcobj-i)
omap if <Plug>(coc-funcobj-i)
xmap af <Plug>(coc-funcobj-a)
omap af <Plug>(coc-funcobj-a)
xmap ic <Plug>(coc-classobj-i)
omap ic <Plug>(coc-classobj-i)
xmap ac <Plug>(coc-classobj-a)
omap ac <Plug>(coc-classobj-a)
" Remap <C-f> and <C-b> to scroll float windows/popups
if has('nvim-0.4.0') || has('patch-8.2.0750')
nnoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? coc#float#scroll(1) : "\<C-f>"
nnoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? coc#float#scroll(0) : "\<C-b>"
inoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? "\<c-r>=coc#float#scroll(1)\<cr>" : "\<Right>"
inoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? "\<c-r>=coc#float#scroll(0)\<cr>" : "\<Left>"
vnoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? coc#float#scroll(1) : "\<C-f>"
vnoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? coc#float#scroll(0) : "\<C-b>"
endif
" Use CTRL-S for selections ranges
" Requires 'textDocument.selectionRange' support of language server
nmap <silent> <C-s> <Plug>(coc-range-select)
xmap <silent> <C-s> <Plug>(coc-range-select)
" Add `:Format` command to format current buffer
command! -nargs=0 Format :call CocActionAsync('format')
nnoremap = :Format<CR>
" Add `:Fold` command to fold current buffer
command! -nargs=? Fold :call CocAction('fold', <f-args>)
" Add `:OR` command for organize imports of the current buffer
command! -nargs=0 OR :call CocActionAsync('runCommand', 'editor.action.organizeImport')
" Add (Neo)Vim's native statusline support
" NOTE: Please see `:h coc-status` for integrations with external plugins that
" provide custom statusline: lightline.vim, vim-airline
set statusline^=%{coc#status()}%{get(b:,'coc_current_function','')}
" Mappings for CoCList
" Show all files
nnoremap <silent><nowait> <space>f :<C-u>CocList files<cr>
" Show all buffers
nnoremap <silent><nowait> <space>b :<C-u>CocList buffers<cr>
" Show all diagnostics
nnoremap <silent><nowait> <space>a :<C-u>CocList diagnostics<cr>
" Manage extensions
nnoremap <silent><nowait> <space>e :<C-u>CocList extensions<cr>
" Show commands
nnoremap <silent><nowait> <space>c :<C-u>CocList commands<cr>
" Find symbol of current document
nnoremap <silent><nowait> <space>o :<C-u>CocList outline<cr>
" Search workspace symbols
nnoremap <silent><nowait> <space>s :<C-u>CocList -I symbols<cr>
" Do default action for next item
nnoremap <silent><nowait> <space>j :<C-u>CocNext<CR>
" Do default action for previous item
nnoremap <silent><nowait> <space>k :<C-u>CocPrev<CR>
" Resume latest coc list
nnoremap <silent><nowait> <space>p :<C-u>CocListResume<CR>
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Clipboard and Paste Settings
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set clipboard=unnamed " Use system clipboard
" Improved paste mode handling
let &t_SI .= "\<Esc>[?2004h" " Enable bracketed paste mode in insert
let &t_EI .= "\<Esc>[?2004l" " Disable bracketed paste mode in normal
inoremap <silent> <C-v> <Esc>:set paste<CR>a<C-r>+<Esc>:set nopaste<CR>a
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Plugin Configurations
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Airline statusline configuration
let g:airline_theme='codedark'
let g:airline_powerline_fonts = 1 " Enable powerline fonts if you have them installed
let g:airline#extensions#branch#enabled = 1 " Enable branch extension for git
let g:airline#extensions#coc#enabled = 1 " Enable CoC extension for airline
let g:airline_left_sep=''
let g:airline_right_sep=''
" GitGutter settings
" Faster updates for git signs (default is 4000ms)
set updatetime=100
" Don't show signs for deleted lines (cleaner look)
let g:gitgutter_sign_removed = '▶'
" Custom signs for git status
let g:gitgutter_sign_added = '+'
let g:gitgutter_sign_modified = '~'
let g:gitgutter_sign_modified_removed = '~▶'
" Auto-pairs settings
" Don't jump to closing pair (more predictable behavior)
let g:AutoPairsMultilineClose = 0
" Disable specific pairs for specific filetypes if needed
" let g:AutoPairs = {'(':')', '[':']', '{':'}', "'":"'", '"':'"', '`':'`'}
" Matchup settings
" Enable matchup in more situations
let g:matchup_matchparen_offscreen = {'method': 'popup'} " Show offscreen matches in a popup
let g:matchup_override_vimtex = 1 " Better matching in LaTeX files
let g:matchup_delim_noskips = 2 " Don't skip comments
" Easy-align settings
" Start interactive EasyAlign in visual mode (e.g. vipga)
xmap ga <Plug>(EasyAlign)
" Start interactive EasyAlign for a motion/text object (e.g. gaip)
nmap ga <Plug>(EasyAlign)
" Unimpaired additional mappings (built on top of default ones)
" Some useful unimpaired mappings examples:
" [b - previous buffer ]b - next buffer
" [q - previous quickfix ]q - next quickfix
" [<Space> - add line above ]<Space> - add line below
" [e - exchange line above ]e - exchange line below
" EasyMotion configuration
" Disable default mappings to start fresh
let g:EasyMotion_do_mapping = 0
" Turn on case-insensitive feature
let g:EasyMotion_smartcase = 1
" Keep cursor column during JK motions
let g:EasyMotion_startofline = 0
" Direct key mappings
nmap s <Plug>(easymotion-s2)
nmap t <Plug>(easymotion-t2)
" Additional mappings with leader
map <Leader><Leader>w <Plug>(easymotion-w)
map <Leader><Leader>b <Plug>(easymotion-b)
map <Leader><Leader>j <Plug>(easymotion-j)
map <Leader><Leader>k <Plug>(easymotion-k)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Key Mappings
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" General mappings
nnoremap <leader>h :nohlsearch<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment