快速上手vim,終端執行vimtutor
,不用謝。
vim的在線游戲練習網站:http://vim-adventures.com/挺好玩的,要錢...
先曬張圖顯擺一下;😃
配置方式
臨時配置、永久配置、快捷鍵配置
臨時配置
直接在底行模式下輸入配置命令,比如顯示行號,底行模式執行set nu
就能顯示行號,退出后配置不再生效。
永久配置
將配置項寫到配置文件中,每次打開vim會自動配置。此配置文件不止一個,/usr/share/vim/.vimrc
、/etc/vim/vimrc
還有用戶配置文件~/.vimrc
,都行都行,底行模式執行h vimrc
能看到所有會搜索配置文件的地方。建議在~/.vimrc
或~/.vim/vimrc
添加用戶的配置,不會影響到其他用戶,也能保留root的樣子。比如顯示行號,創建~/.vimrc
文件,加上配置語句set nu
。
快捷鍵配置
基於永久配置方法,在配置文件中匹配快捷鍵。以顯示行號為例:
noremap <F6> :set nu
noremap <F7> :set nonu
按下F6鍵自動寫入<F6>
。使用時在命令模式下F6,自動在底行模式寫入命令set nu
,回車執行。
vim簡單配置
vim的幫助文檔里可以查看配置項:help vimrc-intro
。vim有很多幫助文檔,設置成中文學起來方便。
命令 | 功能 |
---|---|
set nu | 顯示行號 |
set nonu | 取消行號 |
set cul | 突出顯示當前行 |
set cuc | 突出顯示當前列 |
set showmatch | 括號匹配 |
set completeopt=preview,menu | 自動補全代碼 |
set cindent | 自動縮進 |
set tabstop=4 | 設置Tab鍵為4個空格 |
set shiftwidth=4 | 設置自動縮進長度為4空格 |
set autoindent | 繼承前一行的縮進方式,適用於多行注釋 |
set mouse=a | 在終端中使用鼠標 |
我的vim配置
系統配置,末尾追加了下面這兩行,為了隨時取消/顯示行號,考慮到復制的時候會把行號也復制上。
noremap <F6> :set nu
noremap <F7> :set nonu
用戶配置,我寫上注釋啦,但是有些忘記是什么意思了:
"set encoding=utf-8
set nu "顯示行號,已在系統配置里設置<F7>取消顯示,方便使用。
set nocp "不以兼容模式運行vi。
syntax enable "開啟語法加亮。
syntax on
set helplang=cn "設置中文文檔,英文en,中文cn。前提是已經調好了中文文檔。
set history=30 "命令記錄次數。
colorscheme pablo "配色風格。
set shortmess=atI "簡潔啟動模式。
set showcmd "在狀態欄顯示目前所執行的指令。
set cursorline "顯示當前行。
set hlsearch "高亮顯示搜索。
set ru "顯示標尺。
"設定 GUI 選項
"set guioptions=gmrLtT m:菜單 T:工具欄
"set guioptions=gmrLt "我忘記這兩行是干嘛的了...
"設置縮進
set tabstop=4 "將‘\t’轉換成4個空格。
set softtabstop=4 "為了按Backspace時將4個空格當作一個Tab。
set shiftwidth=4 "設置tab寬度。
set expandtab "用空格代替制表符。
set cindent "以c語言風格自動縮進。
set smartindent "自動識別以#開頭的注釋,不進行換行。
"set autoindent "配合下面一條命令根據不同語言類型進行不同的縮進操作。這個不太懂,注釋了。
"filetype plugin indent on
"設置命令行和狀態欄
set cmdheight=1 "命令行行數。
set laststatus=2 "顯示狀態欄。
set statusline=%F%m%r,%Y,%{&fileformat}\ \ %l,%c%V\ %p%%\ \ [\ %L\ lines\ in\ all\ ]\ \ ASCII=\%b,HEX=\%B
" 設置在狀態行顯示的信息如下:
" %F 當前文件名
" %m 當前文件修改狀態
" %r 當前文件是否只讀
" %Y 當前文件類型
" %{&fileformat}當前文件編碼
" %b 當前光標處字符的 ASCII 碼值
" %B 當前光標處字符的十六進制值
" %l 當前光標行號
" %c 當前光標列號
" %V 當前光標虛擬列號 (根據字符所占字節數計算)
" %p 當前行占總行數的百分比
" %% 百分號
" %L 當前文件總行數
" 狀態行顏色
highlight StatusLine guifg=SlateBlue guibg=Yellow
highlight StatusLineNC guifg=Gray guibg=White
setlocal noswapfile "不要生成隱藏文件。
"這一段忘了從哪看到的了,也不知道啥意思。
" When started as "evim", evim.vim will already have done these settings.
"if v:progname =~? "evim"
" finish
"endif
"以下是從vimtutor里復制來的。
" Get the defaults that most users want.
source $VIMRUNTIME/defaults.vim
if has("vms")
set nobackup " do not keep a backup file, use versions instead
else
set backup " keep a backup file (restore to previous version)
if has('persistent_undo')
set undofile " keep an undo file (undo changes after closing)
endif
endif
if &t_Co > 2 || has("gui_running")
" Switch on highlighting the last used search pattern.
set hlsearch
endif
" Only do this part when compiled with support for autocommands.
if has("autocmd")
" Put these in an autocmd group, so that we can delete them easily.
augroup vimrcEx
au!
" For all text files set 'textwidth' to 78 characters.
autocmd FileType text setlocal textwidth=78
augroup END
else
set autoindent " always set autoindenting on
endif " has("autocmd")
" Add optional packages.
"
" The matchit plugin makes the % command work better, but it is not backwards
" compatible.
" The ! means the package won't be loaded right away but when plugins are
" loaded during initialization.
if has('syntax') && has('eval')
packadd! matchit
endif
其他
vim打開文件小技巧
如果知道要編輯文件的行數,比如說/etc/passwd文件的第18行,就可以使用VIM精確定位所打開文件sudo vim +18 /etc/passwd
。
VIM一次性可以打開兩個文件,比如/etc/passwd和/etc/group,使用Ctrl+W組合鍵切換到另外一個窗口sudo vim -O /etc/passwd /etc/group
。
vim的幾個插件:
- MiniBufExplorer緩存管理器:http://www.vim.org/scripts/script.php?script_id=159。
- Ctags標簽工具:http://www.vim.org/scripts/script.php?script_id=610。
- VisualMark高亮書簽(類似UltraEdit書簽功能):http://www.vim.org/scripts/script. php?script_id=1026。
- SuperTab補全插件:http://www.vim.org/scripts/script.php?script_id=182。
- Python開發插件:http://www.vim.org/scripts/script.php?script_id=790。
- C/C++開發插件:http://www.vim.org/scripts/script.php?script_id=213。
- PHP開發插件:http://www.vim.org/scripts/script.php?script_id=1571。
vim做成IDE方法:
- https://github.com/HenryHo2015/maximum-awesome。
- https://github.com/HenryHo2015/Vundle.vim。
- https://github.com/HenryHo2015/oh-my-zsh
參考列表
《完美應用Ubuntu(第3版)》-何曉龍-電子工業出版社-2017/01
https://segmentfault.com/a/1190000016330314
https://blog.csdn.net/LSG_Down/article/details/89319472