diff options
author | Hsieh Chin Fan <pham@topo.tw> | 2024-05-22 13:57:46 +0800 |
---|---|---|
committer | Hsieh Chin Fan <pham@topo.tw> | 2024-05-22 13:57:46 +0800 |
commit | 76922c395db1d9745e2a47f8c0584545958a3fee (patch) | |
tree | d616e2b0eaa79276ca17b513e42b862a102da9de /vim/init/basic.vim | |
parent | ffd076d2c434d6c850dd1a05a19ff477c2a29023 (diff) |
Update
Diffstat (limited to 'vim/init/basic.vim')
-rw-r--r-- | vim/init/basic.vim | 279 |
1 files changed, 279 insertions, 0 deletions
diff --git a/vim/init/basic.vim b/vim/init/basic.vim new file mode 100644 index 0000000..4c6cd45 --- /dev/null +++ b/vim/init/basic.vim | |||
@@ -0,0 +1,279 @@ | |||
1 | "====================================================================== | ||
2 | " | ||
3 | " init-basic.vim - Need vim tiny compatible | ||
4 | " | ||
5 | " Used for general usecases. No keymap and personal preference | ||
6 | " | ||
7 | " Use '*' to search for: | ||
8 | " VIM_BEHAVIOR | ||
9 | " VISUAL | ||
10 | " EDIT | ||
11 | " JUMP | ||
12 | " SEARCH | ||
13 | " SYNTAX_HIGHLIGHT | ||
14 | " BUFFERS | ||
15 | " ENCODING_PREFERENCE | ||
16 | " FOLDING | ||
17 | " MISC | ||
18 | "====================================================================== | ||
19 | |||
20 | |||
21 | "---------------------------------------------------------------------- | ||
22 | " VIM_BEHAVIOR | ||
23 | "---------------------------------------------------------------------- | ||
24 | |||
25 | let mapleader = "," " Always use comma as leader key | ||
26 | set nocompatible " Disable vi compatible, today is 20XX | ||
27 | set autochdir " Automatically cd to current file | ||
28 | set path=.,** " Allow :find with completion | ||
29 | set mouse= " Disable mouse selection | ||
30 | set winaltkeys=no " Allow alt key for mapping | ||
31 | set cursorline | ||
32 | |||
33 | " Turn persistent undo on | ||
34 | " means that you can undo even when you close a buffer/VIM | ||
35 | set undofile | ||
36 | set undodir=~/.vim/.undodir | ||
37 | set conceallevel=1 | ||
38 | |||
39 | " Apply plugin and indent by filetype | ||
40 | filetype plugin indent on | ||
41 | |||
42 | |||
43 | "---------------------------------------------------------------------- | ||
44 | " VISUAL | ||
45 | "---------------------------------------------------------------------- | ||
46 | |||
47 | colorscheme desert " I like desert! | ||
48 | " In most of the cases, it is overrides by lightline.vim | ||
49 | set statusline=\ %F%m%r%h\ %w\ \ CWD:\ %r%{getcwd()}%h\ \ \ Line:\ %l\ \ Column:\ %c | ||
50 | set showmatch " Show pairing brackets | ||
51 | |||
52 | set number relativenumber " Use relativenumber | ||
53 | set wrap " Disable wrap by default | ||
54 | set scrolloff=3 " Leave some buffer when scrolling down | ||
55 | set ruler " Show cursor position | ||
56 | set laststatus=2 " Always show the status line | ||
57 | set guicursor=n-v-c-sm:block,i-ci-ve:ver25,r-cr-o:hor20 | ||
58 | |||
59 | |||
60 | "---------------------------------------------------------------------- | ||
61 | " EDIT | ||
62 | "---------------------------------------------------------------------- | ||
63 | |||
64 | set backspace=eol,start,indent " Set Backspace behaviors | ||
65 | set autoindent " If current line has indent, automatically set indent for next line | ||
66 | set cindent | ||
67 | set ttimeout | ||
68 | set ttimeoutlen=50 | ||
69 | set updatetime=250 | ||
70 | |||
71 | imap <C-c> <Esc>l | ||
72 | " Change IM to US when exit to Normal mode | ||
73 | autocmd InsertLeave * :silent !fcitx-remote -c &>/dev/null || true | ||
74 | |||
75 | |||
76 | "---------------------------------------------------------------------- | ||
77 | " JUMP | ||
78 | "---------------------------------------------------------------------- | ||
79 | |||
80 | set isfname=@,48-57,/,.,-,_,+,,,#,$,%,~ " This affects filename recognition for gf (go to file) | ||
81 | set suffixesadd=.md " Enable reference markdown file without extension | ||
82 | |||
83 | |||
84 | "---------------------------------------------------------------------- | ||
85 | " SEARCH | ||
86 | "---------------------------------------------------------------------- | ||
87 | set ignorecase " Search case without case sensation | ||
88 | set smartcase | ||
89 | set hlsearch " Hilight all matched texts | ||
90 | set incsearch " Show matched strings when typing | ||
91 | |||
92 | |||
93 | "---------------------------------------------------------------------- | ||
94 | " SYNTAX_HIGHLIGHT | ||
95 | "---------------------------------------------------------------------- | ||
96 | |||
97 | syntax enable | ||
98 | |||
99 | function! GetHighlightGroupName() | ||
100 | let l:syntaxID = synID(line('.'), col('.'), 1) | ||
101 | let l:groupName = synIDattr(l:syntaxID, 'name') | ||
102 | echo "Highlight Group Name: " . l:groupName | ||
103 | endfunction | ||
104 | |||
105 | " Defualt highlight for matched parenthesis is so weird in many colorscheme | ||
106 | " Why the background color is lighter than my caret !? | ||
107 | highlight MatchParen ctermfg=NONE ctermbg=darkgrey cterm=NONE | ||
108 | highlight LuaParen ctermfg=NONE ctermbg=darkgrey cterm=NONE | ||
109 | |||
110 | " Show trailing spaces | ||
111 | highlight ExtraWhitespace ctermbg=red guibg=red | ||
112 | match ExtraWhitespace /\s\+$/ | ||
113 | |||
114 | " Persist visualized lines | ||
115 | " define line highlight color | ||
116 | highlight MultiLineHighlight ctermbg=LightYellow guibg=LightYellow ctermfg=Black guifg=Black | ||
117 | " highlight the current line | ||
118 | nnoremap <silent> <leader><leader>h :call matchadd('MultiLineHighlight', '\%'.line('.').'l')<CR> | ||
119 | " clear all the highlighted lines | ||
120 | nnoremap <silent> <leader><leader>H :call clearmatches()<CR> | ||
121 | |||
122 | |||
123 | "---------------------------------------------------------------------- | ||
124 | " BUFFERS | ||
125 | "---------------------------------------------------------------------- | ||
126 | |||
127 | " Set to auto read when a file is changed from the outside | ||
128 | " Unnamed buffer like CmdWindows should prevent this | ||
129 | set autoread | ||
130 | autocmd FocusGained,BufEnter .* checktime | ||
131 | |||
132 | let g:quitVimWhenPressingCtrlC = 1 | ||
133 | function! ToggleQuit() | ||
134 | let g:quitVimWhenPressingCtrlC = g:quitVimWhenPressingCtrlC ? 0 : 1 | ||
135 | let message = g:quitVimWhenPressingCtrlC ? "Unlock" : "Lock" | ||
136 | echo message | ||
137 | endfunction | ||
138 | |||
139 | nnoremap gl :call ToggleQuit()<CR> | ||
140 | |||
141 | " Simply exit when closing the last buffer | ||
142 | function! Bye() | ||
143 | " Delete current buffer if working on special filetype | ||
144 | let specialFileTypes = ['help', 'netrw', 'vim-plug', 'nerdtree'] | ||
145 | if index(specialFileTypes, &filetype) != -1 | ||
146 | :bdelete | ||
147 | " Delete current buffer if more than one buffers | ||
148 | elseif len(filter(range(1, bufnr('$')), 'buflisted(v:val)')) != 1 | ||
149 | :bdelete | ||
150 | elseif g:quitVimWhenPressingCtrlC | ||
151 | " Otherwise, quit vim | ||
152 | :silent! qall | ||
153 | else | ||
154 | :echo "Press gl to allow quit with <C-c>" | ||
155 | endif | ||
156 | endfunction | ||
157 | |||
158 | " Ctrl-C rules!!! | ||
159 | nnoremap <silent> <C-c> :call Bye()<CR> | ||
160 | |||
161 | " Don't unload a buffer when no longer shown in a window | ||
162 | " This allows you open a new buffer and leaves current buffer modified | ||
163 | set hidden | ||
164 | |||
165 | |||
166 | " Put these in an autocmd group, so that you can revert them with: | ||
167 | " ":augroup vimStartup | au! | augroup END" | ||
168 | augroup vimStartup | ||
169 | au! | ||
170 | |||
171 | " When editing a file, always jump to the last known cursor position. | ||
172 | " Don't do it when the position is invalid, when inside an event handler | ||
173 | " (happens when dropping a file on gvim) and for a commit message | ||
174 | " (it's likely a different one than last time). | ||
175 | autocmd BufReadPost * | ||
176 | \ if line("'\"") >= 1 && line("'\"") <= line("$") && &ft !~# 'commit' | ||
177 | \ | exe "normal! g`\"" | ||
178 | \ | endif | ||
179 | augroup END | ||
180 | |||
181 | " Set filetype for beancount | ||
182 | autocmd BufRead,BufNewFile *.bean call PrepareBean() | ||
183 | function PrepareBean() | ||
184 | set filetype=beancount | ||
185 | silent !setsid fava ~/bean/main.bean &>/dev/null | ||
186 | autocmd VimLeave * silent !killall fava | ||
187 | endfunction | ||
188 | |||
189 | " Set filetype for index.html | ||
190 | autocmd BufWrite *.html,*.js,*.css call ReloadServer() | ||
191 | function ReloadServer() | ||
192 | silent !browser-sync reload &>/dev/null | ||
193 | endfunction | ||
194 | |||
195 | " Hide the first line of a file if editing password file | ||
196 | " TODO a better way to determine a file is related to password-store, now use | ||
197 | " files under /dev/shm as filter | ||
198 | autocmd BufRead /dev/shm/*.txt call SetPasswordFile() | ||
199 | function SetPasswordFile() | ||
200 | setlocal foldminlines=0 | ||
201 | setlocal foldmethod=manual | ||
202 | setlocal foldtext= | ||
203 | norm! ggzfl | ||
204 | endfunction | ||
205 | |||
206 | |||
207 | |||
208 | "---------------------------------------------------------------------- | ||
209 | " ENCODING_PREFERENCE | ||
210 | "---------------------------------------------------------------------- | ||
211 | if has('multi_byte') | ||
212 | set encoding=utf-8 | ||
213 | set fileencoding=utf-8 | ||
214 | " Try encodings by this order | ||
215 | set fileencodings=utf-8,big5,ucs-bom,gbk,gb18030,euc-jp,latin1 | ||
216 | endif | ||
217 | |||
218 | |||
219 | "---------------------------------------------------------------------- | ||
220 | " FOLDING | ||
221 | "---------------------------------------------------------------------- | ||
222 | set foldenable " Allow fold | ||
223 | set foldmethod=indent " Fold contents by indent | ||
224 | set foldlevel=99 " Expand all by default | ||
225 | |||
226 | |||
227 | "---------------------------------------------------------------------- | ||
228 | " MISC | ||
229 | "---------------------------------------------------------------------- | ||
230 | |||
231 | " 顯示括號匹配的時間 | ||
232 | set matchtime=2 | ||
233 | |||
234 | " 顯示最後一行 | ||
235 | set display=lastline | ||
236 | |||
237 | " 允許下方顯示目錄 | ||
238 | set wildmenu | ||
239 | |||
240 | " Improve performance | ||
241 | set lazyredraw | ||
242 | |||
243 | " Format of error message | ||
244 | set errorformat+=[%f:%l]\ ->\ %m,[%f:%l]:%m | ||
245 | |||
246 | " 顯示分隔符號 | ||
247 | set listchars=tab:\|\ ,trail:.,extends:>,precedes:< | ||
248 | |||
249 | " 遇到Unicode值大於255的文本,不必等到空格再折行 | ||
250 | set formatoptions+=m | ||
251 | |||
252 | " 合併兩行中文時,不在中間加空格 | ||
253 | set formatoptions+=B | ||
254 | |||
255 | " Use Unix way to add newline | ||
256 | set ffs=unix,dos,mac | ||
257 | |||
258 | |||
259 | "---------------------------------------------------------------------- | ||
260 | " Ignore these suffixes when find/complete | ||
261 | "---------------------------------------------------------------------- | ||
262 | set suffixes=.bak,~,.o,.h,.info,.swp,.obj,.pyc,.pyo,.egg-info,.class | ||
263 | |||
264 | set wildignore=*.o,*.obj,*~,*.exe,*.a,*.pdb,*.lib "stuff to ignore when tab completing | ||
265 | set wildignore+=*.so,*.dll,*.swp,*.egg,*.jar,*.class,*.pyc,*.pyo,*.bin,*.dex | ||
266 | set wildignore+=*.zip,*.7z,*.rar,*.gz,*.tar,*.gzip,*.bz2,*.tgz,*.xz " MacOSX/Linux | ||
267 | set wildignore+=*DS_Store*,*.ipch | ||
268 | set wildignore+=*.gem | ||
269 | set wildignore+=*.png,*.jpg,*.gif,*.bmp,*.tga,*.pcx,*.ppm,*.img,*.iso | ||
270 | set wildignore+=*.so,*.swp,*.zip,*/.Trash/**,*.pdf,*.dmg,*/.rbenv/** | ||
271 | set wildignore+=*/.nx/**,*.app,*.git,.git | ||
272 | set wildignore+=*.wav,*.mp3,*.ogg,*.pcm | ||
273 | set wildignore+=*.mht,*.suo,*.sdf,*.jnlp | ||
274 | set wildignore+=*.chm,*.epub,*.pdf,*.mobi,*.ttf | ||
275 | set wildignore+=*.mp4,*.avi,*.flv,*.mov,*.mkv,*.swf,*.swc | ||
276 | set wildignore+=*.ppt,*.pptx,*.docx,*.xlt,*.xls,*.xlsx,*.odt,*.wps | ||
277 | set wildignore+=*.msi,*.crx,*.deb,*.vfd,*.apk,*.ipa,*.bin,*.msu | ||
278 | set wildignore+=*.gba,*.sfc,*.078,*.nds,*.smd,*.smc | ||
279 | set wildignore+=*.linux2,*.win32,*.darwin,*.freebsd,*.linux,*.android | ||