一些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的文章:

 

 

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