Skip to content

Instantly share code, notes, and snippets.

@zz-jason
Last active January 19, 2024 06:48
Show Gist options
  • Save zz-jason/35fa6d75f2460b3c223505d26cae6b43 to your computer and use it in GitHub Desktop.
Save zz-jason/35fa6d75f2460b3c223505d26cae6b43 to your computer and use it in GitHub Desktop.
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" General
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" set how many lines of history VIM has to remember
set history=1000
" enable filetype plugins
" filetype plugin on
" filetype indent on
filetype on
" Set to auto read when a file is changed from the outside
set autoread
au FocusGained,BufEnter * checktime
" With a map leader it's possible to do extra key combinations
" like <leader>w saves the current file
let mapleader = ","
" Fast saving
nmap <leader>w :w!<cr>
" :W sudo saves the file, useful for handling the permission-denied error
command! W execute 'w !sudo tee % > /dev/null' <bar> edit!
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" VIM user interface
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Set 7 lines to the cursor - when moving vertically using j/k
set so=7
" Avoid garbled characters in Chinese language windows OS
let $LANG='en'
set langmenu=en
source $VIMRUNTIME/delmenu.vim
source $VIMRUNTIME/menu.vim
" Turn on the Wild menu
set wildmenu
" Ignore compiled files
set wildignore=*.o,*~,*.pyc
if has("win16") || has("win32")
set wildignore+=.git\*,.hg\*,.svn\*
else
set wildignore+=*/.git/*,*/.hg/*,*/.svn/*,*/.DS_Store
endif
" Always show current position
set ruler
" Height of the command bar
set cmdheight=2
" A buffer becomes hidden when it is abandoned
set hid
" Configure backspace so it acts as it should act
set backspace=eol,start,indent
set whichwrap+=<,>,h,l
" Ignore case when searching
set ignorecase
" When searching try to be smart about cases
set smartcase
" Highlight search results
set hlsearch
" Makes search act like search in modern browsers
set incsearch
" Don't redraw while executing macros (good performance config)
set lazyredraw
" For regular expressions turn magic on
set magic
" Show matching brackets when text indicator is over them
set showmatch
" How many tenths of a second to blink when matching brackets
set mat=2
" No annoying sound on errors
set noerrorbells
set novisualbell
set t_vb=
set tm=500
" Properly disable sound on errors on MacVim
if has("gui_macvim")
autocmd GUIEnter * set vb t_vb=
endif
" Add a bit extra margin to the left
set foldcolumn=1
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Colors and fonts
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
syntax enable
try
set t_Co=256
colorscheme desert
catch
endtry
" Set utf8 as standard encoding and en_US as the standard language
set encoding=utf8
" Use Unix as the standard file type
set ffs=unix,dos,mac
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Files, backups and undo
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Turn backup off, since most stuff is in SVN, git etc. anyway...
set nobackup
set nowb
set noswapfile
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Text, tab and indent related
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Use spaces instead of tabs
set expandtab
" Be smart when using tabs ;)
set smarttab
" 1 tab == 4 spaces
set shiftwidth=4
set tabstop=4
set softtabstop=4
" Linebreak on 80 characters
" set linebreak
" set textwidth=80
set ai "Auto indent
set si "Smart indent
set wrap "Wrap lines
set number "Show line numbers
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Visual mode related
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
vnoremap <silent> * :<C-u>call VisualSelection('', '')<CR>/<C-R>=@/<CR><CR>
vnoremap <silent> # :<C-u>call VisualSelection('', '')<CR>?<C-R>=@/<CR><CR>
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Moving around, tabs, windows and buffers
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
try
set switchbuf=useopen,usetab,newtab
set stal=2
catch
endtry
" Return to last edit position when opening files (You want this!)
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Status line
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Always show the status line
set laststatus=2
" Format the status line
" set statusline=\ %{HasPaste()}%F%m%r%h\ %w\ \ CWD:\ %r%{getcwd()}%h\ \ \ Line:\ %l\ \ Column:\ %c
set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")}
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Editing mappings
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Delete trailing white space on save, useful for some filetypes ;)
fun! CleanExtraSpaces()
let save_cursor = getpos(".")
let old_query = getreg('/')
silent! %s/\s\+$//e
call setpos('.', save_cursor)
call setreg('/', old_query)
endfun
if has("autocmd")
autocmd BufWritePre * if !&binary | call CleanExtraSpaces() | endif
endif
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Misc
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Remove the Windows ^M - when the encodings gets messed up
noremap <Leader>m mmHmt:%s/<C-V><cr>//ge<cr>'tzt'm
" Quickly open a buffer for scribble
map <leader>q :e ~/buffer<cr>
" Quickly open a markdown buffer for scribble
map <leader>x :e ~/buffer.md<cr>
" Toggle paste mode on and off
map <leader>pp :setlocal paste!<cr>
"==============================================================================
" "plug.vim"
"==============================================================================
if empty(glob('~/.vim/autoload/plug.vim'))
silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
\ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
autocmd VimEnter * PlugInstall --sync | source $HOME/.vimrc
endif
call plug#begin('~/.vim/plugged')
" status/window/tabline beautifications
Plug 'altercation/vim-colors-solarized'
Plug 'Yggdroot/indentLine'
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
Plug 'airblade/vim-gitgutter'
" navigation
Plug 'scrooloose/nerdtree', {'on': 'NERDTreeToggle'}
Plug 'Xuyuanp/nerdtree-git-plugin'
" Plug 'pseewald/vim-anyfold'
Plug 'vim-scripts/restore_view.vim'
" searching
Plug '/usr/bin/fzf'
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'Yggdroot/LeaderF', {'do': './install.sh'}
Plug 'rking/ag.vim'
Plug 'tpope/vim-fugitive'
" editing & completion
Plug 'dense-analysis/ale'
Plug 'vim-autoformat/vim-autoformat'
" Plug 'ervandew/supertab'
Plug 'mzlogin/vim-markdown-toc'
Plug 'Raimondi/delimitMate'
Plug 'jiangmiao/auto-pairs'
Plug 'tpope/vim-surround'
if has('python3')
Plug 'SirVer/ultisnips'
endif
Plug 'github/copilot.vim', {'do': ':Copilot setup'}
" others
Plug 'skywind3000/asyncrun.vim', {'on': 'AsyncRun'}
" plugins for language server protocolprogramming
Plug 'prabirshrestha/async.vim' " reqiured by vim-lsp
Plug 'prabirshrestha/vim-lsp'
Plug 'jaxbot/semantic-highlight.vim'
call plug#end()
"==============================================================================
" "altercation/vim-colors-solarized"
"==============================================================================
if !empty(glob('~/.vim/plugged/vim-colors-solarized'))
syntax enable
if has('gui_running')
set background=light
else
set background=dark
endif
try
colorscheme solarized
catch
endtry
endif
"==============================================================================
" "vim-airline/vim-airline", "vim-airline/vim-airline-themes"
"==============================================================================
if !empty(glob('~/.vim/plugged/vim-airline'))
let g:airline_theme="solarized"
let g:airline#extensions#tabline#enabled = 1
let g:airline#extensions#tabline#buffer_nr_show = 1
let g:airline#extensions#whitespace#enabled = 0
let g:airline#extensions#whitespace#symbol = '!'
let g:airline_left_sep=''
let g:airline_right_sep=''
endif
"==============================================================================
" "scrooloose/nerdtree"
"==============================================================================
if !empty(glob('~/.vim/plugged/nerdtree'))
nmap ft :NERDTreeToggle<cr>
let NERDTreeMinimalUI = 1
autocmd StdinReadPre * let s:std_in=1
autocmd vimenter * if argc() == 0 && !exists("s:std_in") | exe 'NERDTree' | endif
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
endif
"==============================================================================
" "Xuyuanp/nerdtree-git-plugin"
"==============================================================================
if !empty(glob('~/.vim/plugged/nerdtree-git-plugin'))
let g:NERDTreeIndicatorMapCustom = {
\ "Modified" : "✹",
\ "Staged" : "✚",
\ "Untracked" : "✭",
\ "Renamed" : "➜",
\ "Unmerged" : "═",
\ "Deleted" : "✖",
\ "Dirty" : "✗",
\ "Clean" : "✔︎",
\ "Unknown" : "?"
\ }
endif
"==============================================================================
" "vim-scripts/restore_view.vim"
"==============================================================================
if !empty(glob('~/.vim/plugged/restore_view.vim'))
set viewoptions=cursor,folds,slash,unix
endif
"==============================================================================
" Plug 'junegunn/fzf'
"==============================================================================
if !empty(glob('~/.vim/plugged/fzf'))
if !empty(glob('/usr/bin/fzf'))
set rtp+=/usr/bin/fzf
endif
noremap <C-P> :FZF<cr>
let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.6 } }
endif
"==============================================================================
" Plug 'Yggdroot/LeaderF'
"==============================================================================
if !empty(glob('~/.vim/plugged/LeaderF'))
"noremap <c-p> :FZF<cr>
noremap fb :LeaderfBuffer<cr>
noremap fw :LeaderfWindow<cr>
noremap fs :LeaderfSelf<cr>
noremap fm :LeaderfMru<cr>
" let g:Lf_WindowPosition = 'bottom'
let g:Lf_WindowPosition = 'popup'
let g:Lf_WindowHeight = 10
let g:Lf_ShowRelativePath = 0
let g:Lf_CursorBlink = 0
let g:Lf_HideHelp = 1
let g:Lf_UseVersionControlTool = 1
let g:Lf_PreviewResult = {'Function':0, 'Colorscheme':1}
highlight Lf_hl_match gui=bold guifg=Blue cterm=bold ctermfg=21
highlight Lf_hl_matchRefine gui=bold guifg=Magenta cterm=bold ctermfg=201
let g:Lf_NormalMap = {
\ "File": [["<ESC>", ':exec g:Lf_py "fileExplManager.quit()"<CR>']],
\ "Buffer": [["<ESC>", ':exec g:Lf_py "bufExplManager.quit()"<CR>']],
\ "Mru": [["<ESC>", ':exec g:Lf_py "mruExplManager.quit()"<CR>']],
\ "Tag": [["<ESC>", ':exec g:Lf_py "tagExplManager.quit()"<CR>']],
\ "Function": [["<ESC>", ':exec g:Lf_py "functionExplManager.quit()"<CR>']],
\ "Colorscheme": [["<ESC>", ':exec g:Lf_py "colorschemeExplManager.quit()"<CR>']],
\ }
endif
"==============================================================================
" Plug 'rking/ag.vim'
"==============================================================================
if !empty(glob('~/.vim/plugged/ag.vim'))
let g:ag_prg="ag --vimgrep --smart-case --column"
let g:ag_highlight=1
endif
"==============================================================================
" Plug 'vim-autoformat/vim-autoformat'
"==============================================================================
if !empty(glob('~/.vim/plugged/vim-autoformat'))
if &filetype ==# 'c'
autocmd BufWritePre * :Autoformat
endif
if &filetype ==# 'cpp'
autocmd BufWritePre * :Autoformat
endif
endif
"==============================================================================
" Plug 'SirVer/ultisnips'
"==============================================================================
if !empty(glob('~/.vim/plugged/ultisnips'))
let g:UltiSnipsExpandTrigger = "<tab>"
let g:UltiSnipsJumpForwardTrigger = "<tab>"
let g:UltiSnipsJumpBackwardTrigger = "<s-tab>"
endif
"==============================================================================
" Plug 'skywind3000/asyncrun.vim'
"==============================================================================
if !empty(glob('~/.vim/plugged/asyncrun.vim'))
let g:asyncrun_open = 6
let g:asyncrun_bell = 1
let g:asyncrun_rootmarks = ['.root', '.svn', '.git', '.hg', '.project']
endif
"==============================================================================
" Plug 'prabirshrestha/vim-lsp'
"==============================================================================
if !empty(glob('~/.vim/plugged/vim-lsp'))
if executable('clangd')
au User lsp_setup call lsp#register_server({
\ 'name': 'clangd',
\ 'cmd': {server_info->['clangd']},
\ 'allowlist': ['c', 'cpp'],
\ })
endif
function! s:on_lsp_buffer_enabled() abort
setlocal omnifunc=lsp#complete
setlocal signcolumn=yes
if exists('+tagfunc') | setlocal tagfunc=lsp#tagfunc | endif
nmap <buffer> gd <plug>(lsp-definition)
nmap <buffer> gs <plug>(lsp-document-symbol-search)
nmap <buffer> gS <plug>(lsp-workspace-symbol-search)
nmap <buffer> gr <plug>(lsp-references)
nmap <buffer> gi <plug>(lsp-implementation)
nmap <buffer> gt <plug>(lsp-type-definition)
nmap <buffer> <leader>rn <plug>(lsp-rename)
nmap <buffer> [g <plug>(lsp-previous-diagnostic)
nmap <buffer> ]g <plug>(lsp-next-diagnostic)
nmap <buffer> K <plug>(lsp-hover)
" nnoremap <buffer> <expr><c-f> lsp#scroll(+4)
" nnoremap <buffer> <expr><c-d> lsp#scroll(-4)
let g:lsp_format_sync_timeout = 1000
autocmd! BufWritePre *.rs,*.go call execute('LspDocumentFormatSync')
" refer to doc to add more commands
endfunction
augroup lsp_install
au!
" call s:on_lsp_buffer_enabled only for languages that has the server registered.
autocmd User lsp_buffer_enabled call s:on_lsp_buffer_enabled()
augroup END
let g:lsp_semantic_enabled = 1
let g:lsp_diagnostics_enabled = 0
hi LspSemanticType ctermfg=32
hi LspSemanticClass ctermfg=32
hi LspSemanticInterface ctermfg=32
hi LspSemanticStruct ctermfg=32
hi LspSemanticProperty ctermfg=32
hi LspSemanticFunction ctermfg=32
hi LspSemanticMethod ctermfg=32
hi LspSemanticEnum ctermfg=32
hi LspNamespace ctermfg=32
hi LspSemanticParameter ctermfg=256
hi LspSemanticVariable ctermfg=256
hi LspSemanticTypeParameter ctermfg=256
hi LspSemanticKeyword ctermfg=142
hi LspSemanticOperator ctermfg=142
hi LspSemanticEvents ctermfg=142
hi LspSemanticRegexp ctermfg=142
hi LspSemanticString ctermfg=245
hi LspSemanticNumber ctermfg=245
hi LspSemanticModifier ctermfg=245
hi LspSemanticModifier ctermfg=136
hi LspSemanticEnumMember ctermfg=127
" LspSemanticComment
endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment