目錄
-
- 常用指令
-
- 1. 注釋
- 2. 多視窗
- 3. 查找與替換
- 4. 與shell互動
- 5. 對比差異
- 屬性設定
-
- 1. 進入home路徑下
- 2. 建立.vimrc檔案
- 3. 粘貼以下内容
- 插件
-
- 1. 檢查vim版本是否符合
- 2. 安裝Vundle插件管理器
- 3. 安裝插件
開發工作中往往會用到Linux系統,如果直接使用桌面版還好,但是如果使用putty或MobaXterm此類工具遠端伺服器必然會少不了和vim打交道,希望這篇文章能夠讓大家對vim産生興趣
常用指令
我覺得vim指令是一個讓人又愛又恨的東西,突然從Windows下的滑鼠操作切換到vim的Linux操作會感覺很不友善,有的時候甚至會誤操作導緻很大的坑。但是用習慣後再回到window下回發現會不自覺的使用這些指令,可見,這些指令的确帶來了便攜性,vim指令有很多,一下子也記不完,這裡我就總結一些我個人常用的指令。
1. 注釋
單行注釋:可以直接在行前添加注釋符比如 #
多行注釋:ctrl+v 進入視圖模式, 然後shift+i,輸入注釋符,最後按Esc
2. 多視窗
寫代碼過程中要打開多個視窗,如果反複關閉開啟會很麻煩,vim可以使用多視窗
直接打開多個檔案:
vim -On file1 file2 ...
已經打開一個檔案之後再大開一個檔案:
# 指令模式下
:e file
多視窗切換檔案:
bn #下一個檔案
bp #上一個檔案
3. 查找與替換
# 指令模式下
/要查找的字元串
:%s/被替換的字元串/新字元串/标志
# 詳細内容可以通路下面連結
可參考連結:https://harttle.land/2016/08/08/vim-search-in-file.html
4. 與shell互動
臨時退出:ctrl+z
回到vim:fg
執行shell指令:
# 指令模式下
:! ls ./
5. 對比差異
這一點和beyond compare類似:
vim -diff file1 file2
沒有詳細總結,臨時就想起這麼多,後續會持續更新。
屬性設定
可以對vim屬性設定,使得每次打開vim時都能起作用,基本的屬性設定比如高亮、縮進等,先貼上我的設定
1. 進入home路徑下
cd ~
2. 建立.vimrc檔案
touch .vimrc
vim .vimrc
3. 粘貼以下内容
set nocompatible
set number
set ic
set hlsearch
set encoding=utf-8
set fileencodings=utf-8,ucs-bom,GB2312,big5
set autoindent
set smartindent
set scrolloff=2
set showmatch
set shiftwidth=4
set tabstop=4
set showmatch
set cursorline
set autoread
syntax on
這樣儲存退出後就生效了,網上有很多這一類的教程,建立.vimrc的作用是保證每次啟動vim都可以起作用。
插件
工欲善其事必先利其器,無論是做Python還是c++開發,如果有一個好的IDE,那麼效率将會得到大大的提升,如果想把vim用于輕量級IDE,那麼插件是必不可少的,以下以vim配置Python3為例。
1. 檢查vim版本是否符合
vim --version
在輸出的資訊檢查以下是否有’+python3’字眼,如果沒有說明vim版本太低不支援,需要解除安裝重新安裝vim。
2. 安裝Vundle插件管理器
個人認為Vundle 就如同maven之于Java、npm之于js、pip之于Python,安裝了Vundle後友善後續安裝插件。
首先克隆島指定目錄:
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
如果克隆失敗可以參考我的另外一篇文章 https://zhuanlan.zhihu.com/p/53961303
其次,在~/.vimrc中輸入如下内容:
set nocompatible " 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 'gmarik/Vundle.vim'
" Add all your plugins here (note older versions of Vundle used Bundle instead of Plugin)
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
上述 Add all your plugins here (note older versions of Vundle used Bundle instead of Plugin)下面的一段空白處是用于添加其他插件。
3. 安裝插件
首推YouCompleteMe,這個插件用于自動補全,速度補全速度可以達到pycharm的級别,但是這個插件安裝十分麻煩,可以參考我的另外一篇文章:
另一篇文章連結如下: https://zhuanlan.zhihu.com/p/54074345
然後在~/.vimrc中添加:
儲存退出重新打開,在指令模式下輸入:PluginInstall即可安裝,左下角顯示Done!即為完成,可以在緊接着下面添加如下設定:
let g:ycm_global_ycm_extra_conf = "~/.vim/bundle/YouCompleteMe/cpp/ycm/.ycm_extra_conf.py"
let g:ycm_key_invoke_completion = ''
let g:ycm_min_num_identifier_candidate_chars = 2
let g:ycm_goto_buffer_command = 'horizontal-split'
let g:ycm_seed_identifiers_with_syntax=1
map <F2> :YcmCompleter GoTo<CR> # 按F2跳轉到定義處
let g:ycm_error_symbol = '>>'
let g:ycm_warning_symbol = '>*'
其次是Autoformat這是一個自動格式化代碼的工具,事先需要安裝pep8:
pip install autopep8
其次在vimrc空白處添加:
Plugin 'Chiel92/vim-autoformat'
nnoremap <F6> :Autoformat<CR> # 按F6自動格式化
let g:autoformat_autoindent = 0
let g:autoformat_retab = 0
let g:autoformat_remove_trailing_spaces = 0
儲存并退出,重新進入vim,指令模式下輸入:PluginInstall
還有nerdtree這是一個目錄樹插件,可以給代碼添加目錄:
Plugin 'https://github.com/scrooloose/nerdtree'
nnoremap <F3> :NERDTreeToggle<CR> # 按F3顯示或隐藏目錄
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
還有indentLine,這是一個縮進線插件,對于Python這種縮進要求很嚴格的語言來說很重要:
Plugin 'Yggdroot/indentLine'
let g:indentLine_char='┆'
let g:indentLine_enabled = 1
注意:把上述内容添加到vimrc後需要關閉後重新打開,在指令模式下輸入:PluginInstall,也可以用:Plugin ‘插件’,例如:Plugin ‘Yggdroot/indentLine’
後續會陸續更新,感興趣的同學可以關注一下微信公衆号和頭條号。