Chinaunix首页 | 论坛 | 博客
  • 博客访问: 850307
  • 博文数量: 190
  • 博客积分: 7021
  • 博客等级: 少将
  • 技术积分: 1752
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-17 19:26
文章分类

全部博文(190)

文章存档

2014年(9)

2011年(32)

2010年(149)

我的朋友

分类: LINUX

2010-06-30 15:43:31

在终端下使用vim进行编辑时,默认情况下,编辑的界面上是没有显示行号、语法高亮度显示、智能缩进等功能的。为了更好的在vim下进行工作,需要手动设置一个配置文件:.vimrc。 

在启动vim时,当前用户根目录下的.vimrc文件会被自动读取,该文件可以包含一些设置甚至脚本,所以,一般情况下把.vimrc文件创建在当前用户的根目录下比较方便,即创建的命令为: 

$vi ~/.vimrc 

设置完后 

$:x 或者 $wq 

进行保存退出即可。 

下面给出一个例子,其中列出了经常用到的设置,详细的设置信息请参照参考资料: 

“双引号开始的行为注释行,下同 

“去掉讨厌的有关vi一致性模式,避免以前版本的一些bug和局限 

set nocompatible 

“显示行号 

set nummber 

“检测文件的类型 

filetype on 

“记录历史的行数 

set history=1000 

“背景使用黑色 

set background=dark 

“语法高亮度显示 

syntax on 



“下面两行在进行编写代码时,在格式对起上很有用; 

“第一行,vim使用自动对起,也就是把当前行的对起格式应用到下一行; 

“第二行,依据上面的对起格式,智能的选择对起方式,对于类似C语言编 

“写上很有用 

set autoindent 
set smartindent 

“第一行设置tab键为4个空格,第二行设置当行之间交错时使用4个空格 

set tabstop=4 
set shiftwidth=4 

“设置匹配模式,类似当输入一个左括号时会匹配相应的那个右括号 

set showmatch 

“去除vim的GUI版本中的toolbar 

set guioptions-=T 

“当vim进行编辑时,如果命令错误,会发出一个响声,该设置去掉响声 

set vb t_vb= 

“在编辑过程中,在右下角显示光标位置的状态行 

set ruler 

“默认情况下,寻找匹配是高亮度显示的,该设置关闭高亮显示 

set nohls 

“查询时非常方便,如要查找book单词,当输入到/b时,会自动找到第一 

“个b开头的单词,当输入到/bo时,会自动找到第一个bo开头的单词,依 

“次类推,进行查找时,使用此设置会快速找到答案,当你找要匹配的单词 

“时,别忘记回车 

set incsearch 

“修改一个文件后,自动进行备份,备份的文件名为原文件名加“~“后缀 

if has(“vms”) 

set nobackup 

else 

set backup 

endif 



如果去除注释后,一个完整的.vimrc配置信息如下所示: 



set nocompatible 

set nummber 

filetype on 

set history=1000 

set background=dark 

syntax on 

set autoindent 
set smartindent 

set tabstop=4 
set shiftwidth=4 

set showmatch 

set guioptions-=T 

set vb t_vb= 

set ruler 

set nohls

set mouse=a

set cindent 

set incsearch 

#if has(“vms”) 

#set nobackup 

#else 

#set backup 

#endif
下面给出网上的“史上最强vim配置”,我暂时还没有去试,编辑我只要使用一些简单的功能就能满足要求,有兴趣的可以去试试

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
Maintainer: amix the lucky stiff
" - amix@amix.dk
"

" Version: 3.3 - 21/01/10 01:05:46
"

" Blog_post:
"
http://amix.dk/blog/post/19486#The-ultimate-vim-configuration-vimrc
" Syntax_highlighted:
"
http://amix.dk/vim/vimrc.html
" Raw_version:
"
http://amix.dk/vim/vimrc.txt
"
"
How_to_Install:
" $ mkdir ~/.vim_runtime
"
$ svn co svn://orangoo.com/vim ~/.vim_runtime
" $ cat ~/.vim_runtime/install.sh
"
$ sh ~/.vim_runtime/install.sh <system>
" can be `mac`, `linux` or `windows`
"

