Vim 插鍵及配置

image
歡迎移步部落格閱讀: ofollow,noindex">Vim 插鍵及配置
編輯器之神 —— Vim
平日使用vim經常編輯檔案,想想使用時的痛點,決定研究一下外掛的使用。
Vim的擴充套件通常也被成為bundle或外掛。
軟體版本:
- Mac OS X 10.14.1
- vim 8.1
外掛安裝-Vundle
眾多文章中都提到 Vundle ,那我就選用它好了!
有一個 Vim 的插鍵網站,專門有相關插鍵的配置介紹: VimAwesome
1.將Vundle下載到本地,後面下載的外掛也將會下載到 ~/.vim/bundle
路徑下。
git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim
2.外掛配置,將如下的內容貼上到 ~/.vimrc
的頂部(前提是,你本身 .vimrc
裡一開始沒有什麼其他內容)。
set nocompatible" be iMproved, required filetype off" required " set the runtime path to include Vundle and initialize set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() " alternatively, pass a path where Vundle should install plugins "call vundle#begin('~/some/path/here') " let Vundle manage Vundle, required Plugin 'VundleVim/Vundle.vim' " The following are examples of different formats supported. " Keep Plugin commands between vundle#begin/end. " plugin on GitHub repo " Plugin 'tpope/vim-fugitive' " plugin from http://vim-scripts.org/vim/scripts.html " Plugin 'L9' " Git plugin not hosted on GitHub " Plugin 'git://git.wincent.com/command-t.git' " git repos on your local machine (i.e. when working on your own plugin) " Plugin 'file:///home/gmarik/path/to/plugin' " The sparkup vim script is in a subdirectory of this repo called vim. " Pass the path to set the runtimepath properly. " Plugin 'rstacruz/sparkup', {'rtp': 'vim/'} " Install L9 and avoid a Naming conflict if you've already installed a " different version somewhere else. " Plugin 'ascenator/L9', {'name': 'newL9'} " All of your Plugins must be added before the following line call vundle#end()" required filetype plugin indent on" required " To ignore plugin indent changes, instead use: "filetype plugin on " " Brief help " :PluginList- lists configured plugins " :PluginInstall- installs plugins; append `!` to update or just :PluginUpdate " :PluginSearch foo - searches for foo; append `!` to refresh local cache " :PluginClean- confirms removal of unused plugins; append `!` to auto-approve removal " " see :h vundle for more details or wiki for FAQ " Put your non-Plugin stuff after this line
3.安裝外掛
將需要安裝的插鍵放入 .vimrc
檔案中即可,例如:
Plugin 'scrooloose/nerdtree' "Plugin 'scrooloose/nerdtree'" 如果暫時不啟用,就將該外掛註釋掉
開啟vim,然後輸入:
vim # 開啟 vim :PluginInstall :PluginList # 檢視已安裝插鍵列表
4.升級與解除安裝外掛
# 註釋下面這段話 # Plugin 'Valloric/YouCompleteMe'
然後
:PluginUpdate # 這個命令本身可以一鍵升級所有外掛 :PlugginClean
參考:
5.幫助文件
:h vundle
NERDTree
Plugin 'scrooloose/nerdtree' "F2開啟和關閉樹" map <F2> :NERDTreeToggle<CR> let NERDTreeChDirMode=1 "顯示書籤" let NERDTreeShowBookmarks=1 "設定忽略檔案型別" let NERDTreeIgnore=['\~$', '\.pyc$', '\.swp$'] "視窗大小" let NERDTreeWinSize=25
python-mode
Plugin 'klen/python-mode' " https://vimawesome.com/plugin/python-mode
編寫 Python 檔案儲存時,就會進行語法檢查了:
let g:pymode_rope = 1 let g:pymode_rope_completion = 1 let g:pymode_rope_completion_bind = '<C-p>'"為了自動補全有效,需要將 set paste 設定不啟用 let g:pymode_rope_goto_definition_bind = '<C-c>g' let g:pymode_python = 'python' " 預設檢查 Python2 "Autofix PEP8 errors nnoremap <leader>f :PymodeLintAuto<CR>
快捷鍵:
K 顯示內建函式文件 <leader>r 執行 python 檔案 # let mapleader=", " " 設定 leader 為空格,那麼`,+r`就可以執行 python 檔案了
參考:
- 油管-python-mode
- Can't get the jedi-vim plugin to work 解決了 python-mode 不自動補全的問題
- python-mode/doc/pymode.txt
vim-airline
一個狀態列美化工具,顏控必備。附帶功能可以一目瞭然的區分各種編輯狀態。
Plugin 'vim-airline/vim-airline' "https://github.com/vim-airline/vim-airline Plugin 'vim-airline/vim-airline-themes' "https://github.com/vim-airline/vim-airline-themes https://github.com/vim-airline/vim-airline/wiki/Screenshots
To use solarized dark, set :AirlineTheme solarized and add the following to your .vimrc: let g:airline_solarized_bg='dark'
配置:
let g:airline_powerline_fonts = 1 let g:airline_theme='deus' let g:Powerline_symbols='fancy' let Powerline_symbols='fancy' set t_Co=256 " 狀態列就有顏色了
Line" target="_blank" rel="nofollow,noindex">Yggdroot/indentLine
安裝:
Plugin 'Yggdroot/indentLine'
配置:
let g:indentLine_enabled = 1 let g:indentLine_color_term = 239 map <leader>m :IndentLinesToggle<CR> # 解決vim複製程式碼時,縮排線也被複制的問題 https://github.com/Yggdroot/indentLine/issues/261
為了這個外掛能夠有效果,也是折騰了半天。在 CentOS 平臺是正常的,但是在 Mac 上的縮排線顯示不正確,為何會這樣呢? SOF-Why is apple vim compiled WITHOUT conceal feature? ,原來 Mac 上自帶的 Vim 版本雖然是8.0版本,但是沒有 concel
這個 Feature ,而 indentLine
外掛要顯示對齊線依賴這個, 堅線和星號在使用 conceal 功能 。
那麼該怎麼新增這個特性呢?搜了一圈,可以重新安裝 Vim,可以參考 這篇文章 安裝 Vim。
經過安裝設定之後,可以通過 vim --version|grep con
或者 :echo has("conceal")
檢視是否已經具有 conceal 特性:

