[小冬传经验]-终端ZSH配置,打造最强文本编辑器VIM

终端利器,让敲代码变得顺心如意

Posted by Lordon on March 26, 2020

终端ZSH配置

bash作为*nix类系统必备终端已经多年,但是zsh还没有被作为主流使用起来,不过zsh凭借它的高度定制化与优美的洁面受到了不少程序员的广泛好评 在这里我也表达一下我对zsh的喜爱~
言归正传,搜索关键内容:oh my zsh 配置完成后效果如下所示

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
此处分享一下zsh配置代码

""""""""""""""""""""""
" Author:Lordon 
" Blog: http://LordonCN.github.io
" Version: v2.2
" Update Time: 2020-05-05
" Details:change vim8.2
""""""""""""""""""""""
export ZSH=$HOME/.oh-my-zsh
#source "$HOME/.vim/plugged/gruvbox/gruvbox_256palette_osx.sh" 
export LC_ALL=en_US.UTF-8
# every time rl auto clear the window
clear
# welcome
echo -e "Hello, `whoami`  | Today is `date`
 __    _____  ____  ____  _____  _  _ 
(  )  (  _  )(  _ \(  _ \(  _  )( \( )
 )(__  )(_)(  )   / )(_) ))(_)(  )  ( 
(____)(_____)(_)\_)(____/(_____)(_)\_)

           ^__^
           (oo)_______
           (__)       )--/
              ||----w||
              ||     ||

If you want to grow, you need to suffer more :)"

ZSH_THEME="af-magic"
plugins=(
git
Z
zsh-syntax-highlighting
zsh-autosuggestions
)

source $ZSH/oh-my-zsh.sh
#""""""""""""""""""""""
#" alias settings
#""""""""""""""""""""""

alias cdgithub='cd /Volumes/MAC_软件/MY_Projects_VSCode/dong.github.io/'
# safe
# alias rm='rm -i'
move_to_trash () {
    mv "$@" /Volumes/MAC_软件/Trash_for_rm/
}
alias rm='move_to_trash'
# sleep and shutdown easy to use
alias sleep='pmset sleepnow' 
alias restart='sudo shutdown -r now'
alias shutdown='sudo shutdown -h now'
# source ~/.zshrc
alias rl="source ~/.zshrc"
alias cp="cp -i"
# open dash 
alias dash='open dash://'
# change vim8.2 installed by homebrew
alias vim8='/usr/local/Cellar/vim/8.2.0400/bin/vim'
alias vim='/usr/local/Cellar/neovim/0.4.3/bin/nvim'


export PATH="/usr/local/opt/qt/bin:$PATH"

那么为什么要用vim

去年暑假因为实习的原因在公司接触过vim,虽然只是简单的改改参数用,还是对这个装b利器有点好奇,但是把自己从vscode的舒适区中拽出来给自己找这么个麻烦确实挺累人的。

  • 人总要在适当的时候做出一点改变。

最近几天老王催得紧,想搞的论文内容也没有头绪,逛知乎的时候🈶️看到熟悉的vim专栏,看到大佬们还是在不断地分享好用的vim插件,看着他们行云流水般的操作超级帅的样子👀= = 我决定跳出舒适圈好好学一学。

初步改变

一上来安装一堆插件肯定是不现实的,加上学校服务器连不上网,现在从本地上传文件速度慢的感人,决定只是配置下文本编辑部分即可。那么这样的话主要配置内容就变成了左侧目录部分以及颜色快捷键

配置完成后效果如下所示,再也不用打开一个再保存关闭打开另一个文件了呢…

完全改变

一开始是比较不太倾向于在mac上配置太复杂东西的,所以只是使用了vim自带的功能来配置VIM, 但是吧。。。在知乎逛久了发现VIM作为一款强大的工具不好好利用插件功能肯定是一大损失= = 所以跟着教程配置了一下Plug之后又打开了新世界的大门,奇怪的知识增加了~
说实话,这东西完全就是熟能生巧,快捷指令确实多,按照自己的习惯配置好常用的功能键之后使用起来如鱼得水, 同时将页面设置调整一下变得色彩斑斓看起来更加赏心悦目不是?一个月之后设置了一堆个性化配置,现在用起来 顺手得很:P

