Skip to content

Instantly share code, notes, and snippets.

@roachsinai
Last active April 29, 2024 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roachsinai/92f1841d2d4940be789f0a5b434b113c to your computer and use it in GitHub Desktop.
Save roachsinai/92f1841d2d4940be789f0a5b434b113c to your computer and use it in GitHub Desktop.
minimal vimrc
"
" A (not so) minimal vimrc.
" https://github.com/mhinz/vim-galore/blob/master/static/minimal-vimrc.vim
"
" You want Vim, not vi. When Vim finds a vimrc, 'nocompatible' is set anyway.
" We set it explicitely to make our position clear!
set nocompatible
filetype plugin indent on " Load plugins according to detected filetype.
syntax on " Enable syntax highlighting.
set autoindent " Indent according to previous line.
set pastetoggle =<F2> " https://zhuanlan.zhihu.com/p/652837014
set expandtab " Use spaces instead of tabs.
set tabstop =4
set shiftwidth =4 " >> indents by 4 spaces.
set softtabstop =4
set shiftround " >> indents to next multiple of 'shiftwidth'.
set backspace =indent,eol,start " Make backspace work as you would expect.
set hidden " Switch between buffers without having to save first.
set laststatus =2 " Always show statusline.
set display =lastline " Show as much as possible of the last line.
set showmode " Show current mode in command-line.
set showcmd " Show already typed keys when more are expected.
set incsearch " Highlight while searching with / or ?.
" set hlsearch " Keep matches highlighted.
set ttyfast " Faster redrawing.
set lazyredraw " Only redraw when necessary.
set splitbelow " Open new windows below the current window.
set splitright " Open new windows right of the current window.
set number
highlight LineNr term=bold cterm=NONE ctermfg=DarkGrey ctermbg=NONE gui=NONE guifg=DarkGrey guibg=NONE
" set cursorline " Find the current line quickly.
set wrapscan " Searches wrap around end-of-file.
set report =0 " Always report changed lines.
set synmaxcol =200 " Only highlight the first 200 columns.
set list " Show non-printable characters.
if has('multi_byte') && &encoding ==# 'utf-8'
let &listchars = 'tab:▸ ,extends:❯,precedes:❮,nbsp:±'
else
let &listchars = 'tab:> ,extends:>,precedes:<,nbsp:.'
endif
" The fish shell is not very compatible to other shells and unexpectedly
" breaks things that use 'shell'.
if &shell =~# 'fish$'
set shell=/bin/bash
endif
" https://gist.github.com/meskarune/57b613907ebd1df67eb7bdb83c6e6641
" status bar colors
au InsertEnter * hi statusline guifg=black guibg=#d7afff ctermfg=black ctermbg=magenta
au InsertLeave * hi statusline guifg=black guibg=#8fbfdc ctermfg=black ctermbg=cyan
hi statusline guifg=black guibg=#8fbfdc ctermfg=black ctermbg=cyan
" Status line
" default: set statusline=%f\ %h%w%m%r\ %=%(%l,%c%V\ %=\ %P%)
" Status Line Custom
let g:currentmode={
\ 'n' : 'Normal',
\ 'no' : 'Normal·Operator Pending',
\ 'v' : 'Visual',
\ 'V' : 'V·Line',
\ '' : 'V·Block',
\ 's' : 'Select',
\ 'S' : 'S·Line',
\ '' : 'V·Block',
\ 'i' : 'Insert',
\ 'R' : 'Replace',
\ 'Rv' : 'V·Replace',
\ 'c' : 'Command',
\ 'cv' : 'Vim Ex',
\ 'ce' : 'Ex',
\ 'r' : 'Prompt',
\ 'rm' : 'More',
\ 'r?' : 'Confirm',
\ '!' : 'Shell',
\ 't' : 'Terminal'
\}
function! StatuslineGit()
" use tirm above vim 7.4, if not trim will show garbled code: ^[[?12;4$y
let l:branchname = \
substitute(system('git rev-parse --abbrev-ref HEAD 2>/dev/null'), '^\s*\(.\{-}\)\s*$', '\1', '')
return strlen(l:branchname) > 0?' '.l:branchname.' ':''
endfunction
set laststatus=2
set noshowmode
set statusline=
set statusline+=%0*\ %{toupper(g:currentmode[mode()])}\ " The current mode
set statusline+=%0*\ %n\ " Buffer number
set statusline+=%1*\ %<%F%m%r%h%w\ " File path, modified, readonly, helpfile, preview
"set statusline+=%{StatuslineGit()}
set statusline+=%3*│ " Separator
set statusline+=%2*\ %Y\ " FileType
set statusline+=%3*│ " Separator
set statusline+=%2*\ %{''.(&fenc!=''?&fenc:&enc).''} " Encoding
set statusline+=\ (%{&ff}) " FileFormat (dos/unix..)
set statusline+=%= " Right Side
set statusline+=%2*\ col:\ %02v\ " Colomn number
set statusline+=%3*│ " Separator
set statusline+=%1*\ ln:\ %02l/%L\ (%3p%%)\ " Line number / total lines, percentage of document
set statusline+=%{&paste?'PASTE':''}
" https://stackoverflow.com/questions/5375240/a-more-useful-statusline-in-vim
hi User1 ctermfg=007 ctermbg=239 guibg=#4e4e4e guifg=#adadad
hi User2 ctermfg=007 ctermbg=236 guibg=#303030 guifg=#adadad
hi User3 ctermfg=236 ctermbg=236 guibg=#303030 guifg=#303030
hi User4 ctermfg=239 ctermbg=239 guibg=#4e4e4e guifg=#4e4e4e
" https://jeffkreeftmeijer.com/vim-number/
set number
augroup numbertoggle
autocmd!
autocmd BufEnter,FocusGained,InsertLeave,WinEnter * if &nu && mode() != "i" | set rnu | endif
autocmd BufLeave,FocusLost,InsertEnter,WinLeave * if &nu | set nornu | endif
augroup END
" mac os
" set clipboard=unnamed
let mapleader=" "
nmap <leader>q :q<CR>
nmap <leader>w :w<CR>
nmap <leader>wq :wq<CR>
nmap <tab>q :qall<CR>
xnoremap < <gv
xnoremap > >gv
nmap <leader>y "+y
nmap <leader>d "+d
nmap <leader>p "+p
vmap <leader>y "+y
vmap <leader>d "+d
" let g:fcitx5_remote = $HOME . '/.local/bin/fcitx-remote'
" let g:fcitx5_remote = '/usr/local/bin/im-select'
let g:fcitx5_remote = '/opt/homebrew/bin/im-select'
if has('macunix')
call plug#begin()
Plug 'roachsinai/fcitx.vim', { 'branch': 'macos' }
Plug 'mhinz/vim-grepper', { 'on': ['Grepper', '<plug>(GrepperOperator)'] }
call plug#end()
endif
if !has('gui_running') && !(has('win32') || has('win64'))
" set t_kb=
endif
inoremap <C-h> <left>
inoremap <C-j> <down>
inoremap <C-k> <up>
inoremap <C-l> <right>
inoremap <c-a> <home>
inoremap <c-e> <end>
inoremap <c-d> <del>
inoremap <c-_> <c-k>
inoremap <c-b> <c-left>
inoremap <c-f> <c-right>
cnoremap <c-h> <left>
cnoremap <c-j> <down>
cnoremap <c-k> <up>
cnoremap <c-l> <right>
cnoremap <c-a> <home>
cnoremap <c-e> <end>
cnoremap <c-d> <del>
cnoremap <c-_> <c-k>
cnoremap <c-b> <c-left>
cnoremap <c-f> <c-right>
" show commands begin with current inputs
cnoremap <m-f> <c-d>
" https://vi.stackexchange.com/a/3029/16299
function! Preserve(command)
" Preparation: save window state
let l:saved_winview = winsaveview()
" Run the command:
execute a:command
" Clean up: restore previous window position
call winrestview(l:saved_winview)
endfunction
nnoremap <leader>sw :call Preserve("%s/\\s\\+$//e")<CR>
xnoremap <leader>sw :call Preserve("'<,'>s/\\s\\+$//e")<CR>
hi TrailingWhitespace ctermbg=red guibg=red
call matchadd("TrailingWhitespace", '\v\s+$')
" needed in macos using iTerm2
if $TMUX != ''
set ttimeoutlen=20
elseif &ttimeoutlen > 60 || &ttimeoutlen <= 0
set ttimeoutlen=60
endif
" Get the directory of a file
" - On Ex command lines, returns the directory of the file ('./' for new files)
" - On other command lines (/,?) returns the keymap used to trigger it
function! CommandDir(keymap) abort
let l:command_type = getcmdtype()
if l:command_type isnot# ':'
return a:keymap
endif
let l:dir = expand('%:h')
if empty(l:dir)
let l:dir = '.'
endif
return l:dir . '/'
endfunction
" https://vi.stackexchange.com/a/21800/16299
" Explore
nnoremap <silent> <Leader>e :Explore<CR>
" Quickly input the directory of the current file on the command line
cnoremap <expr> %% CommandDir('%%')
" Need 'recursive' mapping here for %%
nmap <leader>n :e %%
set directory^=$HOME/.vim/swap//
set undofile
set undodir=$HOME/.vim/undo
" if !isdirectory(&undodir)
" call mkdir(&undodir, 'p', 0755)
" endif
" https://stackoverflow.com/a/14449484/6074780
autocmd BufReadPost *
\ if line("'\"") > 0 && line("'\"") <= line("$") |
\ exe "normal! g`\"" |
\ endif
nmap <leader>cs :.s/\[\zs.*\ze]/\=substitute(submatch(0), '_', '\\_', 'g')/<CR>
nmap <leader>zs k0Di[]<esc>PJxDa()<esc>P0
" https://github.com/vim-scripts/BufOnly.vim/blob/master/plugin/BufOnly.vim
command! -nargs=? -complete=buffer -bang Bonly
\ :call BufOnly('<args>', '<bang>')
command! -nargs=? -complete=buffer -bang BOnly
\ :call BufOnly('<args>', '<bang>')
command! -nargs=? -complete=buffer -bang Bufonly
\ :call BufOnly('<args>', '<bang>')
command! -nargs=? -complete=buffer -bang BufOnly
\ :call BufOnly('<args>', '<bang>')
function! BufOnly(buffer, bang)
if a:buffer == ''
" No buffer provided, use the current buffer.
let buffer = bufnr('%')
elseif (a:buffer + 0) > 0
" A buffer number was provided.
let buffer = bufnr(a:buffer + 0)
else
" A buffer name was provided.
let buffer = bufnr(a:buffer)
endif
if buffer == -1
echohl ErrorMsg
echomsg "No matching buffer for" a:buffer
echohl None
return
endif
let last_buffer = bufnr('$')
let delete_count = 0
let n = 1
while n <= last_buffer
if n != buffer && buflisted(n)
if a:bang == '' && getbufvar(n, '&modified')
echohl ErrorMsg
echomsg 'No write since last change for buffer'
\ n '(add ! to override)'
echohl None
else
silent exe 'bdel' . a:bang . ' ' . n
if ! buflisted(n)
let delete_count = delete_count+1
endif
endif
endif
let n = n+1
endwhile
if delete_count == 1
echomsg delete_count "buffer deleted"
elseif delete_count > 1
echomsg delete_count "buffers deleted"
endif
endfunction
" delete buffer without losing the split window
nnoremap <silent> <leader>bd :bp\|bd #<CR>
" delete all buffers except current
" command! BufOnly silent! execute "%bd|e#|bd#"
nnoremap <silent> <leader>bo :BufOnly<CR>
" if executable("rg")
" set grepprg=rg\ --vimgrep\ --smart-case\ --hidden
" set grepformat=%f:%l:%c:%m
" endif
nnoremap <leader>co :copen<CR>
nnoremap <leader>cc :cclose<CR>
let g:grepper = {}
let g:grepper.dir = 'filecwd'
nnoremap <Leader>ac :Grepper -tool rg -noprompt -cword<cr>
nnoremap <Leader>ai :Grepper -tool rg -noprompt -query<space>
" https://www.zhihu.com/question/636018229/answer/3386613023
command! -range ExecVimL call execute(getline(<line1>, <line2>), '')
command! -range ExecLua call execute('lua ' . join(getline(<line1>, <line2>), " \n "), '')
nnoremap <silent> <expr> <leader>s ((&ft=='lua' ? ':ExecLua' : ':ExecVimL') . '<cr>:<c-u>echo "Current line executed"<cr>')
xnoremap <silent> <expr> <leader>s ((&ft=='lua' ? ':ExecLua' : ':ExecVimL') . '<cr>:<c-u>echo "Selected range executed"<cr>')
" https://stackoverflow.com/a/16679182/6074780
" shfit+d in :Ex
nnoremap <leader>rm :call delete(expand('%')) \| bdelete!<CR>
" https://vi.stackexchange.com/a/2187/16299
nnoremap <leader>lb :set nomore <Bar> :ls <Bar> :set more <CR>:b<Space>
" https://skywind3000.com/blog/archives/2021/
function! Terminal_MetaMode(mode)
set ttimeout
if $TMUX != ''
set ttimeoutlen=30
elseif &ttimeoutlen > 80 || &ttimeoutlen <= 0
set ttimeoutlen=80
endif
if has('nvim') || has('gui_running')
return
endif
function! s:metacode(mode, key)
if a:mode == 0
exec "set <M-".a:key.">=\e".a:key
else
exec "set <M-".a:key.">=\e]{0}".a:key."~"
endif
endfunc
for i in range(10)
call s:metacode(a:mode, nr2char(char2nr('0') + i))
endfor
for i in range(26)
call s:metacode(a:mode, nr2char(char2nr('a') + i))
call s:metacode(a:mode, nr2char(char2nr('A') + i))
endfor
if a:mode != 0
for c in [',', '.', '/', ';', '[', ']', '{', '}']
call s:metacode(a:mode, c)
endfor
for c in ['?', ':', '-', '_']
call s:metacode(a:mode, c)
endfor
else
for c in [',', '.', '/', ';', '{', '}']
call s:metacode(a:mode, c)
endfor
for c in ['?', ':', '-', '_']
call s:metacode(a:mode, c)
endfor
endif
endfunc
call Terminal_MetaMode(0)
" https://www.zhihu.com/question/278228687/answer/413375553
noremap <m-h> <c-w>h
noremap <m-l> <c-w>l
noremap <m-j> <c-w>j
noremap <m-k> <c-w>k
inoremap <m-h> <esc><c-w>h
inoremap <m-l> <esc><c-w>l
inoremap <m-j> <esc><c-w>j
inoremap <m-k> <esc><c-w>k
noremap <m-H> <c-w>H
noremap <m-L> <c-w>L
noremap <m-J> <c-w>J
noremap <m-K> <c-w>K
inoremap <m-H> <esc><c-w>H
inoremap <m-L> <esc><c-w>L
inoremap <m-J> <esc><c-w>J
inoremap <m-K> <esc><c-w>K
if has('terminal') && exists(':terminal') == 2 && has('patch-8.1.1')
" vim 8.1 支持 termwinkey ,不需要把 terminal 切换成 normal 模式
" 设置 termwinkey 为 CTRL 加减号(GVIM),有些终端下是 CTRL+?
" 后面四个键位是搭配 termwinkey 的,如果 termwinkey 更改,也要改
set termwinkey=<c-_>
tnoremap <m-h> <c-_>h
tnoremap <m-l> <c-_>l
tnoremap <m-j> <c-_>j
tnoremap <m-k> <c-_>k
tnoremap <m-q> <c-\><c-n>
elseif has('nvim')
" neovim 没有 termwinkey 支持,必须把 terminal 切换回 normal 模式
tnoremap <m-h> <c-\><c-n><c-w>h
tnoremap <m-l> <c-\><c-n><c-w>l
tnoremap <m-j> <c-\><c-n><c-w>j
tnoremap <m-k> <c-\><c-n><c-w>k
tnoremap <m-q> <c-\><c-n>
endif
nnoremap <c-h> :vertical resize -5<cr>
nnoremap <c-k> :resize +5<cr>
nnoremap <c-j> :resize -5<cr>
nnoremap <c-l> :vertical resize +5<cr>
" insert tab at beginning of each line of current buffer
nnoremap <leader>it :%s/^/\t/g<CR>
" format zhihu share
nmap <leader>zs k0yss]Jxys$)^i1. <esc>^
" format csdn link/share
nmap <leader>cs :.s/\[\zs.*\ze]/\=substitute(submatch(0), '_', '\\_', 'g')/<CR>
@roachsinai
Copy link
Author

'�' in \ '�' : 'V·Block', is Ctrl V, Ctrl V.

and Ctrl V, Ctrl S in \ '�' : 'S·Block',.

@roachsinai
Copy link
Author

mkdir ~/.vim/swap
mkdir ~/.vim/undo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment