程序員的效率--專屬vim(一)

專題介紹

一臺配置強大的電腦,一個巨大的顯示屏,一個順手的編輯器,飛快的網速,能夠說是高效工做的必備條件.自命一個搞技術的,深知時間的寶貴,效率能夠說是一我的的能力評價的評介標準.硬件條件只是錢的問題,而軟件的配置則是我的素養問題.
此處本意是打造一個專題,這裏單講編輯器vimphp


打造專屬vim

vim的重要性沒必要費話.網上有幾個不錯的vim配置方案,特別在github上,做者已經簡化到只出幾個命令的地步,有kvim,wklken, easwy等,但這些有很大的針對性,不可照搬不誤,由於好多插件和配置,若是不明就裏,反而會拔苗助長.這就要求對配置和插件作到耳熟能詳,只在身體力行,運用才能遊刃有餘.html

此博文長期更新,以備不時之需,亦可備份待查java

配置vimrc

先上一個最基本的配置代碼python

"==========================================
" Author:  vintion  
" Version: 1.0
" Email: niwenxianq@qq.com
" BlogPost: http://cnblog/vintion
" Last_modify: 2014-11-24
" ====================================================

" 修改leader鍵
let mapleader = ','
let g:mapleader = ','

" 開啓語法高亮
syntax on

"==========================================
" General Settings 基礎設置
"==========================================

"set guifont=Monaco:h20          " 字體 && 字號

" history存儲容量
set history=2000

"檢測文件類型
filetype on

"set tags
":set tags=tags

"針對不一樣的文件類型採用不一樣的縮進格式
filetype indent on
"容許插件
filetype plugin on
"啓動自動補全
filetype plugin indent on

set autoread          " 文件修改以後自動載入。
set shortmess=atI       " 啓動的時候不顯示那個援助索馬里兒童的提示

" 備份,到另外一個位置. 防止誤刪, 目前是取消備份
"set backup
"set backupext=.bak
"set backupdir=/tmp/vimbk/

" 取消備份。 視狀況本身改
set nobackup
" 關閉交換文件
set noswapfile

"create undo file
if has('persistent_undo')
  set undolevels=1000         " How many undos
  set undoreload=10000        " number of lines to save for undo
  set undofile                " So is persistent undo ...
  set undodir=/tmp/vimundo/
endif

set wildignore=*.swp,*.bak,*.pyc,*.class,.svn

" 突出顯示當前行等
set cursorcolumn
set cursorline          " 突出顯示當前行


"設置 退出vim後,內容顯示在終端屏幕, 能夠用於查看和複製
"好處:誤刪什麼的,若是之前屏幕打開,能夠找回
set t_ti= t_te=


" 能夠在buffer的任何地方使用鼠標(相似office中在工做區雙擊鼠標定位)
 set mouse=a
"set mouse-=a             " 鼠標暫不啓用, 鍵盤黨....
set selection=exclusive
set selectmode=mouse,key

" 修復ctrl+m 多光標操做選擇的bug,可是改變了ctrl+v進行字符選中時將包含光標下的字符
"set selection=exclusive
set selection=inclusive
set selectmode=mouse,key

" No annoying sound on errors
" 去掉輸入錯誤的提示聲音
set title                " change the terminal's title
"不要閃爍
set novisualbell         " don't beep
set noerrorbells         " don't beep
set t_vb=
set tm=500

" Remember info about open buffers on close"
set viminfo^=%

" For regular expressions turn magic on
set magic

" Configure backspace so it acts as it should act
set backspace=eol,start,indent
set whichwrap+=<,>,h,l

"==========================================
" Display Settings 展現/排版等界面格式設置
"==========================================
"
"顯示當前的行號列號:
set ruler
set rulerformat=%20(%2*%<%f%=\ %m%r\ %3l\ %c\ %p%%%)

" 命令行(在狀態行下)的高度,默認爲1
 set cmdheight=1

""在狀態欄顯示正在輸入的命令
set showcmd
" 左下角顯示當前vim模式
set showmode

" 在上下移動光標時,光標的上方或下方至少會保留顯示的行數
set scrolloff=7
" 狀態行顏色
highlight StatusLine guifg=SlateBlue guibg=Yellow
highlight StatusLineNC guifg=Gray guibg=White

" set winwidth=79

" 命令行(在狀態行下)的高度,默認爲1,這裏是2
"set statusline=%<%f\ %h%m%r%=%k[%{(&fenc==\"\")?&enc:&fenc}%{(&bomb?\",BOM\":\"\")}]\ %-14.(%l,%c%V%)\ %P
" Always show the status line - use 2 lines for the status bar
set laststatus=2
set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")}

