我覺得vim命令是一個讓人又愛又恨的東西,突然從Windows下的滑鼠操作切換到vim的Linux操作會感覺很不方便,有的時候甚至會誤操作導致很大的坑。但是用習慣後再回到window下回發現會不自覺的使用這些命令,可見,這些命令的確帶來了便攜性,vim命令有很多,一下子也記不完,這裏我就總結一些我個人常用的命令及高效用法。
程式設計師必備寶藏庫 :https:// github.com/Jackpopc/CS- Books-Store1. 註釋
手動輸入註釋符:
單行註釋:可以直接在行前添加註釋符比如 #
多行註釋:ctrl+v 進入檢視模式, 然後shift+i,輸入註釋符,最後按Esc
也可以使用替換命令,詳細的請看第3條:
# 註釋,以Python為例
:m,ns/^/#/g # 註釋m到n行
# 解除註釋
:m,ns/^#//g
# 註釋整個文件
:1,$s/^/#/g
# 還有更高效快速的方法
:%s/^/#/g
2. 多視窗
寫程式碼過程中要開啟多個視窗,如果反復關閉開啟會很麻煩,vim可以使用多視窗
直接開啟多個檔:
# 縱向分割視窗
vim -On file1 file2 ...
# 橫向分割視窗
vim -on file1 file2 ...
已經開啟一個檔之後再大開一個檔:
# 可以開啟本地檔
:e file
# 也可以開啟遠端檔
:e ftp://host/*
多視窗切換檔:
下一個檔:bn
上一個檔:bp
在另一視窗分割開啟檔:
:split file
多視窗之間的切換:
# 依次切換
ctrl+w+w # 雙擊w
# 指定方向切換
先ctrl+w,按上下左右鍵選擇
調整視窗尺寸:
# 增大或減小視窗寬度
ctrl+w, <\> # 也可以ctrl+w, n,<\> 其中n是增大或減小的n行
# 增大或減小視窗高度
ctrl+w, +\- # 也可以ctrl+w, n,+\- 其中n是增大或減小的n列
3. 尋找與替換
# 尋找字串
/str
# 替換
# 替換當前行,其中g是替換標誌,代表global的意思,也可以換成c:需要確認;i:大小寫不敏感;I:大小寫敏感
:s/old/new/g
# 全域替換
:%s/old/new/g
# 替換特定行
:m,ns/old/new/g
# 選擇區域替換
:'<,'>s/old/new/g #先visual模式下選擇要替換的區域
# 詳細內容可以存取下面連結
4. 與shell互動
臨時結束:ctrl+z
回到vim:fg
執行shell命令:
# 命令模式下
:! ls ./
5. 插入
# 在當前行首插入
I # 也就是shift+i
# 在當前位置行尾插入
A
# 在當前字元所在位置插入
i
# 在當前字元之後插入
a
# 在當前行的前一行插入
O # shift+ o
# 在當前行的後一行插入
o
6. 撤銷和重做
# 撤銷操作
u
# 撤銷對整行的操作
U
# 重做(把撤銷的動作撤銷)
ctrl+r
7. 移動
# 移動到文本頭部
gg
# 移動到尾部
G #Windows下同shift+g
# 向前移動一個單詞
w # 2w移動兩個單詞
# 向後移動一個單詞
b
# 移動到行首
0
# 移動到行尾
$
# 移動到段落頭部
(
# 移動到段落尾部
)
# 將當前行移動到螢幕中間
zz
# 到螢幕尾行
L
# 到螢幕中間
M
# 到螢幕首行
H
8. 對比差異
這一點和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
工欲善其事必先利其器,無論是做Python還是c++開發,如果有一個好的IDE,那麽效率將會得到大大的提升,如果想把vim用於輕量級IDE,那麽外掛程式是必不可少的,以下以vim配置Python3為例。
1. 檢查vim版本是否符合
vim --version
2. 安裝Vundle外掛程式管理器
個人認為Vundle 就如同maven之於Java、npm之於js、pip之於Python,安裝了Vundle後方便後續安裝外掛程式。
首先複制到指定目錄:
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
其次,在~/.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
3. 安裝外掛程式
首推 YouCompleteMe ,這個外掛程式用於自動補全,速度補全速度可以達到pycharm的級別,但是這個外掛程式安裝十分麻煩,可以參考我的另外一篇文章:
另一篇文章連結如下:然後在~/.vimrc中添加
Plugin 'Valloric/YouCompleteMe'
保存結束重新開啟,在命令模式下輸入: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