aboutsummaryrefslogtreecommitdiffhomepage
path: root/vim/init/config.vim
diff options
context:
space:
mode:
Diffstat (limited to 'vim/init/config.vim')
-rw-r--r--vim/init/config.vim169
1 files changed, 73 insertions, 96 deletions
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" }}}