"顯示行號:
set number
" 取消換行。
set nowrap

" 括號配對狀況,跳轉並高亮一下匹配的括號
set showmatch
" How many tenths of a second to blink when matching brackets
set matchtime=2

"設置文內智能搜索提示
" 高亮search命中的文本。
set hlsearch
" 打開增量搜索模式,隨着鍵入即時搜索,逐字高亮
set incsearch
" 搜索時忽略大小寫
set ignorecase
" 有一個或以上大寫字母時仍大小寫敏感
set smartcase     " ignore case if search pattern is all lowercase, case-sensitive otherwise

" 代碼摺疊
set foldenable
" 摺疊方法
" manual    手工摺疊
" indent    使用縮進表示摺疊
" expr      使用表達式定義摺疊
" syntax    使用語法定義摺疊
" diff      對沒有更改的文本進行摺疊
" marker    使用標記進行摺疊, 默認標記是 {{{ 和 }}}
set foldmethod=indent
set foldlevel=99

" 縮進配置

set smartindent   " Smart indent
set cindent "C樣式縮進
set autoindent    " 打開自動縮進,繼承前一行的縮進
" never add copyindent, case error   " copy the previous indentation on autoindenting

" tab相關變動
set tabstop=4     " 設置Tab鍵的寬度        [等同的空格個數]
set shiftwidth=4  " 每一次縮進對應的空格數
set softtabstop=4 " 按退格鍵時能夠一次刪掉 4 個空格
set smarttab      " insert tabs on the start of a line according to shiftwidth, not tabstop 按退格鍵時能夠一次刪掉 4 個空格
set expandtab     " 將Tab自動轉化成空格    [須要輸入真正的Tab鍵時,使用 Ctrl+V + Tab]
set shiftround    " 縮進時,取整 use multiple of shiftwidth when indenting with '<' and '>'
set wrap "換行,nowrap是不換行
" A buffer becomes hidden when it is abandoned
set hidden
set wildmode=list:longest
set ttyfast

" 00x增減數字時使用十進制
set nrformats=


" 相對行號      行號變成相對,能夠用 nj  nk   進行跳轉 5j   5k 上下跳5行
set relativenumber number
au FocusLost * :set norelativenumber number
au FocusGained * :set relativenumber
" 插入模式下用絕對行號, 普通模式下用相對
autocmd InsertEnter * :set norelativenumber number
autocmd InsertLeave * :set relativenumber
function! NumberToggle()
  if(&relativenumber == 1)
    set norelativenumber number
  else
    set relativenumber
  endif
endfunc
nnoremap <C-n> :call NumberToggle()<cr>


"==========================================
" FileEncode Settings 文件編碼,格式
"==========================================
" 設置新文件的編碼爲 UTF-8
set encoding=utf-8
" 自動判斷編碼時,依次嘗試如下編碼:
set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1
set helplang=cn
"set langmenu=zh_CN.UTF-8
"set enc=2byte-gb18030
" 下面這句隻影響普通模式 (非圖形界面) 下的 Vim。
set termencoding=utf-8

" Use Unix as the standard file type
set ffs=unix,dos,mac

" 如遇Unicode值大於255的文本,沒必要等到空格再折行。
"自動格式化
"set formatoptions+=m
set formatoptions=tcrqn
" 合併兩行中文時,不在中間加空格:
set formatoptions+=B


"==========================================
" others 其它設置
"==========================================
autocmd! bufwritepost _vimrc source % " vimrc文件修改以後自動加載。 windows。
autocmd! bufwritepost .vimrc source % " vimrc文件修改以後自動加載。 linux。

" 自動補全配置
"讓Vim的補全菜單行爲與通常IDE一致(參考VimTip1228)
set completeopt=longest,menu

" 加強模式中的命令行自動完成操做
set wildmenu
" Ignore compiled files
set wildignore=*.o,*~,*.pyc,*.class

"離開插入模式後自動關閉預覽窗口
autocmd InsertLeave * if pumvisible() == 0|pclose|endif
"回車即選中當前項
inoremap <expr> <CR>       pumvisible() ? "\<C-y>" : "\<CR>"

"上下左右鍵的行爲 會顯示其餘信息
inoremap <expr> <Down>     pumvisible() ? "\<C-n>" : "\<Down>"
inoremap <expr> <Up>       pumvisible() ? "\<C-p>" : "\<Up>"
inoremap <expr> <PageDown> pumvisible() ? "\<PageDown>\<C-p>\<C-n>" : "\<PageDown>"
inoremap <expr> <PageUp>   pumvisible() ? "\<PageUp>\<C-p>\<C-n>" : "\<PageUp>"