" How_to_Upgrade:
"
$ svn update ~/.vim_runtime
"
"
Sections:
" -> General
"
-> VIM user interface
" -> Colors and Fonts
"
-> Files and backups
" -> Text, tab and indent related
"
-> Visual mode related
" -> Command mode related
"
-> Moving around, tabs and buffers
" -> Statusline
"
-> Parenthesis/bracket expanding
" -> General Abbrevs
"
-> Editing mappings
"
"
-> Cope
" -> Minibuffer plugin
"
-> Omni complete functions
" -> Python section
"
-> JavaScript section
"
"
Plugins_Included:
" > minibufexpl.vim -
"
Makes it easy to get an overview of buffers:
" info -> :e ~/.vim_runtime/plugin/minibufexpl.vim
"

" > bufexplorer -
"
Makes it easy to switch between buffers:
" info -> :help bufExplorer
"

" > yankring.vim -
"
Emacs's killring, useful when using the clipboard:
" info -> :help yankring
"
" > surround.vim -
" Makes it easy to work with surrounding text:
" info -> :help surround
"
" > snipMate.vim -
" Snippets for many languages (similar to TextMate'
s):
" info -> :help snipMate
"

" > fuzzyfinder -
"
Find files fast (similar to TextMate's feature):
" info -> :help fuzzyfinder@en
"
" Revisions:
" > 3.3: Added syntax highlighting for Mako mako.vim
" > 3.2: Turned on python_highlight_all for better syntax
" highlighting for Python
" > 3.1: Added revisions ;) and bufexplorer.vim
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => General
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Sets how many lines of history VIM has to remember
set history=300

" Enable filetype plugin
filetype plugin on
filetype indent on

" Set to auto read when a file is changed from the outside
set autoread

" With a map leader it'
s possible to do extra key combinations
" like w saves the current file
let mapleader = "
,"
let g:mapleader = "
,"

"
Fast saving
nmap <leader>w :w!>

" Fast editing of the .vimrc
map e :e! ~/.vim_runtime/vimrc

"
When vimrc is edited, reload it
 bufwritepost vimrc source ~/.vim_runtime/vimrc


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
=> VIM user interface
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
Set 7 lines to the curors - when moving vertical..
set so=7

set wildmenu "Turn on WiLd menu

set ruler "
Always show current position

set cmdheight=2 "The commandbar height

set hid "
Change buffer - without saving

" Set backspace config
set backspace=eol,start,indent
set whichwrap+=<,>,h,l

set ignorecase "
Ignore case when searching

set hlsearch "Highlight search things

set incsearch "
Make search act like search in modern browsers

set magic "Set magic on, for regular expressions

set showmatch "
Show matching bracets when text indicator is over them
set mat=2 "How many tenths of a second to blink

"
No sound on errors
set noerrorbells
set novisualbell
set t_vb=


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
=> Colors and Fonts
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
syntax enable "
Enable syntax hl

" Set font according to system
if MySys() == "
mac"
  set gfn=Bitstream\ Vera\ Sans\ Mono:h13
  set shell=/bin/bash
elseif MySys() == "
windows"
  set gfn=Bitstream\ Vera\ Sans\ Mono:h10
elseif MySys() == "
linux"
  set gfn=Monospace\ 10
  set shell=/bin/bash
endif

if has("
gui_running")
  set guioptions-=T
  set background=dark
  set t_Co=256
  set background=dark
  colorscheme peaksea

  set nu
else
  colorscheme zellner
  set background=dark
  
  set nonu
endif

set encoding=utf8
try
    lang en_US
catch
endtry

set ffs=unix,dos,mac "
Default file types


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
=> Files and backups
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
Turn backup off, since most stuff is in SVN, git anyway...
set nobackup
set nowb
set noswapfile


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
=> Text, tab and indent related
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set expandtab
set shiftwidth=4
set tabstop=4
set smarttab

set lbr
set tw=500

set ai "
Auto indent
set si "Smart indet
set wrap "
Wrap lines

map <leader>t2 :setlocal shiftwidth=2<cr>
map <leader>t4 :setlocal shiftwidth=4<cr>
map <leader>t8 :setlocal shiftwidth=4<cr>


""""""""""""""""""""""""""""""
" => Visual mode related
"
"""""""""""""""""""""""""""""
"
Really
" In visual mode when you press * or # to search for the current selection
vnoremap * :call VisualSearch('f')
vnoremap # :call VisualSearch('b')

"
When you press gv you vimgrep after the selected text
vnoremap <silent> gv :call VisualSearch('gv')<CR>
map <leader>g :vimgrep // **/*.<left><left><left><left><left><left><left>


 CmdLine(str)
    exe "menu Foo.Bar :" . a:str
    emenu Foo.Bar
    unmenu Foo
endfunction

" From an idea by Michael Naumann
function! VisualSearch(direction) range
    let l:saved_reg = @"

    execute "normal! vgvy"

    let l:pattern = escape(@", '\\/.*$^~[]')
    let l:pattern = substitute(l:pattern, "
\n$", "", "")

    if a:direction == 'b'
        execute "
normal ?" . l:pattern . "^M"
    elseif a:direction == 'gv'
        call CmdLine("
vimgrep " . '/'. l:pattern . '/' . ' **/*.')
    elseif a:direction == 'f'
        execute "
normal /" . l:pattern . "^M"
    endif

    let @/ = l:pattern
    let @"
= l:saved_reg
endfunction



"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
=> Command mode related
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
Smart mappings on the command line
cno $h e ~/
cno $d e ~/Desktop/
cno $j e ./
cno $c e <C-\>eCurrentFileDir("e")<cr>

" $q is super useful when browsing on the command line
cno $q eDeleteTillSlash()

"
Bash like keys for the command line
cnoremap <C-A>        <Home>
cnoremap <C-E>        <End>
cnoremap <C-K>        <C-U>

cnoremap <C-P> <Up>
cnoremap <C-N> <Down>

" Useful on some European keyboards
map 陆 $
imap 陆 $
vmap 陆 $
cmap 陆 $


func! Cwd()
  let cwd = getcwd()
  return "
e " . cwd
endfunc

func! DeleteTillSlash()
  let g:cmd = getcmdline()
  if MySys() == "
linux" || MySys() == "mac"
    let g:cmd_edited = substitute(g:cmd, "
\\(.*\[/\]\\).*", "\\1", "")
  else
    let g:cmd_edited = substitute(g:cmd, "
\\(.*\[\\\\]\\).*", "\\1", "")
  endif
  if g:cmd == g:cmd_edited
    if MySys() == "
linux" || MySys() == "mac"
      let g:cmd_edited = substitute(g:cmd, "
\\(.*\[/\]\\).*/", "\\1", "")
    else
      let g:cmd_edited = substitute(g:cmd, "
\\(.*\[\\\\\]\\).*\[\\\\\]", "\\1", "")
    endif
  endif
  return g:cmd_edited
endfunc

func! CurrentFileDir(cmd)
  return a:cmd . "
" . expand("%:p:h") . "/"
endfunc


"
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Moving around, tabs and buffers
"
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Map space to / (search) and c-space to ? (backgwards search)
map /
map ?
map :noh

"
Smart way to move btw. 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

" Close the current buffer
map bd :Bclose

"
Close all the buffers
map <leader>ba :1,300 bd!>

" Use the arrows to something usefull
map :bn
map :bp

"
Tab configuration
map <leader>tn :tabnew %<cr>
map <leader>te :tabedit
map <leader>tc :tabclose<cr>
map <leader>tm :tabmove

" When pressing cd switch to the directory of the open buffer
map cd :cd %:p:h


command! Bclose call BufcloseCloseIt()
function! BufcloseCloseIt()
   let l:currentBufNum = bufnr("
%")
   let l:alternateBufNum = bufnr("
#")

   if buflisted(l:alternateBufNum)
     buffer #
   else
     bnext
   endif

   if bufnr("
%") == l:currentBufNum
     new
   endif

   if buflisted(l:currentBufNum)
     execute("
".l:currentBufNum)
   endif
endfunction

"
Specify the behavior when switching between buffers
try
  set switchbuf=usetab
  set stal=2
catch
endtry


""""""""""""""""""""""""""""""
" => Statusline
"
"""""""""""""""""""""""""""""
"
Always hide the statusline
set laststatus=2

" Format the statusline
set statusline=\ %F%m%r%h\ %w\ \ CWD:\ %r%{CurDir()}%h\ \ \ Line:\ %l/%L:%c


function! CurDir()
    let curdir = substitute(getcwd(), '/Users/amir/', "
~/", "g")
    return curdir
endfunction


"
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Parenthesis/bracket expanding
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
vnoremap $1 `>a)`
vnoremap $2 `>a]`
vnoremap $3 `>a}`
vnoremap $$ `>a"
<esc>`<i"
vnoremap $q `>a'`
vnoremap $e `>a"
<esc>`<i"

"
Map auto complete of (, ", ', [
inoremap $1 ()i
inoremap $2 []i
inoremap $3 {}i
inoremap $4 {o}O
inoremap $q ''i
inoremap $e "
"i


"
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => General Abbrevs
"
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
iab xdate <c-r>=strftime("%d/%m/%y %H:%M:%S")<cr>


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
=> Editing mappings
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
Remap VIM 0
map 0 ^

"Move a line of text using ALT+[jk] or Comamnd+[jk] on mac
nmap mz:m+`z
nmap mz:m-2`z
vmap :m'>+`mzgv`yo`z
vmap :m'<-2`>my`
if MySys() == "
mac"
  nmap
  nmap
  vmap
  vmap
endif

"
Delete trailing white space, useful for Python ;)
 DeleteTrailingWS()
  exe "normal mz"
  %s/\s\+$//ge
  exe "normal `z"
endfunc
autocmd BufWrite *.py :call DeleteTrailingWS()


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
=> Cope
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
Do :help cope if you are unsure what cope is. It's super useful!
map cc :botright cope
map n :cn
map p :cp


""""""""""""""""""""""""""""""
" => bufExplorer plugin
""""""""""""""""""""""""""""""
let g:bufExplorerDefaultHelp=0
let g:bufExplorerShowRelativePath=1


""""""""""""""""""""""""""""""
" => Minibuffer plugin
""""""""""""""""""""""""""""""
let g:miniBufExplModSelTarget = 1
let g:miniBufExplorerMoreThanOne = 2
let g:miniBufExplModSelTarget = 0
let g:miniBufExplUseSingleClick = 1
let g:miniBufExplMapWindowNavVim = 1
let g:miniBufExplVSplit = 25
let g:miniBufExplSplitBelow=1

let g:bufExplorerSortBy = "name"

autocmd BufRead,BufNew :call UMiniBufExplorer

map u :TMiniBufExplorer:TMiniBufExplorer


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Omni complete functions
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
autocmd FileType css set omnifunc=csscomplete#CompleteCSS


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Spell checking
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"Pressing ,ss will toggle and untoggle spell checking
map ss :setlocal spell!

"Shortcuts using
map sn ]s
map sp [s
map sa zg
map s? z=


""""""""""""""""""""""""""""""
" => Python section
""""""""""""""""""""""""""""""
au FileType python set nocindent
let python_highlight_all = 1
au FileType python syn keyword pythonDecorator True None False self

au BufNewFile,BufRead *.jinja set syntax=htmljinja
au BufNewFile,BufRead *.mako set ft=mako

au FileType python inoremap $r return
au FileType python inoremap $i import
au FileType python inoremap $p print
au FileType python inoremap $f #--- PH ----------------------------------------------FP2xi
au FileType python map 1 /class
au FileType python map 2 /def
au FileType python map C ?class
au FileType python map D ?def


""""""""""""""""""""""""""""""
" => JavaScript section
"""""""""""""""""""""""""""""""
au FileType javascript call JavaScriptFold()
au FileType javascript setl fen
au FileType javascript setl nocindent

au FileType javascript imap AJS.log();hi
au FileType javascript imap alert();hi

au FileType javascript inoremap $r return
au FileType javascript inoremap $f //--- PH ----------------------------------------------FP2xi

function! JavaScriptFold()
    setl foldmethod=syntax
    setl foldlevelstart=1
    syn region foldBraces start=/{/ end=/}/ transparent fold keepend extend

    function! FoldText()
    return substitute(getline(v:foldstart), '
{.*', '{...}', '')
    endfunction
    setl foldtext=FoldText()
endfunction


""""""""""""""""""""""""""""""
" => Fuzzy finder
""""""""""""""""""""""""""""""
try
    call fuf#defineLaunchCommand('
FufCWD', 'file', 'fnamemodify(getcwd(), ''%:p:h'')')
    map t :FufCWD **/
catch
endtry


""""""""""""""""""""""""""""""
" => Vim grep
""""""""""""""""""""""""""""""
let Grep_Skip_Dirs = '
RCS CVS SCCS .svn generated'
set grepprg=/bin/grep\ -nH


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => MISC
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Remove the Windows ^M - when the encodings gets messed up
noremap m mmHmt:%s///ge'
tzt

上面的语法高亮是不对的,没有shell脚本语言的选择,感兴趣的可以去查看

阅读(1616) | 评论(0) | 转发(0) |
0

上一篇:Shell Script

下一篇:linux ln命令使用

给主人留下些什么吧!~~