-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdot_vimrc.tmpl
More file actions
334 lines (273 loc) · 11.6 KB
/
Copy pathdot_vimrc.tmpl
File metadata and controls
334 lines (273 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
" 第一次使用,請先安裝go,然後再下以下該行指令
" brew install vim cmake python go nodejs java
" sudo ln -sfn $(brew --prefix java)/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
" curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
" vim +PlugInstall +qall
"
" winget install -e GoLang.Go OpenJS.NodeJS
" mkdir -p vimfiles\autoload
" Invoke-WebRequest -Uri "https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim" -OutFile "vimfiles\autoload\plug.vim"
" =============================================================================
"vim-scripts/php.vim-html-enhanced' 檔案編碼
set encoding=utf-8
set fileencodings=utf-8,cp950
set enc=utf8
" 佈景主題
syntax on " 語法上色
set synmaxcol=0
" 界面顯示
set mouse=a " 啟用滑鼠
set confirm " 操作過程有衝突時,以明確的文字來詢問
"set ruler " 顯示右下角設定值
set cursorline " 顯示目前的游標位置
set cursorcolumn " 顯示目前的游標位置
set number " 顯示行號
set colorcolumn=80 " 顯示編輯器建議寬度
set scrolloff=3 " 捲動時保留n行彈性
set laststatus=2
" SSH / TMUX 剪貼簿共享 (OSC 52)
let s:is_ssh = !empty($SSH_CLIENT) || !empty($SSH_TTY) || !empty($SSH_CONNECTION)
if s:is_ssh
" 純 Vimscript Base64 編碼器(高度相容,無外部依賴,相容 Vim 7.0+)
function! s:base64_encode(str) abort
let l:b = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
let l:out = []
let l:len = strlen(a:str)
let l:padding = (3 - l:len % 3) % 3
let l:s = a:str . repeat("\<Nul>", l:padding)
let l:i = 0
let l:slen = strlen(l:s)
while l:i < l:slen
let l:v1 = char2nr(strpart(l:s, l:i, 1))
let l:v2 = char2nr(strpart(l:s, l:i+1, 1))
let l:v3 = char2nr(strpart(l:s, l:i+2, 1))
let l:n = l:v1 * 65536 + l:v2 * 256 + l:v3
let l:n1 = (l:n / 262144) % 64
let l:n2 = (l:n / 4096) % 64
let l:n3 = (l:n / 64) % 64
let l:n4 = l:n % 64
call add(l:out, strpart(l:b, l:n1, 1))
call add(l:out, strpart(l:b, l:n2, 1))
call add(l:out, strpart(l:b, l:n3, 1))
call add(l:out, strpart(l:b, l:n4, 1))
let l:i += 3
endwhile
let l:res = join(l:out, '')
if l:padding > 0
let l:res = strpart(l:res, 0, strlen(l:res) - l:padding) . repeat('=', l:padding)
endif
return l:res
endfunction
" 快速編碼與降級偵測
function! s:get_base64(str) abort
if executable('base64')
let l:b64 = system('echo -n ' . shellescape(a:str) . ' | base64 | tr -d "\n"')
if v:shell_error == 0 && strlen(l:b64) > 0
return l:b64
endif
endif
return s:base64_encode(a:str)
endfunction
" 輸出 OSC 52 逸出碼到 /dev/tty (繞過 Vim 內部快取)
function! s:osc52_copy(str) abort
let l:b64 = s:get_base64(a:str)
let l:osc = "\<Esc>]52;c;" . l:b64 . "\007"
if !empty($TMUX)
let l:osc = "\<Esc>Ptmux;\<Esc>" . substitute(l:osc, "\<Esc>", "\<Esc>\<Esc>", "g") . "\<Esc>\\"
endif
try
call writefile([l:osc], '/dev/tty', 'b')
catch
" 如果 /dev/tty 不可寫,嘗試使用 echoraw (Vim 8.2+)
if exists('*echoraw')
call echoraw(l:osc)
endif
endtry
endfunction
" 監聽複製事件 (Vim 8.0.1394+ 支援 TextYankPost)
if exists('##TextYankPost')
function! s:on_yank() abort
let l:event = v:event
if l:event.regname == '' || l:event.regname == '+' || l:event.regname == '*'
let l:text = join(l:event.regcontents, "\n")
call s:osc52_copy(l:text)
endif
endfunction
augroup VimSSHOSC52Copy
autocmd!
autocmd TextYankPost * call s:on_yank()
augroup END
endif
else
" 使用作業系統剪貼簿
set clipboard=unnamedplus " 共用系統剪貼簿 (Wayland有額外用套件處理,請見下方plug)
endif
"set statusline=%4*%<\%m%<[%f\%r%h%w]\ [%{&ff},%{&fileencoding},%Y]%=\[Position=%l,%v,%p%%]
" 縮排設定
set smartindent
set shiftwidth=4 " 設定縮排寬度
set tabstop=4 " tab 的字元數
set softtabstop=4
set expandtab " 用 space 代替 tab
" 編輯設定
set backspace=2 " 在 insert 也可用 backspace
set history=1000 " 保留 1000 個使用過的指令
" 搜尋設定
set ic " 搜尋不分大小寫。
set hlsearch " 設定高亮度顯示搜尋結果
set incsearch " 在關鍵字還沒完全輸入完畢前就顯示結果
" Ctrl+J Ctrl+k 整行移動
nnoremap <C-j> :m .+1<CR>==
nnoremap <C-k> :m .-2<CR>==
inoremap <C-j> <Esc>:m .+1<CR>==gi
inoremap <C-k> <Esc>:m .-2<CR>==gi
vnoremap <C-j> :m '>+1<CR>gv=gv
vnoremap <C-k> :m '<-2<CR>gv=gv
nnoremap <A-j> :m .+1<CR>==
nnoremap <A-k> :m .-2<CR>==
inoremap <A-j> <Esc>:m .+1<CR>==gi
inoremap <A-k> <Esc>:m .-2<CR>==gi
vnoremap <A-j> :m '>+1<CR>gv=gv
vnoremap <A-k> :m '<-2<CR>gv=gv
" 啟用Tab縮牌
nmap <TAB> v>
nmap <S-TAB> v<
vmap <TAB> >gv
vmap <S-TAB> <gv
" 透過v選取整行縮排後,不取消選取整行
vnoremap < <gv
vnoremap > >gv
" 顯示行尾
set listchars=eol:¬,tab:→→,trail:.,extends:>,precedes:<
set list
" =============================================================================
" 安裝指令:vim +PlugInstall +qall
call plug#begin()
Plug 'jasonccox/vim-wayland-clipboard'
Plug 'vim-airline/vim-airline'
Plug 'joshdick/onedark.vim'
" On-demand loading: loaded when the specified command is executed
Plug 'preservim/nerdtree', { 'on': 'NERDTreeToggle' }
Plug 'ryanoasis/vim-devicons'
Plug 'bryanmylee/vim-colorscheme-icons'
Plug 'ctrlpvim/ctrlp.vim' " 模糊搜尋 Ctrl+P
{{ if .enableYcm }}
" YCM自動補齊
function! BuildYCM(info)
" info is a dictionary with 3 fields
" - name: name of the plugin
" - status: 'installed', 'updated', or 'unchanged'
" - force: set on PlugInstall! or PlugUpdate!
if a:info.status == 'installed' || a:info.force
!./install.py
endif
endfunction
Plug 'Valloric/YouCompleteMe', { 'do': './install.py --all' }
Plug 'ervandew/supertab'
" Track the engine.
Plug 'SirVer/ultisnips'
" Snippets are separated from the engine. Add this if you want them:
Plug 'honza/vim-snippets'
" Trigger configuration. You need to change this to something other than <tab> if you use one of the following:
" - https://github.com/Valloric/YouCompleteMe
" - https://github.com/nvim-lua/completion-nvim
" https://stackoverflow.com/questions/14896327/ultisnips-and-youcompleteme
" make YCM compatible with UltiSnips (using supertab)
let g:ycm_key_list_select_completion = ['<C-n>', '<Down>']
let g:ycm_key_list_previous_completion = ['<C-p>', '<Up>']
let g:SuperTabDefaultCompletionType = '<C-n>'
" better key bindings for UltiSnipsExpandTrigger
let g:UltiSnipsExpandTrigger = "<tab>"
let g:UltiSnipsJumpForwardTrigger = "<tab>"
let g:UltiSnipsJumpBackwardTrigger = "<s-tab>"
Plug 'mattn/emmet-vim' "Ctrl+Y ,
{{ end }}
Plug 'jiangmiao/auto-pairs' " 自動補全對稱符
" 這是自訂括號的寫法
au FileType ejs let b:AutoPairs = AutoPairsDefine({'<%': '%>', '<!--': '-->'})
au FileType html let b:AutoPairs = AutoPairsDefine({'<!--': '-->'})
" <F8> 看看你設定了哪些變數、函數,也可以快速跳轉
Plug 'majutsushi/tagbar'
nmap <F8> :TagbarToggle<CR>
Plug 'editorconfig/editorconfig-vim'
" 按 <F6> 可以回朔到開啟檔案以來的任何歷史,還會標出修改的地方,很酷
Plug 'mbbill/undotree'
nnoremap <F6> :UndotreeToggle<CR>
" 可以在文档中显示 git 信息
Plug 'airblade/vim-gitgutter'
" 終端機
Plug 'pakutoma/toggle-terminal'
let g:toggle_terminal#command = 'zsh'
tnoremap <silent> <C-@> <C-w>:ToggleTerminal<CR>
nnoremap <silent> <C-@> :ToggleTerminal<CR>
let g:toggle_terminal#position = 'rightbelow'
" Ranger檔案總管
if !has("gui_running") "在GVim無法使用,直接排除
Plug 'rafaqz/ranger.vim'
nnoremap <leader>fr :FloatermNew --height=0.9 --width=0.9 --title=Ranger ranger %:p:h<CR>
endif
Plug 'voldikss/vim-floaterm'
nnoremap <leader>lg :FloatermNew --height=0.9 --width=0.9 --title=Lazygit lazygit<CR>
call plug#end()
" =============================================================================
" Theme
"Use 24-bit (true-color) mode in Vim/Neovim when outside tmux.
"If you're using tmux version 2.2 or later, you can remove the outermost $TMUX check and use tmux's 24-bit color support
"(see < http://sunaku.github.io/tmux-24bit-color.html#usage > for more information.)
if (empty($TMUX))
if (has("nvim"))
"For Neovim 0.1.3 and 0.1.4 < https://github.com/neovim/neovim/pull/2198 >
let $NVIM_TUI_ENABLE_TRUE_COLOR=1
endif
"For Neovim > 0.1.5 and Vim > patch 7.4.1799 < https://github.com/vim/vim/commit/61be73bb0f965a895bfb064ea3e55476ac175162 >
"Based on Vim patch 7.4.1770 (`guicolors` option) < https://github.com/vim/vim/commit/8a633e3427b47286869aa4b96f2bfc1fe65b25cd >
" < https://github.com/neovim/neovim/wiki/Following-HEAD#20160511 >
if (has("termguicolors"))
set termguicolors
endif
endif
syntax on
" 加入這段在 .vimrc 中,避免色彩主題未安裝導致錯誤
if (empty(glob("~/.vim/plugged/onedark.vim")) == 0)
colorscheme onedark
endif
let g:airline_theme='onedark'
let g:airline_powerline_fonts = 1
let g:airline#extensions#tabline#enabled = 1
" -----------------------------------------------------------------------------
" NERDTree
nnoremap <C-t> :NERDTreeToggle<CR>
nmap <F9> :NERDTreeToggle<CR>
function! s:MaybeStartNERDTree()
if winheight(0) > 20 && winwidth(0) > 140
" 啟動時自動開啟 NERDTree,且只在沒有指定檔案時開啟
autocmd VimEnter * NERDTree | wincmd p
"autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif
" 如果關掉最後一個 buffer,並且只剩下 NERDTree,則關閉 Vim
autocmd BufEnter * if winnr('$') == 1 && getbufvar('%', '&filetype') == 'nerdtree' | quit | endif
let NERDTreeShowHidden=1
"let g:nerdtree_tabs_open_on_console_startup=1
let NERDTreeQuitOnOpen=0
"" 沒有文件開啟的時候關閉nerdtree
autocmd QuitPre * if empty(&bt) | lclose | endif
"" Start NERDTree
"autocmd VimEnter * NERDTree
"" Go to previous (last accessed) window.
"autocmd VimEnter * wincmd p
"autocmd StdinReadPre * let s:std_in=1
"autocmd VimEnter * NERDTree | if argc() > 0 || exists("s:std_in") | wincmd p | endif
"autocmd BufEnter * if tabpagenr('$') == 1 && winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() | quit | endif
" Start NERDTree and put the cursor back in the other window.
autocmd VimEnter * NERDTree | wincmd p
" Exit Vim if NERDTree is the only window remaining in the only tab.
autocmd BufEnter * if tabpagenr('$') == 1 && winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() | quit | endif
" If another buffer tries to replace NERDTree, put it in the other window, and bring back NERDTree.
autocmd BufEnter * if winnr() == winnr('h') && bufname('#') =~ 'NERD_tree_\d\+' && bufname('%') !~ 'NERD_tree_\d\+' && winnr('$') > 1 |
\ let buf=bufnr() | buffer# | execute "normal! \<C-W>w" | execute 'buffer'.buf | endif
" Mirror the NERDTree before showing it. This makes it the same on all tabs.
nnoremap <C-n> :NERDTreeMirror<CR>:NERDTreeFocus<CR>
endif
endfunction
" 自動執行
autocmd VimEnter * call s:MaybeStartNERDTree()