" if this not work ,make sure .viminfo is writable for you
if has("autocmd")
  au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
endif

"==========================================
" HotKey Settings  自定義快捷鍵設置
"==========================================

" 主要按鍵重定義

" 關閉方向鍵, 強迫本身用 hjkl
map <Left> <Nop>
map <Right> <Nop>
map <Up> <Nop>
map <Down> <Nop>

"Treat long lines as break lines (useful when moving around in them)
"se swap以後,同物理行上線直接跳
nnoremap k gk
nnoremap gk k
nnoremap j gj
nnoremap gj j

" F1 - F6 設置
" F1 廢棄這個鍵,防止調出系統幫助
" F2 行號開關,用於鼠標複製代碼用
" F3 顯示可打印字符開關
" F4 換行開關
" F5 粘貼模式paste_mode開關,用於有格式的代碼粘貼
" F6 語法開關,關閉語法能夠加快大文件的展現

" I can type :help on my own, thanks.  Protect your fat fingers from the evils of <F1>
noremap <F1> <Esc>"

""爲方便複製,用<F2>開啓/關閉行號顯示:
function! HideNumber()
  if(&relativenumber == &number)
    set relativenumber! number!
  elseif(&number)
    set number!
  else
    set relativenumber!
  endif
  set number?
endfunc
nnoremap <F2> :call HideNumber()<CR>
nnoremap <F3> :set list! list?<CR>
nnoremap <F4> :set wrap! wrap?<CR>
              "set paste
set pastetoggle=<F5>            "    when in insert mode, press <F5> to go to
                                "    paste mode, where you can paste mass data
                                "    that won't be autoindented

" disbale paste mode when leaving insert mode
au InsertLeave * set nopaste

nnoremap <F6> :exec exists('syntax_on') ? 'syn off' : 'syn on'<CR>


"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
noremap H ^
noremap L $


"Map ; to : and save a million keystrokes
" ex mode commands made easy 用於快速進入命令行
nnoremap ; :


" 命令行模式加強,ctrl - a到行首, -e 到行尾
cnoremap <C-j> <t_kd>
cnoremap <C-k> <t_ku>
cnoremap <C-a> <Home>
cnoremap <C-e> <End>

" 搜索相關

" Map <Space> to / (search) and Ctrl-<Space> to ? (backwards search)
map <space> /
" 進入搜索Use sane regexes"
nnoremap / /\v
vnoremap / /\v

"Keep search pattern at the center of the screen."
nnoremap <silent> n nzz
nnoremap <silent> N Nzz
nnoremap <silent> * *zz
nnoremap <silent> # #zz
nnoremap <silent> g* g*zz

" switch # *
nnoremap # *
nnoremap * #

" 去掉搜索高亮
"noremap <silent><leader>/ :nohls<CR>
set nohlsearch

" --------tab/buffer相關

"Use arrow key to change buffer"
" TODO: 如何跳轉到肯定的buffer?
" :b1 :b2   :bf :bl
nnoremap [b :bprevious<cr>
nnoremap ]b :bnext<cr>
noremap <left> :bp<CR>
noremap <right> :bn<CR>


" tab 操做
" TODO: ctrl + n 變成切換tab的方法
"map <C-2> 2gt
map <leader>th :tabfirst<cr>
map <leader>tl :tablast<cr>

map <leader>tj :tabnext<cr>
map <leader>tk :tabprev<cr>
map <leader>tn :tabnext<cr>
map <leader>tp :tabprev<cr>

map <leader>te :tabedit<cr>
map <leader>td :tabclose<cr>
map <leader>tm :tabm<cr>


" 新建tab  Ctrl+t
nnoremap <C-t>     :tabnew<CR>
inoremap <C-t>     <Esc>:tabnew<CR>
" TODO: 配置成功這裏, 切換更方便, 兩個鍵
" nnoremap <C-S-tab> :tabprevious<CR>
" nnoremap <C-tab>   :tabnext<CR>
" inoremap <C-S-tab> <Esc>:tabprevious<CR>i
" inoremap <C-tab>   <Esc>:tabnext<CR>i
" nnoremap <C-Left> :tabprevious<CR>
" nnoremap <C-Right> :tabnext<CR>

