作者:楓上霧棋
來源:https://segmentfault.com/a/1190000011466454
Vim 是一個上古神器,剛剛接觸的同學可能很難駕馭,本篇文章主要帶領大家 快速入門,並 持續分享 當前流行的一些 vim-plugin
安裝
sudo apt-get install vim // Ubuntu
新手指南
vimtutor // vim 教程
移動光標
# hjkl
# 2w 向前移動兩個單詞
# 3e 向前移動到第 3 個單詞的末尾
# 0 移動到行首
# $ 當前行的末尾
# gg 文件第一行
# G 文件最後一行
# 行號+G 指定行
# <ctrl>+o 跳轉回之前的位置
# <ctrl>+i 返回跳轉之前的位置
/<ctrl>/<ctrl>
退出
#進入正常模式
# :q! 不保存退出
# :wq 保存後退出
刪除
# x 刪除當前字符
# dw 刪除至當前單詞末尾
# de 刪除至當前單詞末尾,包括當前字符
# d$ 刪除至當前行尾
# dd 刪除整行
# 2dd 刪除兩行
修改
# i 插入文本
# A 當前行末尾添加
# r 替換當前字符
# o 打開新的一行並進入插入模式
撤銷
# u 撤銷
# <ctrl>+r 取消撤銷
/<ctrl>
複製粘貼剪切
# v 進入可視模式
# y 複製
# p 粘貼
# yy 複製當前行
# dd 剪切當前行
文件
# :e! 強制刷新該文件
# <ctrl>+g 顯示當前行以及文件信息
/<ctrl>
查找
# / 正向查找(n:繼續查找,N:相反方向繼續查找)
# ? 逆向查找
# % 查找配對的 {,[,(
# :set ic 忽略大小寫
# :set noic 取消忽略大小寫
# :set hls 匹配項高亮顯示
# :set is 顯示部分匹配
替換
# :s/old/new 替換該行第一個匹配串
# :s/old/new/g 替換全行的匹配串
# :%s/old/new/g 替換整個文件的匹配串
摺疊
# zc 摺疊
# zC 摺疊所有嵌套
# zo 展開摺疊
# zO 展開所有摺疊嵌套
執行外部命令
# :!shell 執行外部命令
字體
# <ctrl> - 縮小
# <ctrl> shift + 放大
# <ctrl> 0 還原
/<ctrl>/<ctrl>/<ctrl>
分屏
$ Ctrl+W v // 左右
$ Ctrl+W s // 上下
# 移動光標
$ Ctrl+W h/j/k/l // 左/上/下/右
# 移動分屏
$ Ctrl+W H/J/K/L // 左/上/下/右
# 修改屏幕尺寸
$ Ctrl+W =/+/-
基本配置
.vimrc 是 Vim 的配置文件,需要我們自己創建
cd
touch .vimrc
取消備份
set nobackup
set noswapfile
文件編碼
set encoding=utf-8
查找
set ic
set hls
set is
顯示行號
set number
顯示光標當前位置
set ruler
設置縮進
set cindent
set tabstop=2
set shiftwidth=2
突出顯示當前行
set cursorline
左下角顯示當前 vim 模式
set showmode
代碼摺疊
set nofoldenable
主題
syntax enable
set background=dark
colorscheme solarized
- altercation/vim-colors-solarized
- Anthony25/gnome-terminal-colors-solarized
插件配置
使用 vim-plug 管理插件
樹形目錄
代碼,引號,路徑補全
Plug 'Valloric/YouCompleteMe'
Plug 'Raimondi/delimitMate'
Plug 'Shougo/deoplete.nvim', { 'do': ':UpdateRemotePlugins' }
- Valloric/YouCompleteMe
- Raimondi/delimitMate
- Shougo/deoplete.nvim
語法高亮,檢查
- w0rp/ale
- sheerun/vim-polyglot
文件,代碼搜索,打開最近打開的文件
- kien/ctrlp.vim
- ggreer/the_silver_searcher
- rking/ag.vim
- vim-scripts/mru.vim
加強版狀態欄
- itchyny/lightline.vim
代碼註釋
- scrooloose/nerdcommenter
git
Plug 'airblade/vim-gitgutter'
Plug 'tpope/vim-fugitive'
- airblade/vim-gitgutter
- tpope/vim-fugitive
Markdown
Plug 'suan/vim-instant-markdown'
Plug 'junegunn/goyo.vim'
Plug 'amix/vim-zenroom2'
let g:instant_markdown_slow = 1
let g:instant_markdown_autostart = 0
# :InstantMarkdownPreview
# :Goyo
- suan/vim-instant-markdown
- junegunn/goyo.vim
- amix/vim-zenroom2
Emmet
- mattn/emmet-vim
html 5
Plug 'othree/html5.vim'
- othree/html5.vim
css 3
Plug 'hail2u/vim-css3-syntax'
Plug 'ap/vim-css-color'
augroup VimCSS3Syntax
autocmd!
autocmd FileType css setlocal iskeyword+=-
augroup END
- hail2u/vim-css3-syntax
- ap/vim-css-color
JavaScipt
- pangloss/vim-javascript
js-beautify
Plug 'maksimr/vim-jsbeautify'
autocmd FileType html noremap <buffer>:call HtmlBeautify() /<buffer>
- js-beautify
React
Plug 'mxw/vim-jsx'
let g:jsx_ext_required = 0
- mxw/vim-jsx
Prettier
- prettier/vim-prettier
自動規範化中文排版
Plug 'hotoo/pangu.vim'
# :Pangu
- hotoo/pangu.vim
總結
最後,提供 to-vim-tmux-zsh 倉庫可供參考,其中,除包含 vim 的配置,還有 tmux 和 zsh 的分享
閱讀更多 程序員聖經 的文章