一些vim技巧

前言

一直很喜歡vim這個超級好玩的編輯器,她可以讓程序員真正體會到編程的快樂,並且致力於通過快速,簡單的命令做更多的工作。因爲簡單,所以強大。開始學習的時候,學習曲線是略顯陡峭,但是一旦掌握了一些常用的命令,那就可以飛速的進行編輯了。在日常的學習使用中,總結了一些經驗,同時參考了一些別人使用的技巧,陸續貼出來,想要學習的同志可以看看,當然也方便自己不時回顧,呵呵。

 

vim的運行時配置

vim的運行時(runtime)配置文件是vim在運行時的所有行爲的模板,如果定製的比較順手,就會大大的提高自己的編輯速度。在*nix下,vim的配置放在.vimrc的文件中,帶.前綴的文件在*nix系統中通常是隱藏的。而在windows下,這個文件通常爲_vimrc,放在vim的安裝目錄下。

 

.vimrc文件 (2009-07-22) 這個就是我目前所用的vimrc,已經比較順手了。

" This is my _vimrc under windows platform   
" and it can be used on *nix too   
" all the difference of them is the font setting session   
" happy Viming, guys!   
" copyLeft (#) Abruzzi John   
  
set linebreak   " line break   
set nocompatible    " no compatible   
set history=400 " history   
set ruler   
set number  " line number   
set hlsearch    " highlight search   
set noincsearch " no in C search   
set expandtab   " expand table   
set t_vb= "close bell   
set foldmethod=marker   
set tabstop=4   " table step   
set shiftwidth=4       
set nobackup    " don't backup   
set smarttab    " smart table   
set smartindent " smart indent   
set autoindent  " auto indent   
set cindent "cindent   
set cursorline  " hightlight cursor line 高亮光標所在行   
  
" set the back space
set backspace=indent,eol,start "這行比較重要,剛接觸vim的朋友會發現有時候backspace鍵刪不了文字

colorscheme desert " color scheme   
  
let Tlist_Use_Right_Window=0    " for tag_list plugin only   
let Tlist_File_Fold_Auto_Close=1 " for tag_list plugin only   
  
let g:winManagerWindowLayout="FileExplorer|TagList" " for winmanager    
  
filetype plugin indent on   " filetype setting   
set completeopt=longest,menu    " for code complete   
  
" the following function is used for show the status bar on the buttom   
function! CurrectDir()   
    let curdir = substitute(getcwd(), "", "", "g")   
    return curdir   
endfunction   
set statusline=\ [File]\ %F%m%r%h\ %w\ \ [PWD]\ %r%{CurrectDir()}%h\ \ %=[Line]\ %l,%c\ %=\ %P   
  
" this is a setting of font   
if has("win32")   
    set guifont=Courier_New:h10:cANSI   
endif   
  
" make sure that syntax always on   
if exists("syntax_on")   
    syntax reset   
else   
    syntax on    
endif   
  

" java complete
if has("autocmd")
    autocmd Filetype java setlocal omnifunc=javacomplete#Complete
endif

""""""""""""""""""""""""""""""""""""""""""""""""""""""
let performance_mode=1

function! MySys()
    if has("win32")
        return "win32"
    elseif has("unix")
        return "unix"
    else
        return "mac"
    endif
endfunction

if MySys() == "unix" || MySys() == "mac"
    set shell=bash
else
    " set win32 shell
endif

" set auto read when file is changed from outside
if exists("&autoread")
    set autoread
endif

" enable the mouse
if exists("&mouse")
    set mouse=a
endif

" set mapleader
let mapleader=","
let g:mapleader=","

"fast saving
nmap <leader>x :xa!<cr>
nmap <leader>w :w!<cr>

"switch to current directory
map <leader>cd :cd %:p:h<cr>

" just for fun
map <F9> ggVGg?

" folding code
if exists("&foldenable")
    set fen
endif

if exists("&foldlevel")
    set fdl=0
endif

" tag list --
map <F3> :Tlist<cr>

"remove the windows ^M windows系統中常常可以看到文本中夾雜着^M這樣的控制字符,用此命令刪除之
noremap <leader>m :%s/"r//g<cr>

 

 

這個rc文件是在上次的基礎上加了一些前人的配置,比較適合自己的用法習慣,並大都有註釋,大家自己參考下。個人認爲,沒有什麼“史上最強”之類的配置,只有適合自己的配置,如此而已。

 

這裏有個大俠的vim tips,有興趣的可以看看,可以學到很多技巧的:http://www.rayninfo.co.uk/vimtips.html

 

好了,今天貼一下在vim中打開文件系統的文件,大小寫互相轉換等命令

 

---------------------------------------- 
" Exploring 前提是你安裝了vim的explorer插件,vim7.x自帶了這一插件,注意命令前面的冒號(:)

:e . : file explorer 
:Exp(lore) : file explorer note capital Ex 
:Sex(plore) : file explorer in split window 
:browse e : windows style browser 
:ls : list of buffers 
:cd .. : move to parent directory 
:args : list of files 
:args *.php : open list of files (you need this!) 
:lcd %:p:h : change to directory of current file 切換到當前文件夾

---------------------------------------- 

" Changing Case 
guu : lowercase line 將整行變成小寫
gUU : uppercase line 
Vu : lowercase line 
VU : uppercase line 
g~~ : flip case line 小寫變大寫,大寫變小寫
vEU : Upper Case Word 
vE~ : Flip Case Word 
ggguG : lowercase entire file 將整個文件變小寫

 

 

map 命令映射

map命令提供給用戶自定義鍵綁定的功能,這使得vim變成了一個高度可定製的編輯系統。比如你在別的編輯器中習慣使用CTRL+S進行保存,在vim中默認的行爲是 :w,你可能覺得不順手,那麼就可以將保存文件的命令進行mapping

 

imap <CTRL-S> :w<CR>

 其中i表示在插入模式。

 

我比較喜歡使用Function鍵作爲快捷鍵,比如:

 

imap <F7> <ESC>:w!<cr>
imap <F8> <ESC>:w!<cr>i

 

好了,這次就先說這麼多,如果文章太長,反而影響學習效率,如果有興趣,可以參考我以前的幾篇關於vim的文章:

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章