image
rking/ag.vim
ag 的語法:
ag [FILE-TYPE] [OPTIONS] PATTERN [PATH]
ag 這個 vim 插鍵主要是基於這個專案 ggreer/the_silver_searcher
ag --list-file-types # 檢視支援自定義哪些檔案型別
安裝了這個插鍵後,在 vim 的命令模式下,可以使用 :Ag [options] {pattern} [{directory}]
搜尋了。
majutsushi/tagbar
安裝 vim 插鍵之前,機器本身需要 ctags
:
# ubuntu sudo apt-get install ctags # centos sudo yum install ctags # mac brew install ctags
在這時使用 vim-tagbar 外掛可以幫你快速瞭解當前檔案中的類、方法等。
Plugin 'majutsushi/tagbar' " https://github.com/majutsushi/tagbar
配置:
nmap <F8> :TagbarToggle<CR>
關於 tagbar 的使用,看檢視這篇文章 wklken-大綱式導航
YouCompleteMe
目前主要涉及的是 Python 開發,所以,YCM 目前沒有配置,如下僅供參考。
Plugin 'Valloric/YouCompleteMe'
sudo apt-get install build-essential cmake sudo apt-get install python-dev python3-dev cd ~/.vim/bundle/YouCompleteMe ./install.py --all cp ~/.vim/bundle/YouCompleteMe/third_party/ycmd/examples/.ycm_extra_conf.py ~/.vim/
"YouCompleteMe配置相關 let g:ycm_server_python_interpreter='/usr/bin/python' let g:ycm_global_ycm_extra_conf='~/.vim/.ycm_extra_conf.py'
主題
安裝主題的方法比較簡單,例如需要安裝 molokai
主題,手動安裝則按照如下步驟操作:
- 從 https://github.com/fatih/molokai 下載
molokai.vim
檔案,放入~/.vim/colors
中 - 然後在
~/.vimrc
檔案中加入行colorscheme molokai
即可。
因為我在 VSCode 和 Iterm2 中都採用了 Dracula 的主題,因此,vim 主題我也同樣偏愛它,可以採用插鍵的方式安裝:
Plugin 'dracula/vim' :PluginInstall
- 然後在
~/.vimrc
檔案中加入行colorscheme dracula
即可。
主題相關的命令:
:colorscheme"檢視當前主題 :colorscheme space tab "列出所有主題 :colorscheme your-theme "切換主題
設定vim
為了讓vim使用起來更加得心應手,先做一些簡單的配置。
編輯VIM配置檔案,可能一開始沒有這個檔案,不過沒關係,直接 vi ~/.vimrc
儲存這個檔案即可。
今天學習到 <leader>
這個概念,很強大,快捷鍵很方便!
-
nnoremap
將一個組合快捷鍵對映為另一個快捷鍵。
關於 leader
以及其他 map
知識,可以檢視如下文章:
個人vim配置
參考多人的配置,打造屬於自己的Vim配置,這個配置不涉及外掛的設定,因為常常生產環境是網路不通的,要迅速配置能用:
let mapleader="," " 設定 leader let g:mapleader = ',' " 分屏視窗移動, Smart way to move between windows map <C-j> <C-W>j map <C-k> <C-W>k map <C-h> <C-W>h map <C-l> <C-W>l " Go to home and end using capitalized directions " H和L跳轉到行首行末, 實在不想按0和$, 太遠 noremap H ^ noremap L $ " 命令列模式增強,ctrl - a到行首, -e 到行尾 cnoremap <C-a> <Home> cnoremap <C-e> <End> " 去掉搜尋高亮 noremap <silent><leader>/ :nohls<CR> " 快速儲存和退出 " Quickly close the current window nnoremap <leader>q :q<CR> " Quickly save the current file nnoremap <leader>w :w<CR> syntax on " 自動語法高亮 set cursorline " 突出顯示當前行 set encoding=utf-8 set fileencoding=utf-8 set fileformat=unix "從Win上覆制檔案時,避免換行符錯誤 set hlsearch " 搜尋時高亮顯示被找到的文字 set ignorecase smartcase " 搜尋時忽略大小寫,但在有一個或以上大寫字母時仍保持對大小寫敏感 set incsearch " 輸入搜尋內容時就顯示搜尋結果 set laststatus=2 " 顯示狀態列 (預設值為 1, 無法顯示狀態列) set magic " 設定魔術 set nocompatible " 關閉 vi 相容模式 set number " 顯示行號 set paste" 解決拷貝的時遇到註釋會自動註釋後續所有行的問題 set ruler " 開啟狀態列標尺 set shiftwidth=4 " 設定 << 和 >> 命令移動時的寬度為 4 set softtabstop=4 " 使得按退格鍵時可以一次刪掉 4 個空格 set smartindent " 開啟新行時使用智慧自動縮排 set tabstop=4 " 設定 tab 長度為 4 set ambiwidth=double " 設定為雙字寬顯示,否則無法完整顯示如:☆
vim配置檔案中的註釋,末尾用 "
隔開即可。保留註釋,對於瞭解配置內容有利。
vim配置參考
- Michael728/my-config-files 個人總結的配置檔案
- wklken/k-vim
- Mac OS X 配置指南-vim 同時也有關於 macOS 介紹
- 強大的vim配置檔案,讓程式設計更隨意 有vim配置的中文註釋
FAQ
1. github訪問速度慢,下載外掛失敗
2. mac上iterm2中, tmux裡面滑鼠複製, 無法選中一行內容
然後你會發現你想複製terminal上的東西的時候,死活複製不了,這時按住 Option (Alt)
鍵就行了。
參考
- pytlab-優雅的在終端中編寫Python 同時介紹了 Tmux 和 Vim 的配置
- 那些離了就活不了的 VIM 外掛 介紹了一些經典的插鍵
- 阮一峰-Vim 配置入門
- YouCompleteMe 安裝配置方法