插件安装

在知乎逛久了看了不少vim经验贴,在大佬们的怂恿下开始用vim-plug装插件了。。这东西跟换装游戏一样开始了就一发不可收拾/笑
这是近一周来整理的适配我的MAC的插件,对于我目前使用来说已经用着很舒服了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
""""""""""""""""""""""
" -----0527----Plug box-------------
""""""""""""""""""""""
call plug#begin('~/.vim/plugged')
Plug 'preservim/nerdtree' 						"左侧目录树
Plug 'mhinz/vim-startify' 						"welcome
Plug 'tpope/vim-fugitive' 						"Git 插件
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
Plug 'morhetz/gruvbox'							"当前主题很喜欢
Plug 'luochen1990/rainbow'  					"括号不同颜色
Plug 'dense-analysis/ale' 						"语法检查
Plug 'Yggdroot/LeaderF', {'do': './install.sh'} "模糊搜索
Plug 'nathanaelkane/vim-indent-guides' 			"智能缩进插件 
Plug 'skywind3000/asyncrun.vim' 				"F5运行当前程序
Plug 'itchyny/vim-cursorword' 					"检测同名单词用下划线标出
Plug 'skywind3000/vim-quickui'					"添加ui 两下空格键 vim8.2
Plug 'neoclide/coc.nvim', {'branch': 'release'} "补全指令插件 移步好用
Plug 'iamcco/markdown-preview.vim' 				"markdown prewiew ontime
call plug#end()

简单展示一下当前工作环境:

linux端同步

目前特别喜欢MAC的原因是因为他的类unix的终端操作.
因为在linux习惯了win+num切换应用的快捷键在MAC不太适应之外其他都还用的很舒服. 我想这也是我在MAC配置完环境之后还要切换到linux更新下配置的原因吧(虽然主要是更改vim的配置文件~).


vim的配置文件只需要拿过来.vimrc 以及.vim/ 直接覆盖即可,可真是良心.

分享一下自己的配置文件

My MAC VIM自定义配置

vim ~/.vimrc

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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
""""""""""""""""""""""
" Author:Lordon 
" Blog: http://LordonCN.github.io
" Version: v4.1
" Update Time: 2020-07-05
" Details: change the size of table 
""""""""""""""""""""""
"🌟设置leader按键为空格
let mapleader = "\<space>"
set visualbell
set autoread
	
nmap <leader>s :Startify<cr>
nmap <leader>w :w!<cr>
nmap <leader>q :q!<cr> 
" past the copy content in 0 regester
nmap 0p "0p
""""""""""""""""""""""
"💻copy content to systerm copyboard
" command: "+yw 或者 
"visual选中"+y 
""""""""""""""""""""""
vmap -= "+y
"🌟ve(visual select word) It's to difficult to reach
"nmap <leader>m ve
" delete one word --learn from book

"🌟go to tag
nmap <leader>g <C-]>

"🌟change buffer tabe
nmap <leader>n :bn<cr> 

"🌟打开目录树 y定位到当前目录
nmap <leader>t :NERDTreeToggle<cr>
nnoremap <silent> <Leader>y :NERDTreeFind<CR>

"🌟coc bookmark config
nmap <leader>b :CocList bookmark<cr>

