aboutsummaryrefslogtreecommitdiffhomepage
path: root/vim
diff options
context:
space:
mode:
authorHsieh Chin Fan <pham@topo.tw>2024-07-08 23:24:22 +0800
committerHsieh Chin Fan <pham@topo.tw>2024-07-08 23:24:22 +0800
commit7a98c89f22253177d9f5a0f83c9dbaa297eb54b0 (patch)
tree566f70c7c7777bd1983708f9bf6e285d28c93639 /vim
parentd834d6da82ba20e32699380d335a65936bad16f9 (diff)
Update
Diffstat (limited to 'vim')
-rw-r--r--vim/init.vim19
-rw-r--r--vim/init/basic.vim34
-rw-r--r--vim/init/config.vim169
-rw-r--r--vim/init/special_highlight.vim28
4 files changed, 109 insertions, 141 deletions
diff --git a/vim/init.vim b/vim/init.vim
index f2c7bb3..031a0ed 100644
--- a/vim/init.vim
+++ b/vim/init.vim
@@ -8,6 +8,7 @@ endif
8" Get current dir 8" Get current dir
9" let s:home = fnamemodify(resolve(expand('<sfile>:p')), ':h') 9" let s:home = fnamemodify(resolve(expand('<sfile>:p')), ':h')
10let s:home = '~/helper/vim' 10let s:home = '~/helper/vim'
11execute 'cd '.s:home
11 12
12" Load script in current dir 13" Load script in current dir
13" command! -nargs=1 LoadScript exec 'source '.s:home.'/'.'<args>' 14" command! -nargs=1 LoadScript exec 'source '.s:home.'/'.'<args>'
@@ -15,28 +16,24 @@ let s:home = '~/helper/vim'
15" Add current dir into runtimepath 16" Add current dir into runtimepath
16execute 'set runtimepath+='.s:home 17execute 'set runtimepath+='.s:home
17 18
18
19"---------------------------------------------------------------------- 19"----------------------------------------------------------------------
20" Locad Modules 20" Locad Scripts
21"---------------------------------------------------------------------- 21"----------------------------------------------------------------------
22 22
23" Basic configuration 23" Basic configuration
24source ~/helper/vim/init/basic.vim 24source init/basic.vim
25 25
26" Key mappings 26" Key mappings
27source ~/helper/vim/init/keymaps.vim 27source init/keymaps.vim
28 28
29" Extra config for different contexts 29" Extra config for different contexts
30source ~/helper/vim/init/config.vim 30source init/config.vim
31
32" Highlight
33source ~/helper/vim/init/special_highlight.vim
34 31
35if has('nvim') 32if has('nvim')
36 " For neovim 33 " For neovim
37 source ~/helper/vim/lazy.lua 34 source lazy.lua
38else 35else
39 " For vim 36 " For vim
40 source ~/helper/vim/init/plugins.vim 37 source init/plugins.vim
41 source ~/helper/vim/init/style.vim 38 source init/style.vim
42endif 39endif
diff --git a/vim/init/basic.vim b/vim/init/basic.vim
index b268c6b..e18423d 100644
--- a/vim/init/basic.vim
+++ b/vim/init/basic.vim
@@ -120,12 +120,7 @@ endfunction
120nnoremap <silent> <C-c> :call Bye()<CR> 120nnoremap <silent> <C-c> :call Bye()<CR>
121 121
122" }}} 122" }}}
123 123" Go to last cursor position ----------------{{{
124" Don't unload a buffer when no longer shown in a window
125" This allows you open a new buffer and leaves current buffer modified
126set hidden
127
128" ":augroup vimStartup | au! | augroup END"
129augroup vimStartup 124augroup vimStartup
130 au! 125 au!
131 " When editing a file, always jump to the last known cursor position. 126 " When editing a file, always jump to the last known cursor position.
@@ -139,6 +134,7 @@ augroup vimStartup
139augroup END 134augroup END
140 135
141" }}} 136" }}}
137" }}}
142" TABSIZE ----------------{{{ 138" TABSIZE ----------------{{{
143 139
144set expandtab 140set expandtab
@@ -178,6 +174,32 @@ set backupdir=~/.vim/tmp
178" backup when write file 174" backup when write file
179set writebackup 175set writebackup
180" }}} 176" }}}
177" HIGHLIGHT ----------------{{{
178syntax enable
179
180function! GetHighlightGroupName()
181 let l:syntaxID = synID(line('.'), col('.'), 1)
182 let l:groupName = synIDattr(l:syntaxID, 'name')
183 echo "Highlight Group Name: " . l:groupName
184endfunction
185
186" Defualt highlight for matched parenthesis is so weird in many colorscheme
187" Why the background color is lighter than my caret !?
188" highlight MatchParen ctermfg=NONE ctermbg=darkgrey cterm=NONE
189highlight LuaParen ctermfg=NONE ctermbg=darkgrey cterm=NONE
190
191" Show trailing spaces
192highlight ExtraWhitespace ctermbg=red guibg=red
193match ExtraWhitespace /\s\+$/
194
195" Persist visualized lines
196" define line highlight color
197highlight MultiLineHighlight ctermbg=LightYellow guibg=LightYellow ctermfg=Black guifg=Black
198" highlight the current line
199nnoremap <silent> <leader>gh :call matchadd('MultiLineHighlight', '\%'.line('.').'l')<CR>
200" clear all the highlighted lines
201nnoremap <silent> <leader>gH :call clearmatches()<CR>
202" }}}
181" MISC ----------------{{{ 203" MISC ----------------{{{
182 204
183" Use Unix way to add newline 205" Use Unix way to add newline
diff --git a/vim/init/config.vim b/vim/init/config.vim
index ec9f090..b34ed81 100644
--- a/vim/init/config.vim
+++ b/vim/init/config.vim
@@ -1,29 +1,40 @@
1"====================================================================== 1"======================================================================
2"
3" init-config.vim - 正常模式下的配置,在 init-basic.vim 后调用 2" init-config.vim - 正常模式下的配置,在 init-basic.vim 后调用
4"
5" Created by skywind on 2018/05/30
6" Last Modified: 2018/05/30 19:20:46
7"
8"====================================================================== 3"======================================================================
9 4
10"---------------------------------------------------------------------- 5" Unnamed Buffer ----------------{{{
11" 6augroup DeleteUnnamedEmptBuffer!
12"----------------------------------------------------------------------
13augroup LeavingBuffer!
14 au BufLeave {} if getline(1, '$') == [''] | setlocal bufhidden=wipe | endif 7 au BufLeave {} if getline(1, '$') == [''] | setlocal bufhidden=wipe | endif
15augroup END 8augroup END
9" }}}
10" TMUX ----------------{{{
16 11
17
18"----------------------------------------------------------------------
19" 有 tmux 何没有的功能键超时(毫秒) 12" 有 tmux 何没有的功能键超时(毫秒)
20"----------------------------------------------------------------------
21if $TMUX != '' 13if $TMUX != ''
22 set ttimeoutlen=30 14 set ttimeoutlen=30
23elseif &ttimeoutlen > 80 || &ttimeoutlen <= 0 15elseif &ttimeoutlen > 80 || &ttimeoutlen <= 0
24 set ttimeoutlen=80 16 set ttimeoutlen=80
25endif 17endif
26 18
19" 防止tmux下vim的背景色显示异常
20" Refer: http://sunaku.github.io/vim-256color-bce.html
21if &term =~ '256color' && $TMUX != ''
22 " disable Background Color Erase (BCE) so that color schemes
23 " render properly when inside 256-color tmux and GNU screen.
24 set t_ut=
25endif
26
27
28" Ref: https://gist.github.com/andersevenrud/015e61af2fd264371032763d4ed965b6
29" You might have to force true color when using regular vim inside tmux as the
30" colorscheme can appear to be grayscale with 'termguicolors' option enabled.
31if !has('gui_running') && &term =~ '^\%(screen\|tmux\)'
32 let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
33 let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
34endif
35
36" }}}
37" KeyCode ----------------{{{
27 38
28"---------------------------------------------------------------------- 39"----------------------------------------------------------------------
29" 终端下允许 ALT,详见:http://www.skywind.me/blog/archives/2021 40" 终端下允许 ALT,详见:http://www.skywind.me/blog/archives/2021
@@ -50,9 +61,8 @@ if has('nvim') == 0 && has('gui_running') == 0
50 endfor 61 endfor
51endif 62endif
52 63
53
54"---------------------------------------------------------------------- 64"----------------------------------------------------------------------
55" 终端下功能键 65" 功能键端码矫正
56"---------------------------------------------------------------------- 66"----------------------------------------------------------------------
57function! s:key_escape(name, code) 67function! s:key_escape(name, code)
58 if has('nvim') == 0 && has('gui_running') == 0 68 if has('nvim') == 0 && has('gui_running') == 0
@@ -60,10 +70,6 @@ function! s:key_escape(name, code)
60 endif 70 endif
61endfunc 71endfunc
62 72
63
64"----------------------------------------------------------------------
65" 功能键终端码矫正
66"----------------------------------------------------------------------
67call s:key_escape('<F1>', 'OP') 73call s:key_escape('<F1>', 'OP')
68call s:key_escape('<F2>', 'OQ') 74call s:key_escape('<F2>', 'OQ')
69call s:key_escape('<F3>', 'OR') 75call s:key_escape('<F3>', 'OR')
@@ -81,55 +87,23 @@ call s:key_escape('<S-F10>', '[21;2~')
81call s:key_escape('<S-F11>', '[23;2~') 87call s:key_escape('<S-F11>', '[23;2~')
82call s:key_escape('<S-F12>', '[24;2~') 88call s:key_escape('<S-F12>', '[24;2~')
83 89
84 90" }}}
85"---------------------------------------------------------------------- 91" Filetype ----------------{{{
86" 防止tmux下vim的背景色显示异常
87" Refer: http://sunaku.github.io/vim-256color-bce.html
88"----------------------------------------------------------------------
89if &term =~ '256color' && $TMUX != ''
90 " disable Background Color Erase (BCE) so that color schemes
91 " render properly when inside 256-color tmux and GNU screen.
92 set t_ut=
93endif
94
95" Ref: https://gist.github.com/andersevenrud/015e61af2fd264371032763d4ed965b6
96" You might have to force true color when using regular vim inside tmux as the
97" colorscheme can appear to be grayscale with 'termguicolors' option enabled.
98if !has('gui_running') && &term =~ '^\%(screen\|tmux\)'
99 let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
100 let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
101endif
102
103
104"----------------------------------------------------------------------
105" 配置微调
106"----------------------------------------------------------------------
107
108" 打开文件时恢复上一次光标所在位置
109autocmd BufReadPost *
110 \ if line("'\"") > 1 && line("'\"") <= line("$") |
111 \ exe "normal! g`\"" |
112 \ endif
113
114" 定义一个 DiffOrig 命令用于查看文件改动
115if !exists(":DiffOrig")
116 command DiffOrig vert new | set bt=nofile | r ++edit # | 0d_ | diffthis
117 \ | wincmd p | diffthis
118endif
119
120
121"----------------------------------------------------------------------
122" 文件类型微调
123"---------------------------------------------------------------------- 92"----------------------------------------------------------------------
124augroup InitFileTypesGroup 93augroup InitFileTypesGroup
125 94
126 " 清除同组的历史 autocommand
127 au! 95 au!
128 96
129 " help page: open in a new tab 97 " Filetype for Vim ----------------{{{
98
99 " Help page
130 autocmd BufEnter *.txt if &filetype == 'help' | wincmd T | endif 100 autocmd BufEnter *.txt if &filetype == 'help' | wincmd T | endif
131 101
132 " shegang: Automatically apply filetype by shebang 102 " quickfix: hide line number
103 autocmd FileType quickfix setlocal nonumber
104
105 " }}}
106 " Shebeng: Set filetype from shebeng ----------------{{{
133 function! s:ApplyShebang() 107 function! s:ApplyShebang()
134 let l:filetype = matchstr(getline(1), '^#!.*[ /]\zs[[:alnum:]]\+$') 108 let l:filetype = matchstr(getline(1), '^#!.*[ /]\zs[[:alnum:]]\+$')
135 let l:shebangMatch = #{ node: "javascript" } 109 let l:shebangMatch = #{ node: "javascript" }
@@ -142,8 +116,31 @@ augroup InitFileTypesGroup
142 endif 116 endif
143 endfunction 117 endfunction
144 autocmd BufReadPost * call <SID>ApplyShebang() 118 autocmd BufReadPost * call <SID>ApplyShebang()
119 " }}}
120 " Markdown ----------------{{{
121 au FileType markdown setlocal wrap
122 au FileType markdown set sw=2 ts=2
123 " Fold by heading level
124 function! MarkdownLevel()
125 let hash_num = matchstr(getline(v:lnum), '^#\+')
126 let hash_num_at_top = matchstr(getline(v:lnum-1), '^#\+')
127 if empty(hash_num)
128 if empty(hash_num_at_top)
129 return "="
130 else
131 return ">"..(len(hash_num_at_top))
132 endif
133 else
134 return len(hash_num)-1
135 endif
136 endfunction
137 au FileType markdown setlocal foldexpr=MarkdownLevel()
138 au FileType markdown setlocal foldmethod=expr
139
140 " }}}
141 " HTML ----------------{{{
145 142
146 " html: Quickly edit html tag class 143 " Usage: <leader>cl(ass) or <leader>id to edit html tag attribute
147 function! s:ChangeAttr(pattern) 144 function! s:ChangeAttr(pattern)
148 let l:attr = matchstr(getline('.'), a:pattern.'="') 145 let l:attr = matchstr(getline('.'), a:pattern.'="')
149 if l:attr == '' 146 if l:attr == ''
@@ -160,29 +157,20 @@ augroup InitFileTypesGroup
160 startinsert 157 startinsert
161 endif 158 endif
162 endfunction 159 endfunction
163 " Edit class and id for javascript files
164 autocmd FileType html,markdown nnoremap <buffer> <leader>cl :call <SID>ChangeAttr("class")<CR> 160 autocmd FileType html,markdown nnoremap <buffer> <leader>cl :call <SID>ChangeAttr("class")<CR>
165 autocmd FileType html,markdown nnoremap <buffer> <leader>id :call <SID>ChangeAttr("id")<CR> 161 autocmd FileType html,markdown nnoremap <buffer> <leader>id :call <SID>ChangeAttr("id")<CR>
166 162
167 " markdown 163 " Reload preview server
168 au FileType markdown setlocal wrap 164 autocmd BufWrite *.html,*.js,*.css call ReloadServer()
169 au FileType markdown set sw=2 ts=2 165 function ReloadServer()
170 " Fold by heading level 166 silent !browser-sync reload &>/dev/null
171 function! MarkdownLevel()
172 let hash_num = matchstr(getline(v:lnum), '^#\+')
173 let hash_num_at_top = matchstr(getline(v:lnum-1), '^#\+')
174 if empty(hash_num)
175 if empty(hash_num_at_top)
176 return "="
177 else
178 return ">"..(len(hash_num_at_top))
179 endif
180 else
181 return len(hash_num)-1
182 endif
183 endfunction 167 endfunction
184 au FileType markdown setlocal foldexpr=MarkdownLevel() 168
185 au FileType markdown setlocal foldmethod=expr 169 " }}}
170 " Mail ----------------{{{
171 autocmd BufRead /tmp/mutt-* set tw=72
172 " }}}
173 " Password ----------------{{{
186 174
187 " Hide the first line of a file if editing password file 175 " Hide the first line of a file if editing password file
188 " TODO a better way to determine a file is related to password-store, now use 176 " TODO a better way to determine a file is related to password-store, now use
@@ -197,6 +185,8 @@ augroup InitFileTypesGroup
197 setlocal foldtext=s:custom() 185 setlocal foldtext=s:custom()
198 norm! ggzfl 186 norm! ggzfl
199 endfunction 187 endfunction
188 " }}}
189 " Beancount ----------------{{{
200 190
201 " Set filetype for beancount 191 " Set filetype for beancount
202 autocmd BufRead,BufNewFile *.bean call PrepareBean() 192 autocmd BufRead,BufNewFile *.bean call PrepareBean()
@@ -206,21 +196,8 @@ augroup InitFileTypesGroup
206 autocmd VimLeave * silent !killall fava 196 autocmd VimLeave * silent !killall fava
207 endfunction 197 endfunction
208 198
209 " Set filetype for index.html 199 " }}}
210 autocmd BufWrite *.html,*.js,*.css call ReloadServer()
211 function ReloadServer()
212 silent !browser-sync reload &>/dev/null
213 endfunction
214
215 " quickfix: hide line number
216 au FileType quickfix setlocal nonumber
217
218 " 强制对某些扩展名的 filetype 进行纠正
219 au BufNewFile,BufRead *.as setlocal filetype=actionscript
220 au BufNewFile,BufRead *.pro setlocal filetype=prolog
221 au BufNewFile,BufRead *.es setlocal filetype=erlang
222 au BufNewFile,BufRead *.asc setlocal filetype=asciidoc
223 au BufNewFile,BufRead *.vl setlocal filetype=verilog
224 au BufRead /tmp/mutt-* set tw=72
225 200
226augroup END 201augroup END
202
203" }}}
diff --git a/vim/init/special_highlight.vim b/vim/init/special_highlight.vim
deleted file mode 100644
index 26d2787..0000000
--- a/vim/init/special_highlight.vim
+++ /dev/null
@@ -1,28 +0,0 @@
1"----------------------------------------------------------------------
2" SYNTAX_HIGHLIGHT
3"----------------------------------------------------------------------
4
5syntax enable
6
7function! GetHighlightGroupName()
8 let l:syntaxID = synID(line('.'), col('.'), 1)
9 let l:groupName = synIDattr(l:syntaxID, 'name')
10 echo "Highlight Group Name: " . l:groupName
11endfunction
12
13" Defualt highlight for matched parenthesis is so weird in many colorscheme
14" Why the background color is lighter than my caret !?
15" highlight MatchParen ctermfg=NONE ctermbg=darkgrey cterm=NONE
16highlight LuaParen ctermfg=NONE ctermbg=darkgrey cterm=NONE
17
18" Show trailing spaces
19highlight ExtraWhitespace ctermbg=red guibg=red
20match ExtraWhitespace /\s\+$/
21
22" Persist visualized lines
23" define line highlight color
24highlight MultiLineHighlight ctermbg=LightYellow guibg=LightYellow ctermfg=Black guifg=Black
25" highlight the current line
26nnoremap <silent> <leader>gh :call matchadd('MultiLineHighlight', '\%'.line('.').'l')<CR>
27" clear all the highlighted lines
28nnoremap <silent> <leader>gH :call clearmatches()<CR>