" normal模式下切換到確切的tab
noremap <leader>1 1gt
noremap <leader>2 2gt
noremap <leader>3 3gt
noremap <leader>4 4gt
noremap <leader>5 5gt
noremap <leader>6 6gt
noremap <leader>7 7gt
noremap <leader>8 8gt
noremap <leader>9 9gt
noremap <leader>0 :tablast<cr>

" Toggles between the active and last active tab "
" The first tab is always 1 "
let g:last_active_tab = 1
nnoremap <leader>gt :execute 'tabnext ' . g:last_active_tab<cr>
nnoremap <silent> <c-l> :execute 'tabnext ' . g:last_active_tab<cr>
vnoremap <silent> <c-l> :execute 'tabnext ' . g:last_active_tab<cr>
autocmd TabLeave * let g:last_active_tab = tabpagenr()


" ------- 選中及操做改鍵

"Reselect visual block after indent/outdent.調整縮進後自動選中,方便再次操做
vnoremap < <gv
    vnoremap > >gv

" y$ -> Y Make Y behave like other capitals
map Y y$

" select all
map <Leader>sa ggVG"
"map <Leader>as gg"+yG
" select block
nnoremap <leader>v V`}

" w!! to sudo & write a file
cmap w!! w !sudo tee >/dev/null %

" kj 替換 Esc
inoremap kj <Esc>

" 滾動Speed up scrolling of the viewport slightly
nnoremap <C-e> 2<C-e>
nnoremap <C-y> 2<C-y>


"Jump to start and end of line using the home row keys
" 加強tab操做, 致使這個會有問題, 考慮換鍵
"nmap t o<ESC>k
"nmap T O<ESC>j

" Quickly close the current window
nnoremap <leader>q :q<CR>

" Swap implementations of ` and ' jump to markers
" By default, ' jumps to the marked line, ` jumps to the marked line and
" column, so swap them
nnoremap ' `
nnoremap ` '

" remap U to <C-r> for easier redo
nnoremap U <C-r>

" Quickly edit/reload the vimrc file
nmap <silent> <leader>ev :e $MYVIMRC<CR>
nmap <silent> <leader>sv :so $MYVIMRC<CR>

"==========================================
" FileType Settings  文件類型設置
"==========================================

" Python 文件的通常設置,好比不要 tab 等
autocmd FileType python set tabstop=4 shiftwidth=4 expandtab ai

" 保存python文件時刪除多餘空格
fun! <SID>StripTrailingWhitespaces()
    let l = line(".")
    let c = col(".")
    %s/\s\+$//e
    call cursor(l, c)
endfun
autocmd FileType c,cpp,java,php,ruby,python autocmd BufWritePre <buffer> :call <SID>StripTrailingWhitespaces()


" 定義函數AutoSetFileHead,自動插入文件頭
autocmd BufNewFile *.sh,*.py exec ":call AutoSetFileHead()"
function! AutoSetFileHead()
    "若是文件類型爲.sh文件
    if &filetype == 'sh'
        call setline(1, "\#!/bin/bash")
    endif

    "若是文件類型爲python
    if &filetype == 'python'
        call setline(1, "\#!/usr/bin/env python")
        call append(1, "\# encoding: utf-8")
    endif

    normal G
    normal o
    normal o
endfunc

" F10 to run python script
nnoremap <buffer> <F10> :exec '!python' shellescape(@%, 1)<cr>

"F5: C/C++的編譯和運行
map <F5> :call CompileRunGpp()<CR>
func! CompileRunGpp()
exec "w"
exec "!g++ % -o %<"
exec "! ./%<"
endfunc

"==========================================
" Theme Settings  主題設置
"==========================================

" Set extra options when running in GUI mode
if has("gui_running")
    set guifont=Monaco:h14
    if has("gui_gtk2")   "GTK2
        set guifont=Monaco\ 12, Monospace\ 12
    endif
    set guioptions-=T
    set guioptions+=e
    set guioptions-=r
    set guioptions-=L
    set guitablabel=%M\ %t
    set showtabline=1
    set linespace=2
    set noimd
    set t_Co=256
endif

" theme主題
set background=dark
"colorscheme solarized
set t_Co=256

"colorscheme molokai
"colorscheme desert

"設置標記一列的背景顏色和數字一行顏色一致
hi! link SignColumn   LineNr
hi! link ShowMarksHLl DiffAdd
hi! link ShowMarksHLu DiffChange

"" for error highlight,防止錯誤整行標紅致使看不清
highlight clear SpellBad
highlight SpellBad term=standout ctermfg=1 term=underline cterm=underline
highlight clear SpellCap
highlight SpellCap term=underline cterm=underline
highlight clear SpellRare
highlight SpellRare term=underline cterm=underline
highlight clear SpellLocal
highlight SpellLocal term=underline cterm=underline

""""""""""""""""""""""""""""""
" Tag list (ctags)
""""""""""""""""""""""""""""""
nmap <silent> <F9> <ESC>:Tlist<RETURN>
nmap tl :Tlist<cr> 
let Tlist_Show_One_File = 1 "不一樣時顯示多個文件的tag,只顯示當前文件的 
let Tlist_File_Fold_Auto_Close = 1
let Tlist_Exit_OnlyWindow = 1 "若是taglist窗口是最後一個窗口,則退出vim 
let Tlist_Use_Right_Window = 1         "在右側窗口中顯示taglist窗口 
" minibufexpl插件的通常設置
let g:miniBufExplMapWindowNavVim = 1
let g:miniBufExplMapWindowNavArrows = 1
let g:miniBufExplMapCTabSwitchBufs = 1
let g:miniBufExplModSelTarget = 1  


"********************************************************
"                      NERD_Tree 配置                   *
"********************************************************

"顯示加強
let NERDChristmasTree=1

"自動調整焦點
let NERDTreeAutoCenter=1

"鼠標模式:目錄單擊,文件雙擊
let NERDTreeMouseMode=2

"打開文件後自動關閉
let NERDTreeQuitOnOpen=1

"顯示文件
let NERDTreeShowFiles=1

"顯示隱藏文件
let NERDTreeShowHidden=1

"高亮顯示當前文件或目錄
let NERDTreeHightCursorline=1

"顯示行號
let NERDTreeShowLineNumbers=1

"窗口位置
let NERDTreeWinPos='left'

"窗口寬度
let NERDTreeWinSize=31

"不顯示'Bookmarks' label 'Press ? for help'
let NERDTreeMinimalUI=1
nnoremap <silent> <F4> :NERDTreeToggle<CR>
"快捷鍵
"nnoremap <silent> <F4> :NERDTreeToggle<CR>
""map <F4> :silent! NERDTreeToggle<CR>
""let NERDTreeWinPos="right" "窗口在右側
"當打開NERDTree時,自動顯示bookmark
""let NERDTreeShowBookmarks=1


"winManagerWinodw
let g:winManagerWindowLayout='FileExplorer|TagList'
nmap wm :WMToggle<cr>

"括號補全
: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


"*********************************************************
"                  vundle 配置                           *
"*********************************************************

set rtp+=~/.vim/bundle/Vundle.vim

call vundle#begin()

" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'


" My Plugins is here
Plugin 'tpope/vim-fugitive'
Plugin 'L9'
Plugin 'a.vim'
Plugin 'Auto-Pairs'
Plugin 'vim-scripts/taglist.vim'
Plugin 'The-NERD-tree'
Plugin 'Syntastic'
Plugin 'FuzzyFinder'
Plugin 'Lokaltog/vim-powerline'
Plugin 'Valloric/YouCompleteMe' 

call vundle#end()
"啓動智能補全
filetype plugin indent on
"*****************************************************
"                   taglist配置                      *
"*****************************************************

"不顯示"press F1 to display help"
let Tlist_Compact_Format=1

"窗口在右側顯示
let Tlist_Use_Right_Window=1

"只顯示當前文件的tags
let Tlist_Show_One_File=1  

"高亮顯示
let Tlist_Auto_Highlight_tag=1

"隨文件自動更新
let Tlist_Auto_Update=1

"設置寬度
let Tlist_WinWidth=30       

"taglist窗口是最後一個窗口,則退出vim
let Tlist_Exit_OnlyWindow=1 

"單擊跳轉
let Tlist_Use_SingClick=1

"打開關閉快捷鍵
nnoremap <silent> <F8> :TlistToggle<CR>

"*****************************************************
"           YouCompleteMe配置                        *
"*****************************************************

"leader映射爲逗號「,」
let mapleader = ","  

"配置默認的ycm_extra_conf.py
let g:ycm_global_ycm_extra_conf = '~/.vim/bundle/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py' 
 
"按,jd 會跳轉到定義
nnoremap <leader>jd :YcmCompleter GoToDefinitionElseDeclaration<CR>   

"打開vim時再也不詢問是否加載ycm_extra_conf.py配置
let g:ycm_confirm_extra_conf=0   

"使用ctags生成的tags文件
let g:ycm_collect_identifiers_from_tag_files = 1 


"*****************************************************
"           Syntastic配置                            *
"*****************************************************


let g:Syntastic_check_on_open=1

更新...linux

相關文章
相關標籤/搜索