|
Vim 是一个上古神器,本篇文章主要持续总结使用 Vim 的过程中不得不了解的一些指令和注意事项,以及持续分享一个前端工作者不得不安装的一些插件,而关于 Vim 的简介,主题的选择,以及为何使用 vim-plug 来管理插件等内容,有兴趣的同学下来可以自己了解和对比下。 本文导航◈ 安装03%◈ 新手指南04%◈ 移动光标05%◈ 退出08%◈ 删除08%◈ 修改10%◈ 撤销11%◈ 复制粘贴剪切12%◈ 状态13%◈ 查找13%◈ 替换15%◈ 折叠16%◈ 执行外部命令17%◈ .vimrc18%◈ 基本配置23%◈ 取消备份23%◈ 文件编码23%◈ 显示行号24%◈ 取消换行24%◈ 显示光标当前位置25%◈ 设置缩进25%◈ 突出显示当前行26%◈ 查找27%◈ 左下角显示当前 vim 模式27%◈ 代码折叠28%◈ 主题29%◈ 插件配置32%◈ 树形目录32%◈ 代码,引号,路径补全46%◈ 语法高亮,检查50%◈ 文件,代码搜索56%◈ 加强版状态栏59%◈ 代码注释63%◈ git69%◈ Markdown71%◈ Emmet74%◈ html 576%◈ css 377%◈ JavaScipt82%◈ React91%◈ Prettier93%◈ 总结97%安装 sudo apt-getinstall vim// Ubuntu 其他平台,可以自行谷歌。 vimtutor// vim 教程 上面是史上最简单,最全面的Vim基础教程,至今无人超越。 下面是作者基于上面的归纳: * m) _5 L; _4 ^9 N, }' t+ B 移动光标# hjkl/ K# ?# `0 F- S- x # 2w 向前移动两个单词 # 3e 向前移动到第 3 个单词的末尾: C' A1 Z8 ^6 Y) F # 0 移动到行首( G! D: g2 [0 i! J0 S+ g* ^ # $ 当前行的末尾1 t5 m0 t0 _! q0 i9 K5 Y/ R # gg 文件第一行 # G 文件最后一行1 p Z+ W/ O+ y1 |3 \, X. R # 行号+G 指定行 # <ctrl>+o 跳转回之前的位置9 b8 U8 C3 L+ `) u # <ctrl>+i 返回跳转之前的位置& \0 ~$ {: P0 k+ ` { 退出 # <esc> 进入正常模式; o. x3 n: H( s0 I # :q! 不保存退出 # :wq 保存后退出 * }4 h! P+ P9 A2 j5 u 删除 # x 删除当前字符 # dw 删除至当前单词末尾 # de 删除至当前单词末尾,包括当前字符 # d$ 删除至当前行尾9 O7 A( p* W3 I0 m # dd 删除整行4 y/ m9 y( \7 C! N/ @ # 2dd 删除两行 1 j5 o1 Z; {- e* g |! H8 a 修改0 D9 I' ]" p/ J- ]4 u/ I! S: U # i 插入文本 F, Q- f8 k! Z9 S # A 当前行末尾添加 # r 替换当前字符 # o 打开新的一行并进入插入模式' M7 o2 G* L, ?2 d9 S, d' C * J* ?) L, ~" V2 {4 a; i ~ 撤销& _. Q K* u+ G+ d # u 撤销# m) \& n9 {7 U/ w5 G # <ctrl>+r 取消撤销 复制粘贴剪切 # v 进入可视模式% }2 N2 I5 o. k! D& B3 t9 s( N( _ # y 复制# \% c+ Y8 [/ j. x7 E # p 粘贴 # yy 复制当前行: v$ H6 |" D: o. \4 [ # dd 剪切当前行! _3 H+ Y& r* T, ~( v+ L 状态 #<ctrl>+g 显示当前行以及文件信息. { |, {9 h7 p' ^- h9 y) r 查找 # / 正向查找(n:继续查找,N:相反方向继续查找) # ? 逆向查找7 ~2 ]& H4 k5 e& ?7 }" R # % 查找配对的 {,[,( # :set ic 忽略大小写$ v" B. v* v4 z+ [& i, L. u # :set noic 取消忽略大小写4 d* z) P1 Y; B6 @( S4 K2 W- U/ L. N- w # :set hls 匹配项高亮显示 # :set is 显示部分匹配 替换 # :s/old/new 替换该行第一个匹配串9 v9 Q1 T2 A% V0 s # :s/old/new/g 替换全行的匹配串- V! X% O) `1 A # :%s/old/new/g 替换整个文件的匹配串/ w3 J$ L0 h' I, s. V/ n ( R7 H2 z1 s0 u, u+ Q 折叠 # zc 折叠( T: Q4 E8 e- W2 _ # zC 折叠所有嵌套/ S, Z3 @) A) t5 { # zo 展开折叠% A# z# A' O" y- H' ]) G) q% Y$ L% l; j # zO 展开所有折叠嵌套 执行外部命令+ \; R& E( E/ } i' v: N. u # :!shell 执行外部命令 % _) `) }3 P6 J0 o( l! N 3 y% l- e" r; y3 [# E8 M .vimrc8 a M4 [. x( K .vimrc 是 Vim 的配置文件,需要我们自己创建: cd Home // 进入 Home 目录: a( i( N$ _! L/ d( G1 b9 Dtouch .vimrc // 配置文件- \* [1 ]; S7 P9 w7 ^$ W' S0 ^ 9 @" J9 h& \9 H) V7 @6 D2 h0 Q1 h # Unix3 I" m" B; ]) @4 Z+ I # vim-plug) [8 n: M; s x, [. M9 ]) X; t8 H # Vim curl -fLo ~/.vim/autoload/plug.vim --create-dirs \* H9 T4 k1 [; o( |. v( N8 W https://raw.githubusercontent.co ... lug/master/plug.vim # Neovim curl -fLo ~/.local/share/nvim/site/autoload/plug.vim --create-dirs \ https://raw.githubusercontent.co ... lug/master/plug.vim4 _* X. Q, o* P+ D% S: V 其他平台,可以查看 vim-plug[1]。 set nobackup8 G# ^4 x* e% r4 A+ `4 V set noswapfile 7 _* _/ R+ o0 P7 m/ _ 文件编码 setencoding=utf-87 P0 ^4 @' C8 y% L 1 Q; I0 a' i5 e% v* x2 p7 B 显示行号 setnumber& h7 L/ D% [' {1 M; d. S# i `9 n3 Y3 I7 ?" t& t - J- Q: T9 E+ [6 t* Y. o5 h 取消换行4 B$ p o# i) k0 h) j0 Z+ m3 S9 v setnowrap% Q% x1 @& X1 g: W2 O) w: B 8 H2 ~- h4 K* I 显示光标当前位置 setruler0 U* U/ f& `0 b, N ) n6 d2 g4 d9 } 设置缩进; O2 V0 z7 E* I' C. e' G) ~ set cindent set tabstop=2' C8 n" W8 W3 o1 x2 c9 R& |" ~ set shiftwidth=2 4 P" E M s+ I; {4 Y! V 突出显示当前行, p% N7 H- @- k; ?: {; @" a. V setcursorline; [% T9 j" s4 s! ]- o' C* ~! _ # v$ o' P& {5 G3 i7 A 查找* g# m+ e1 t* d" B set ic set hls7 t% {$ N- \6 f/ @7 z set is 6 T- x" N6 K- |2 h, k5 b7 Z; f, D 左下角显示当前vim模式3 p: Q+ U7 h. B) d6 {% `; h( [ setshowmode/ y& t: ?4 {4 e' n% C2 R " G# K3 k! g- y) @6 {; z7 t3 u; C 代码折叠, R4 h8 f% c9 A2 ^# R, n #启动 vim 时关闭折叠代码+ N5 I6 c/ g5 w set nofoldenable 主题 syntax enable set background=dark9 I+ y8 V$ \% e( _7 b colorscheme solarized6 C! L2 B4 E5 ]7 C& z ◈ altercation/vim-colors-solarized[2]: n: y K( T: e& o+ f# i ◈ Anthony25/gnome-terminal-colors-solarized[3] , J! N* K1 O! L: f2 v 插件配置树形目录Plug 'scrooloose/nerdtree' Plug 'jistr/vim-nerdtree-tabs'2 q0 Y7 R% e, ~$ C% I Plug 'Xuyuanp/nerdtree-git-plugin'5 d9 q: O6 [! q( x, y( L0 ? ' p$ L/ e2 s& _' n$ Y, p+ @ autocmd vimenter * NERDTree map <C-n> :NERDTreeToggle<CR>! }( K* I" W J* t, x& @ let NERDTreeShowHidden=15 d9 \9 ] l, v+ w' a: \* [ let g:NERDTreeShowIgnoredStatus = 1 let g:nerdtree_tabs_open_on_console_startup=1 let g:NERDTreeIndicatorMapCustom = {" O* [7 C4 w) A0 A9 v \ "Modified" : "✹",) h4 d+ [7 f% J- R \ "Staged" : "✚", \ "Untracked" : "✭",% K" c1 O6 i, p, z \ "Renamed" : "➜",4 `3 t. a$ U4 B- j1 A+ ^ \ "Unmerged" : "═", \ "Deleted" : "✖", \ "Dirty" : "✗", \ "Clean" : "✔︎", \ 'Ignored' : '☒', \ "Unknown" : "?" \ }2 q1 x8 K) L7 Y( [) O) a # o 打开关闭文件或目录 # e 以文件管理的方式打开选中的目录 # t 在标签页中打开 # T 在标签页中打开,但光标仍然留在 NERDTree # r 刷新光标所在的目录/ [, |+ U) o9 L$ j& I0 M # R 刷新当前根路径 # X 收起所有目录; E( J2 Y1 ^4 @% y- O/ p( j( g # p 小写,跳转到光标所在的上一级路径0 n. x" J6 V" w7 C, k # P 大写,跳转到当前根路径 # J 到第一个节点 # K 到最后一个节点 # I 显示隐藏文件 # m 显示文件操作菜单 m. l. \1 Z+ f/ O/ Z0 p # C 将根路径设置为光标所在的目录 # u 设置上级目录为根路径' c4 u( K* z2 a B' P9 T # ctrl + w + w 光标自动在左右侧窗口切换5 s1 p8 ^% ~; H' B5 x* R% ]/ x # ctrl + w + r 移动当前窗口的布局位置0 t" w* H. }& r2 h) r! N # :tabc 关闭当前的 tab # :tabo 关闭所有其他的 tab ^# {0 z. I. f& }) { n' c # :tabp 前一个 tab # :tabn 后一个 tab # gT 前一个 tab # gt 后一个 tab' D/ x/ }* l0 G ◈ scrooloose/nerdtree[4]8 l F- K* B7 `6 o8 R) K1 T& W 代码,引号,路径补全Plug 'Valloric/YouCompleteMe'* E& _5 ~' n: s9 v o◈ vim-nerdtree-tabs[5] ◈ nerdtree-git-plugin[6] Plug 'Raimondi/delimitMate'% D( i8 @3 x, F6 r" l Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' }& c, n- O- Z! w+ E9 G3 C. }5 o- K L7 ~ ◈ Valloric/YouCompleteMe[7] ◈ Raimondi/delimitMate[8]' [, `2 E2 X0 c* w* _" M/ a0 r ◈ Shougo/deoplete.nvim[9] Plug 'sheerun/vim-polyglot'! j* Q, D# f- D& |( N8 _ Plug 'w0rp/ale'2 N. W# A7 @5 a) c let g:ale_linters = { \ 'javascript': ['eslint'], \ 'css': ['stylelint'], \}1 d5 N: t6 j& F let g:ale_fixers = { \ 'javascript': ['eslint'],# K" D" [) x% t( A& L F! @$ N6 N) R \ 'css': ['stylelint'],% O- t- w( p+ F/ Y8 H6 v J \}3 S f4 V, P' u6 l3 P# u let g:ale_fix_on_save = 1* K- A+ y7 _; U2 B1 b , }4 A+ T7 Z$ s) Y0 |% f0 V let g:ale_sign_column_always = 1 f' d6 _0 x% l+ f+ Y7 B/ s let g:ale_sign_error = '●' let g:ale_sign_warning = '▶'% T0 t7 M6 W' }$ _0 t nmap <silent> <C-k> < lug>(ale_previous_wrap)nmap <silent> <C-j> < lug>(ale_next_wrap)◈ w0rp/ale[10] ◈ sheerun/vim-polyglot[11] : k: h/ ^! r& D& j 文件,代码搜索" B6 C* E6 U6 _1 k J4 u5 [Plug 'rking/ag.vim'1 `9 C0 U! d# [ Plug 'kien/ctrlp.vim'' Z0 e/ x t, K! j% L ◈ kien/ctrlp.vim[12]) M7 q7 h: z. M- g ◈ ggreer/the_silver_searcher[13] ◈ rking/ag.vim[14] Plug 'vim-airline/vim-airline', ]. j! ]& o( R: F Plug 'vim-airline/vim-airline-themes' 8 _0 ?$ S, O9 B8 n let g:airline_theme='papercolor' ◈ vim-airline/vim-airline[15]1 ?* b$ `$ q- ~ ◈ vim-airline/vim-airline-themes[16] 2 s, b. J% b0 j v0 i# q 代码注释Plug 'scrooloose/nerdcommenter' # <leader>cc // 注释 # <leader>cm 只用一组符号注释 # <leader>cA 在行尾添加注释 # <leader>c$ /* 注释 */ # <leader>cs /* 块注释 */ # <leader>cy 注释并复制; e' ]4 l. R# A S) H; f9 I- t # <leader>c<space> 注释/取消注释+ a% B$ j2 ~' @: U # <leader>ca 切换 // 和 /* */* F+ S8 r( h* a( c/ _! k # <leader>cu 取消注释# e [$ |9 ]/ ^" A8 A% C8 q! x % H) Y: n7 E( J5 Y: a8 g7 f4 t let g:NERDSpaceDelims = 18 n- |" W: s* k; I, B let g:NERDDefaultAlign = 'left' let g:NERDCustomDelimiters = { \ 'javascript': { 'left': '//', 'leftAlt': '/**', 'rightAlt': '*/' }, \ 'less': { 'left': '/**', 'right': '*/' }5 S& W. T( O# ~0 k& ]3 Z \ } ◈ scrooloose/nerdcommenter[17] Plug 'airblade/vim-gitgutter': k. d$ u1 ]/ m6 P. B Plug 'tpope/vim-fugitive'" ^1 m: x8 ~% X Q ◈ airblade/vim-gitgutter[18] ◈ tpope/vim-fugitive[19] ! `! N) P8 h. v0 x- q Markdown% ]4 l! Z v/ A- vPlug 'suan/vim-instant-markdown'2 {0 U6 m5 H$ b }: s* E$ d 4 {+ A. {$ B4 m* _3 A6 o' @. V let g:instant_markdown_slow = 1 let g:instant_markdown_autostart = 0/ B+ X" {: Y) m$ T: Y$ ^8 P # :InstantMarkdownPreview ◈ suan/vim-instant-markdown[20] # E9 g% J# d" _8 B) _1 T& a' }3 N; X EmmetPlug 'mattn/emmet-vim' let g:user_emmet_leader_key='<Tab>' let g:user_emmet_settings = {3 x8 T* B8 U" A/ o( U2 \8 e' l6 V \ 'javascript.jsx' : { \ 'extends' : 'jsx', \ },8 `8 G" R/ X; h \ } ◈ mattn/emmet-vim[21] 9 j" V6 Z# Z$ B$ J+ S9 z0 N# |3 ?! B html 5Plug'othree/html5.vim'/ C- ?9 w9 l5 M5 j/ b( v ◈ othree/html5.vim[22] Plug 'hail2u/vim-css3-syntax' Plug 'ap/vim-css-color'. W1 S+ F6 F; }" Y0 I9 X8 U 2 U2 |( @2 z# I# B( g$ i augroup VimCSS3Syntax autocmd!$ N! }( y8 ~7 _7 e8 b% A0 t, u autocmd FileType css setlocal iskeyword+=- augroup END ◈ hail2u/vim-css3-syntax[23] ◈ ap/vim-css-color[24] & ?1 |, [: O) S6 R/ k. p8 b" G7 M JavaScipt3 _, \! t4 ^9 `2 V2 z1 a. Z, [Plug 'pangloss/vim-javascript' let g:javascript_plugin_jsdoc = 1 let g:javascript_plugin_ngdoc = 14 R9 J$ T! |6 N) _' y8 f) k4 p- r let g:javascript_plugin_flow = 1/ R' @! Q) o7 L+ r: ~/ I set foldmethod=syntax let g:javascript_conceal_function = "ƒ" let g:javascript_conceal_null = "ø" let g:javascript_conceal_this = "@" let g:javascript_conceal_return = "⇚"2 C9 G0 S$ h3 i let g:javascript_conceal_undefined = "¿"* A& m% O+ \5 e9 e+ O9 d let g:javascript_conceal_NaN = "ℕ" let g:javascript_conceal_prototype = "¶"( C {( z; S5 j3 h6 X' f) L: L let g:javascript_conceal_static = "•": C) O2 c& H) n) r7 J; b; u let g:javascript_conceal_super = "Ω"0 t: i. A5 q! V2 V: F# H let g:javascript_conceal_arrow_function = "⇒"/ U2 v b5 I8 E& _8 D/ f let g:javascript_conceal_noarg_arrow_function = " "% e! @, I- a1 d6 o k3 ~ let g:javascript_conceal_underscore_arrow_function = " "2 J! \) e* |9 J/ {. V* U( V set conceallevel=1 ◈ pangloss/vim-javascript[25] (注:上述脚本中存在特殊字符,有的情况下显示不正确,请直接用上述链接的内容。) + z& ^/ N! U2 c1 Z% J$ O# c/ G React! Q- T$ F/ j3 hPlug 'mxw/vim-jsx' let g:jsx_ext_required = 0 ◈ mxw/vim-jsx[26] * ^* h- P5 g! }" ]# S2 ^& u% A$ x. C8 ^ PrettierPlug 'prettier/vim-prettier', { \ 'do': 'yarn install', \ 'for': ['javascript', 'typescript', 'css', 'less', 'scss', 'json', 'graphql'] } let g:prettier#config#bracket_spacing = 'true' let g:prettier#config#jsx_bracket_same_line = 'false' let g:prettier#autoformat = 00 Q1 ~+ L! p( v) k" r$ j! s. A autocmd BufWritePre *.js,*.jsx,*.mjs,*.ts,*.tsx,*.css,*.less,*.scss,*.json,*.graphql PrettierAsync # rettier0 K( q8 ~+ l9 v4 g- F( _8 w◈ prettier/vim-prettier[27] . A2 u5 s# R9 D4 H 总结最后,呈上参考配置 .vimrc[28],如果关于 vim 有更好的 idea,欢迎在评论中交流。 [1]: https://github.com/junegunn/vim-plug V* x# u* C; _1 K# a[2]: https://github.com/altercation/vim-colors-solarized3 f4 f+ n" u# O9 _* T+ A4 ]* V [3]: https://github.com/Anthony25/gnome-terminal-colors-solarized3 d# l1 y5 G" H* x) n2 p1 V7 N2 n6 s4 P [4]: https://github.com/scrooloose/nerdtree! t4 w5 V, J w. S, G$ T) E7 d+ ` [5]: https://github.com/jistr/vim-nerdtree-tabs [6]: https://github.com/Xuyuanp/nerdtree-git-plugin [7]: https://github.com/Valloric/YouCompleteMe' i% d- x9 j* r1 _ [8]: https://github.com/Raimondi/delimitMate, m* N, o. N @/ b. u# o8 E [9]: https://github.com/Shougo/deoplete.nvim [10]: https://github.com/w0rp/ale [11]: https://github.com/sheerun/vim-polyglot [12]: https://github.com/kien/ctrlp.vim [13]: https://github.com/ggreer/the_silver_searcher& V1 d+ q' K' c9 i [14]: https://github.com/rking/ag.vim1 ?6 j% d3 i! X. p S [15]: https://github.com/vim-airline/vim-airline [16]: https://github.com/vim-airline/vim-airline-themes [17]: https://github.com/scrooloose/nerdcommenter3 _( S1 q [& n! t" J: ]5 m' d5 S. h7 U [18]: https://github.com/airblade/vim-gitgutter [19]: https://github.com/tpope/vim-fugitive3 T4 ]0 `! T6 p0 N4 v% U [20]: https://github.com/suan/vim-instant-markdown9 _$ p3 g, _2 d8 ? G, T [21]: https://github.com/mattn/emmet-vim [22]: https://github.com/othree/html5.vim [23]: https://github.com/hail2u/vim-css3-syntax& u. S0 o4 N/ A0 x( M( h) s [24]: https://github.com/ap/vim-css-color" W/ Y" F1 H E) E6 m [25]: https://github.com/pangloss/vim-javascript( j/ V- J/ ]3 R4 s+ R: g3 ~3 h [26]: https://github.com/mxw/vim-jsx [27]: https://github.com/prettier/vim-prettier( A1 O4 P0 j' A# O: q' J! S [28]: https://github.com/FengShangWuQi/to-vim/blob/master/.vimrc / y8 X1 Q" U$ c( S5 a2 s- j |
微信公众号
手机版