"开ui导航 
nnoremap <silent><space>r :call quickui#menu#open()<cr>
"🌟exit  insert model to normal use jj 
imap jj <Esc>`^
"快捷生成TODO change todo and to insert mode
"imap <C-t> <!--TODO--> jj7hdwi
imap <C-t>m <!--TODO(Lordon):--> jj3hi
imap <C-t>p # TODO(Lordon):
""""""""""""""""""""""
" 设置vim scheme
""""""""""""""""""""""
" this next line is needed to enable your custom colors:
colorscheme gruvbox
set background=dark
syntax enable
syntax on
" no swap
set noswapfile
set autoread
"搜索高亮
set hlsearch
"设置行号
set number
set relativenumber
"设置右下角光标位置
set ruler
"搜索所有相关
set incsearch
"不区分大小写搜索
set ignorecase
"显示出输入的命令
set showcmd 
set encoding=utf-8
"当前行加下标
"set cursorline 
"使用鼠标功能
set mouse=a
set mousehide 
"设置命令保存历史
set history=100
"命令行高度加1
set cmdheight=1 
"vim命令模式tab预选提示
set wildmenu
"禁止折行
set nowrap 

filetype indent on

set list lcs=tab:>-
"for cpp
set tabstop=4
"change tab length 
set shiftwidth=4

set t_Co=256
set autochdir
autocmd BufEnter * silent! lcd %:p:h

" 匹配成对括号
inoremap ( ()<LEFT>
inoremap { {}<LEFT>
inoremap [ []<LEFT>

""""""""""""""""""""""
" ---------Plug box-------------
""""""""""""""""""""""
call plug#begin('~/.vim/plugged')
Plug 'preservim/nerdtree' 						"左侧目录树
Plug 'mhinz/vim-startify' 						"welcome
Plug 'tpope/vim-fugitive' 						"Git 插件
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
Plug 'voldikss/vim-floaterm'					"floating window
Plug 'morhetz/gruvbox'							"当前主题很喜欢
Plug 'luochen1990/rainbow'  					"括号不同颜色
" Plug 'dense-analysis/ale' 						"语法检查
Plug 'Yggdroot/LeaderF', {'do': './install.sh'} "模糊搜索
Plug 'nathanaelkane/vim-indent-guides' 			"智能缩进插件 
Plug 'skywind3000/asyncrun.vim' 				"F5运行当前程序
Plug 'itchyny/vim-cursorword' 					"检测同名单词用下划线标出
Plug 'skywind3000/vim-quickui'					"添加ui 两下空格键 vim8.2
Plug 'neoclide/coc.nvim', {'branch': 'release'} "补全指令插件 移步好用
Plug 'iamcco/markdown-preview.vim' 				"markdown prewiew ontime
call plug#end()


let airline#extensions#coc#error_symbol = 'Error:'
let airline#extensions#coc#warning_symbol = 'Warning:'
let g:airline#extensions#tabline#enabled = 1

""""""""""""""""""""""
" Show the name
""""""""""""""""""""""
function! AccentDemo()
  let keys = ['N','E','U','L','o','r','d','o','n']
  for k in keys
    call airline#parts#define_text(k, k)
  endfor
  call airline#parts#define_accent('N', 'bold')
  call airline#parts#define_accent('E', 'green')
  call airline#parts#define_accent('U', 'blue')
  call airline#parts#define_accent('L', 'purple')
  call airline#parts#define_accent('o', 'orange')
  call airline#parts#define_accent('r', 'purple')
  call airline#parts#define_accent('d', 'bold')
  call airline#parts#define_accent('o', 'green')
  call airline#parts#define_accent('n', 'italic')
  let g:airline_section_a = airline#section#create(keys)
endfunction
autocmd VimEnter * call AccentDemo()


""""""""""""""""""""""
" Plug----leaderF
""""""""""""""""""""""
nnoremap <leader>h vy:Leaderf rg -e /Users/Lordon/ path<CR>
let g:Lf_ReverseOrder = 1
let g:Lf_ShortcutF = '<C-P>'
let g:Lf_WorkingDirectoryMode = 'Ac'
"let g:Lf_WindowPosition = 'right'
let g:Lf_WindowPosition = 'popup'
let g:Lf_PreviewInPopup = 1
let g:Lf_StlSeparator = { 'left': "\ue0b0", 'right': "\ue0b2", 'font': "DejaVu Sans Mono for Powerline" }
let g:Lf_PreviewResult = {'Function': 0, 'BufTag': 0 }

let g:Lf_PreviewHorizontalPosition = 'left'
" Show icons, icons are shown by default
let g:Lf_ShowDevIcons = 1
let g:Lf_HideHelp = 1
let g:Lf_UseCache = 0
let g:Lf_UseVersionControlTool = 0
let g:Lf_IgnoreCurrentBufferName = 1
" let g:Lf_WorkingDirectoryMode = 1

""""""""""""""""""""""
" Plug----nerdtree
" nerdtree settings leader+f :search for files
""""""""""""""""""""""
nnoremap <leader>f :nerdtreefind<cr>
let nerdtreewinsize=30
let g:NERDTreeChDirMode = 2

""""""""""""""""""""""
" Plug-----缩进可视化插件 Indent Guides
" 从第二层开始可视化显示缩进
""""""""""""""""""""""
let g:indent_guides_start_level=2
" 色块宽度
let g:indent_guides_guide_size=1
" 快捷键 i 开/关缩进可视化
nmap <silent> <Leader>i <Plug>IndentGuidesToggle


""""""""""""""""""""""
"Plug asyncrun
"Quickly Run
""""""""""""""""""""""
" 任务结束时候响铃提醒
let g:asyncrun_bell = 1



"F5运行当前程序
map <F5> :call CompileRun()<CR>
    func! CompileRun()
	exec "w"
	if &filetype == 'sh'
        "    :!clear && time bash %
		exec "AsyncRun bash %"
		exec "copen"
	elseif &filetype == 'python'
		exec "AsyncRun -raw python %"
		exec "copen"
		"编译当前文件
	elseif &filetype == 'cpp'
		exec "!g++-8 % -o %<"
		exec "!./%<"

	endif
	endfunc


" source的时候这段会报错 不过没关系 
filetype plugin indent on 
"新建.c,.h,.sh,.java文件,自动插入文件头 
autocmd BufNewFile *.cpp,*.sh exec ":call SetTitle_()" 
""定义函数SetTitle,自动插入文件头 
func SetTitle_() 
    "如果文件类型为.sh文件 
    if &filetype == 'sh' 
        call setline(1,"\#########################################################################") 
        call append(line("."), "\# File Name: ".expand("%")) 
        call append(line(".")+1, "\# Author: Lordon") 
        call append(line(".")+2, "\# mail: xunjtech@gmail.com") 
        call append(line(".")+3, "\# Created Time: ".strftime("%c")) 
        call append(line(".")+4, "\#########################################################################") 
        call append(line(".")+6, "") 
    endif
    if &filetype == 'cpp'
        call setline(1, "/*************************************************************************")  
        call setline(2, "\ @Author: lordon")  
        call setline(3, "\ @Created Time : ".strftime("%c"))  
        call setline(4, "\ @Description:")  
        call setline(5, " ************************************************************************/")  
        call setline(6,"")  
        call setline(7, "#include<iostream>")
        call setline(8, "#include<cmath>")
        call setline(9, "#include<vector>")
        call setline(10,"#include<string>")
        call setline(11,"#include<algorithm>")
        call setline(12,"")  
        call setline(13,"using namespace std;")
        call setline(14,"")  
        call setline(15,"int main(){")  
        call setline(16,"")  
        call setline(17,"return 0;")  
        call setline(18,"}")  
    endif
   "新建文件后,自动定位到文件末尾
   autocmd BufNewFile * normal G
endfunc 



""""""""""""""""""""""
"📖remote PC need to delete this part
""""""""""""""""""""""

call quickui#menu#reset()
let g:quickui_color_scheme = 'gruvbox'
call quickui#menu#install("&File", [
			\ [ "&Open/NewFile", 'call feedkeys(":tabe ")'],
			\ [ "&Save\t(:w)", 'write'],
		\ [ "--", ],
			\ [ "&New Bookmark", 'CocCommand bookmark.annotate'],
			\ [ "&List Bookmark", 'CocList bookmark'],
			\ ['&Spell %{&spell? "Disable":"Enable"}', 'set spell!', 'Toggle spell check %{&spell? "off" : "on"}'],
			\ [ "&Creat Tag", '!ctags -R'],
		\ [ "--", ],
			\ [ "LeadF &File", 'Leaderf file', 'Open file with leaderf'],
			\ [ "LeadF &Mru", 'Leaderf mru --regexMode', 'Open recently accessed files'],
			\ [ "LeadF &Buffer", 'Leaderf buffer', 'List current buffers in leaderf'],
			\ [ "--", ],
			\ [ "E&xit", 'qa' ],
			\ ])

call quickui#menu#install('&Tools', [
			\ ['List &Buffer', 'call quickui#tools#list_buffer("tabe")', ],
			\ ['New &Buffer', 'call quickui#tools#buffer_switch("tabe")', ],
			\ ['--',''],
			\ ['Show &Gitcommit', 'CocCommand git.showCommit', ],
			\ ['Display &Messages', 'call quickui#tools#display_messages()', ],
			\ ['--',''],
			\ ["Relati&ve number %{&relativenumber? 'OFF':'ON'}", 'set relativenumber!'],
			\ ])

call quickui#menu#install('&Plugin', [
			\ ["&NERDTree\t<space>t", 'NERDTreeToggle', 'toggle nerdtree'],
			\ ["-"],
			\ ['&Source vimrc','source ~/.vimrc'],
			\ ['&PlugInstall','PlugInstall'],
			\ ["-"],
			\ ["Plugin &Update", "PlugUpdate", "Update plugin"],
			\ ])

call quickui#menu#install('Help (&?)', [
			\ ["&Index", 'tab help index', ''],
			\ ['Ti&ps', 'tab help tips', ''],
			\ ['--',''],
			\ ["&Tutorial", 'tab help tutor', ''],
			\ ['&Quick Reference', 'tab help quickref', ''],
			\ ['&Summary', 'tab help summary', ''],
			\ ['--',''],
			\ ['&Vim Script', 'tab help eval', ''],
			\ ['&Function List', 'tab help function-list', ''],
			\ ['&Dash Help', 'call asclib#utils#dash_ft(&ft, expand("<cword>"))'],
			\ ], 10000)
"----------------------------------------------------------------------
" context menu
"----------------------------------------------------------------------
let g:context_menu_k = [
			\ ["&Peek Definition\tAlt+;", 'call quickui#tools#preview_tag("")'],
			\ ["S&earch in Project\t\\cx", 'exec "silent! GrepCode! " . expand("<cword>")'],
			\ [ "--", ],
			\ [ "Find &Definition\t\\cg", 'call MenuHelp_Fscope("g")', 'GNU Global search g'],
			\ [ "Find &Symbol\t\\cs", 'call MenuHelp_Fscope("s")', 'GNU Gloal search s'],
			\ [ "Find &Called by\t\\cd", 'call MenuHelp_Fscope("d")', 'GNU Global search d'],
			\ [ "Find C&alling\t\\cc", 'call MenuHelp_Fscope("c")', 'GNU Global search c'],
			\ [ "Find &From Ctags\t\\cz", 'call MenuHelp_Fscope("z")', 'GNU Global search c'],
			\ [ "--", ],
			\ [ "Goto D&efinition\t(YCM)", 'YcmCompleter GoToDefinitionElseDeclaration'],
			\ [ "Goto &References\t(YCM)", 'YcmCompleter GoToReferences'],
			\ [ "Get D&oc\t(YCM)", 'YcmCompleter GetDoc'],
			\ [ "Get &Type\t(YCM)", 'YcmCompleter GetTypeImprecise'],
			\ [ "--", ],
			\ ['Dash &Help', 'call asclib#utils#dash_ft(&ft, expand("<cword>"))'],
			\ ['Cpp&man', 'exec "Cppman " . expand("<cword>")', '', 'c,cpp'],
			\ ['P&ython Doc', 'call quickui#tools#python_help("")', 'python'],
			\ ]

if has('nvim')
" display vim messages in the textbox
	function! DisplayMessages()
		let x = ''
		redir => x
		silent! messages
		redir END
		let x = substitute(x, '[\n\r]\+\%$', '', 'g')
		let content = filter(split(x, "\n"), 'v:key != ""')
		let opts = {"close":"button", "title":"Vim Messages"}
		call quickui#textbox#open(content, opts)
	endfunc
endif


"智能缩进
set smartindent
set foldmethod=indent



""""""""""""""""""""""
"https://github.com/voldikss/vim-floaterm#basic-usage
" Configuration of floaterm 
" F7-start a new terminal F8-put the terminal to background
""""""""""""""""""""""
let g:floaterm_keymap_new    = '<F7>'
let g:floaterm_keymap_toggle = '<F8>'