1、ci" (由 change operator 和 text-object i" 組成)
這個命令會找到當前光標所在行的下一個 " 括起來的字符串,清除引號裏面的內容,並進入 insert mode 以方便修改
用起來比解釋起來簡單,你可用 const char* hello = "Hello world."; 類似這樣的代碼來測試
2、yaB (由 yank operator 和 text-object aB 組成)
這個命令會將當前光標所在的代碼塊({} 括起來的內容,包括花括號本身)復制到默認 register,
方便在其他地方用 p 貼出來(當然,寫代碼不鼓勵 copy and paste),你可用用任何一個 {} 來測試,然后找另外一個地方 p
3、gUiw (由 make uppercase operator 和 text-object iw 組成)
這個命令會將當前光標所在的 word 全部變成大寫
如將 max_size 變成 MAX_SIZE
4、進入 insert mode 編輯(進入 insert mode 別只用 i,要習慣視乎情況,使用 I a A o O s C)
5、對齊 gg=G 快速對齊整個文件
6、tabnew mytab 新建一個標簽mytab,:tabn 跳到下一個標簽,:tabp 跳到上一個標簽,用gt或gT在普通模式下跳轉到下一個和上一個標簽
7、切割窗口操作:split 水平方向切割一個窗口,:vsplit 豎直方向切割一個窗口,用Ctrl-w+方向鍵 在各個切割窗口之間切換。
8、宏操作
qa (將命令錄制到a這個剪貼板上{vim有好多個剪貼板,比如abcdefg……}) 3x (重復三遍刪除操作,將foo刪掉,其實也可以用f命令來搭配,對復雜的情況更適用) 0 (移動到行首) j (移動到下一行) q (停止錄制) 3@a (重復三遍剛才錄制的命令,3就是將命令重復3遍,@就是運行,a就是剛剛保存的那個剪貼板)
10、配置.vimrc
"=========================================================================
"Dessription: .vimrc文件,for Linux
"Create: 2015年08月02日 15時13分
"Email:1002643051@qq.com
"Author:Kevin
"=========================================================================
"歷史記錄數
set history=10000
"顯示行號
set number
"狀態列的行數,預設一行,建議設成兩行
set cmdheight=3
"保存全局變量,! 以大寫字母開頭的全局變數,並且不含有小寫字母
set viminfo+=!
"讓漢字以2個寬度顯示
set ambiwidth=double
"Fill space between windows 在被分割的窗口間顯示空白,便於閱讀
set fillchars=stl:\ ,stlnc:\ ,vert:\
"顯示標尺 在狀態行上顯示光標所在位置的行號和列號
set ruler
set rulerformat=%20(%2*%<%f%=\ %m%r\ %3l\ %c\ %p%%%)
"在狀態列顯示目前的模式,命令行顯示vim當前模式
set showmode
"輸入的命令顯示出來,看的清楚些
set showcmd
"光標移動到buffer的頂部和底部時保持3行距離
set scrolloff=3
"狀態行顯示的內容
set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")}
"啟動顯示狀態行(1),總是顯示狀態行(2)
set laststatus=2
"去掉討厭的有關vi一致性模式,避免以前版本的一些bug和局限
set nocompatible
"啟動的時候不顯示那個援助索馬里兒童的提示
set shortmess=atI
"代碼補全
set completeopt=preview,menu
"設置當文件被改動時自動載入
set autoread
"自動把內容寫回文件: 如果文件被修改過,在每個 :next、:rewind、:last、:first、:previous、:stop、:suspend、:tag、:!、:make、CTRL-] 和 CTRL-^命令時進行;用 :buffer、CTRL-O、CTRL-I、'{A-Z0-9} 或 `{A-Z0-9} 命令轉到別的文件時亦然。
set autowrite
"設置魔術匹配控制,可以通過:h magic查看更詳細的幫助信息
set magic
"在處理未保存或只讀文件的時候,彈出確認
set confirm
"字符間插入的像素行數目
set linespace=0
"增強模式中的命令行自動完成操作
set wildmenu
"可以在buffer的任何地方使用鼠標(類似office中在工作區雙擊鼠標定位)
set mouse=a
set selection=exclusive
set selectmode=mouse,key
"通過使用: commands命令,告訴我們文件的哪一行被改變過
set report=0
"不讓vim發出討厭的滴滴聲
set noerrorbells
"在執行宏命令時,不進行顯示重繪;在宏命令執行完成后,一次性重繪,以便提高性能。
set lazyredraw
"===================語法======================================================
"打開語法高亮
syntax enable
"允許按指定主題進行語法高亮,而非默認高亮主題
syntax on
"設置顏色
colorscheme desert
set background=dark
color ron " desert torte
"用淺色高亮當前行
autocmd InsertEnter * se cul
"設置高亮顯示當前行
set cursorline
"開啟高亮光標列
set cursorcolumn
"使回格鍵(backspace)正常處理indent, eol, start等
set backspace=2
"允許backspace和光標鍵跨越行邊界
set whichwrap+=<,>,h,l
"===================Tab and indent===============================================
"不要用空格代替制表符
set expandtab
"在行和段開始處使用制表符
set smarttab
"統一縮進為4
set shiftwidth=4
set tabstop=4
set softtabstop=4
"設置自動縮排
set autoindent
"cindent對c語法的縮進更加智能靈活
set cindent
"比autoindent稍智能的自動縮進,在這種縮進模式中,每一行都和前一行有相同的縮進量 粘貼時候使用 set paste
set smartindent
"==============文件備份和編碼===========================================================
"不自動保存備份文件
set nobackup
"禁止生成臨時文件
set noswapfile
"自動切換當前目錄為當前文件所在的目錄
set autochdir
"設定默認解碼
set fileencodings=utf-8
set fileformats=unix,dos,mac
set fenc=utf-8
set fencs=utf-8,usc-bom,euc-jp,gb18030,gbk,gb2312,cp936
"====================Text search and replace=====================================
"高亮顯示匹配的括號
set showmatch
"匹配括號高亮的時間(單位是十分之一秒)
set matchtime=2
"高亮顯示查找搜索的值
set hlsearch
"在搜索時,輸入的詞句的逐字符高亮(類似firefox的搜索
set incsearch
"搜索時忽略大小寫,但在有一個或以上大寫字母時仍大小寫敏感
set ignorecase
"如果搜索模式包含大寫字符,不使用 'ignorecase' 選項。只有在輸入搜索模式並且打開 'ignorecase' 選項時才會使用
set smartcase
"搜索時,不循環搜索
set nowrapscan
"帶有如下符號的單詞不要被換行分割
set iskeyword+=_,$,@,%,#,-,.
set helplang=cn
set list listchars=tab:\|\ ,trail:.,extends:>,precedes:<
"狀態行顏色
highlight StatusLine guifg=SlateBlue guibg=Yellow " 狀態行顏色
highlight StatusLineNC guifg=Gray guibg=White " 狀態行顏色
"高亮字符,讓其不受100列限制
highlight OverLength ctermbg=red ctermfg=white guibg=red guifg=white
match OverLength '\%101v.*'
"與windows共享剪貼板
set clipboard+=unnamed
" 自動格式化
set formatoptions=tcrqn
"============CTags的設定 =============================================================
let Tlist_Ctags_Cmd = '/usr/bin/ctags'
"按照名稱排序
let Tlist_Sort_Type = "name"
"在右側顯示窗口
let Tlist_Use_Right_Window = 1
"壓縮方式
let Tlist_Compart_Format = 1
"如果只有一個buffer,kill窗口也kill掉buffer
let Tlist_Exist_OnlyWindow = 1
"不要關閉其他文件的tags
let Tlist_File_Fold_Auto_Close = 0
"不要顯示折疊樹
let Tlist_Enable_Fold_Column = 0
"不同時顯示多個文件的tag,只顯示當前文件的
let Tlist_Show_One_File = 1
"============Autocommands =============================================================
" 只在下列文件類型被偵測到的時候顯示行號,普通文本文件不顯示
if has("autocmd")
autocmd FileType xml,html,c,cs,java,perl,shell,bash,cpp,python,vim,php,ruby set number
autocmd FileType xml,html vmap <C-o> <ESC>'<i<!--<ESC>o<ESC>'>o-->
autocmd FileType java,c,cpp,cs vmap <C-o> <ESC>'<o/*<ESC>'>o*/
autocmd FileType html,text,php,vim,c,java,xml,bash,shell,perl,python setlocal textwidth=100
autocmd Filetype html,xml,xsl source $VIMRUNTIME/plugin/closetag.vim
\ if line("'\"") > 0 && line("'\"") <= line("{1}quot;) |
\ exe "normal g`\"" |
\ endif
endif " has("autocmd")
"用空格鍵來開關折疊
set foldenable
"set foldmethod=manual
set foldmethod=syntax
set foldlevel=100
nnoremap <space> @=((foldclosed(line('.')) < 0) ? 'zc' : 'zo')<CR>
"設置快捷方式 Set mapleader
let mapleader = ","
"快捷加載,當輸入",ss"時,加載 .vimrc 文件
map <silent> <leader>ss :source ~/.vimrc<cr>
"快速打開編輯 .vimrc配置文件--->",ee"
map <silent> <leader>ee :e ~/.vimrc<cr>
"快速保存文件--->",w"
map <silent> <leader>w :w<cr>
"快速保存並退出文件--->",w"
map <silent> <leader>wq :wq<cr>
map <silent> <F9> :TlistToggle<cr>
"使用前導鍵加w來實現加速文件保存,來代替:w!加回車
nmap w :w!<cr>
nmap z :x<cr>
"ctages映射
autocmd! bufwritepost .vimrc source ~/.vimrc
" 高亮顯示普通txt文件(需要txt.vim腳本)
au BufRead,BufNewFile * setfiletype txt
"============自動補全 =============================================================
:inoremap ( ()<ESC>i
:inoremap ) <c-r>=ClosePair(')')<cr>
:inoremap { {<CR>}<ESC>O
:inoremap } <c-r>=ClosePair('}')<cr>
:inoremap [ []<ESC>i
:inoremap ] <c-r>=ClosePair(']')<cr>
:inoremap " ""<ESC>i
:inoremap ' ''<ESC>i
function! ClosePair(char)
if getline('.')[col('.') - 1] == a:char
return "\<Right>"
else
return a:char
endif
endfunction
"偵測文件類型 載入文件類型插件
filetype plugin indent on
"關掉智能補全時的預覽窗口
set completeopt=longest,menu
CTags安裝
http://ctags.sourceforge.net/ tar zvxf ctags-5.6.tar.gz cd ctags-5.6 ./configure make make install ctags -R --c++-kinds=+p --fields=+iaS --extra=+q src
CTags插件下載
cd /usr/local/src wget http://www.vim.org/scripts/download_script.php?src_id=19574 mv download_script.php\?src_id\=19574 vimtag.zip unzip vimtag.zip cp doc plugin ~/.vim/
使用下面的命令生成幫助標簽(下面的操作在vim中進行)
:helptags ~/.vim/doc :help taglist.txt
使用vundle安裝插件
https://github.com/chenpingzhao/Vundle.vim.git ~/.vim/bundle/vundle
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()
" let Vundle manage Vundle
" required!
Bundle 'gmarik/vundle'
Bundle 'tpope/vim-fugitive'
Bundle 'Lokaltog/vim-easymotion'
Bundle 'rstacruz/sparkup', {'rtp': 'vim/'}
Bundle 'tpope/vim-rails.git'
Bundle 'L9'
Bundle 'FuzzyFinder'
Bundle 'git://git.wincent.com/command-t.git'
filetype plugin indent on " required!
"
" Brief help -- 此處后面都是vundle的使用命令
" :BundleList - list configured bundles
" :BundleInstall(!) - install(update) bundles
" :BundleSearch(!) foo - search(or refresh cache first) for foo
" :BundleClean(!) - confirm(or auto-approve) removal of unused bundles
"
" see :h vundle for more details or wiki for FAQ
" NOTE: comments after Bundle command are not allowed..
配置語法高亮
:BundleInstall
加入的行
set laststatus=2 set t_Co=256 let g:Powerline_symbols='unicode' set encoding=utf8 colorscheme desert
其他一些常用的命令
sudo !! //以 root 帳戶執行上一條命令
python -m SimpleHTTPServer //利用 Python 搭建一個簡單的 Web 服務器,可通過 http://$HOSTNAME:8000 訪問
:w !sudo tee % //在 Vim 中無需權限保存編輯的文件
cd - //更改到上一次訪問的目錄
^foo^bar //將上一條命令中的 foo 替換為 bar,並執行
cp filename{,.bak} //快速備份或復制文件
mtr google.com //traceroute + ping
!whatever:p //搜索命令歷史,但不執行
ssh-copy-id user@host //將 ssh keys 復制到 user@host 以啟用無密碼 SSH 登錄
ffmpeg -f x11grab -s wxga -r 25 -i :0.0 -sameq /tmp/out.mpg //把 Linux 桌面錄制為視頻
參考
http://easwy.com/blog/archives/advanced-vim-skills-omin-complete/comment-page-1/
http://www.vim.org/scripts/script.php?script_id=273
https://github.com/ma6174/vim
