" 不要使用vi的键盘模式,而是vim自己的
set nocompatible
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
" 加载配置。
behave mswin
set diffexpr=MyDiff()
function MyDiff()
let opt = '-a --binary '
if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
let arg1 = v:fname_in
if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
let arg2 = v:fname_new
if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
let arg3 = v:fname_out
if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
let eq = ''
if $VIMRUNTIME =~ ' '
if &sh =~ '\
let cmd = '""' . $VIMRUNTIME . '\diff"'
let eq = '"'
else
let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
endif
else
let cmd = $VIMRUNTIME . '\diff'
endif
silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq
endfunction
"""""""""""""""编码""""""""""""""""""""""""""""""""""""""""""""""""""
set encoding=utf-8
set fileencodings=utf-8,chinese,latin-1
if has("win32")
set fileencoding=chinese
else
set fileencoding=utf-8
endif
"解决菜单乱码
source $VIMRUNTIME/delmenu.vim
source $VIMRUNTIME/menu.vim
"解决consle输出乱码
language messages zh_CN.utf-8
""""""""""""""""通用配置""""""""""""""""""""""""""""""""""""""""""""""""""""""
"无菜单、工具栏
set go=
"配色方案
colorscheme evening
" 显示行号
set nu!
"按Ctrl+N进行代码补全
set completeopt=longest,menu
" 设置备份文件后缀
"set backupext=.bak
" 设置备份文件夹
set backupdir=D:/workspace/VIM
" 设置tab键的宽度
set tabstop=4
" 整词换行
set linebreak
" 打开语法高亮
syn on
" 命令行补全
set wildmenu
"设置快速编辑.vimrc文件 ,e 编辑.vimrc
map e :call SwitchToBuf("~/_vimrc")
"保存.vimrc文件后会自动调用新的.vimrc
autocmd! bufwritepost .vimrc source ~/_vimrc
" 自动格式化设置
filetype indent on
set autoindent
set smartindent
" 查询时非常方便,如要查找book单词,当输入到/b时,会自动找到
set incsearch
" 加了这句才可以用智能补全
filetype pluginindenton
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 语言的编译和运行
" 支持的语言:java F5编译(保存+编译) F6运行
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
func! CompileCode()
exec "w"
if &filetype == "java"
exec "!javac -encoding utf-8 %"
endif
endfunc
func! RunCode()
if &filetype == "java"
exec "!java -classpath %:h; %:t:r"
endif
endfunc
" F5 保存+编译
map :call CompileCode()
" F6 运行
map :call RunCode()
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 快速打开vimrc
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"Set mapleader
let mapleader = ","
"Fast reloading of the .vimrc
map ss :source ~/.vimrc
"Fast editing of .vimrc
map ee :e ~/.vimrc
"When .vimrc is edited, reload it
autocmd! bufwritepost .vimrc source ~/.vimrc
""""""""""""""""""""""SwitchToBuf()"""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
"实现它在所有标签页的窗口中查找指定的文件名,如果找到这样一个窗口,
"就跳到此窗口中;否则,它新建一个标签页来打开vimrc文件
"上面自动编辑.vimrc文件用到的函数
function! SwitchToBuf(filename)
let bufwinnr = bufwinnr(a:filename)
if bufwinnr != -1
exec bufwinnr . "wincmd w"
return
else
" find in each tab
tabfirst
let tab = 1
while tab <= tabpagenr("$")
let bufwinnr = bufwinnr(a:filename)
if bufwinnr != -1
exec "normal " . tab . "gt"
exec bufwinnr . "wincmd w"
return
endif
tabnext
let tab = tab + 1
endwhile
" not exist, new tab
exec "tabnew " . a:filename
endif
endfunction
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 保存代码文件前自动修改最后修改时间
au BufWritePre *.sh call TimeStamp('#')
au BufWritePre .vimrc,*.vim call TimeStamp('"')
au BufWritePre *.c,*.h call TimeStamp('//')
au BufWritePre *.cpp,*.hpp call TimeStamp('//')
au BufWritePre *.cxx,*.hxx call TimeStamp('//')
au BufWritePre *.java call TimeStamp('//')
au BufWritePre *.rb call TimeStamp('#')
au BufWritePre *.py call TimeStamp('#')
au BufWritePre Makefile call TimeStamp('#')
au BufWritePre *.php
\call TimeStamp(', '?>')
au BufWritePre *.html,*htm
\call TimeStamp('')
" 更改Leader为","
let g:C_MapLeader = ','
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"
"Last change用到的函数,返回时间,能够自动调整位置
function! TimeStamp(...)
let sbegin = ''
let send = ''
if a:0 >= 1
let sbegin = a:1.'\s*'
endif
if a:0 >= 2
let send = ' '.a:2
endif
let pattern = 'Last Change: .\+'
\. send
let pattern = '^\s*' . sbegin . pattern . '\s*$'
let now = strftime('%Y-%m-%d %H:%M:%S',
\localtime())
let row = search(pattern, 'n')
if row == 0
let now = a:1 . ' Last Change: '
\. now . send
call append(2, now)
else
let curstr = getline(row)
let col = match( curstr , 'Last')
let spacestr = repeat(' ',col - 1)
let now = a:1 . spacestr . 'Last Change: '
\. now . send
call setline(row, now)
endif
endfunction
set shellslash
set grepprg=grep\ -nJ\ $*
let g:tex_flavor='latex'
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 搜索和匹配 "
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 高亮显示匹配的括号
set showmatch
" 匹配括号高亮的时间(单位是十分之一秒)
set matchtime=3
" 在搜索的时候忽略大小写
set ignorecase
" 不要高亮被搜索的句子(phrases)
" set nohlsearch
" 在搜索时,输入的词句的逐字符高亮(类似firefox的搜索)
set incsearch
" 输入:set list命令是应该显示些啥?
set listchars=tab:\|\ ,trail:.,extends:>,precedes:<,eol:$
" Tab补全时忽略这些忽略这些
set wildignore=*.o,*.obj,*.bak,*.exe
" 光标移动到buffer的顶部和底部时保持3行距离
set scrolloff=3
"搜索出之后高亮关键词
set hlsearch
nmap :noh
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 自动补全括号,包括大括号
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
:inoremap ( ()i
:inoremap ) =ClosePair(')')
:inoremap { {}i
:inoremap } =ClosePair('}')
:inoremap [ []i
:inoremap ] =ClosePair(']')
:inoremap < <>i
:inoremap > =ClosePair('>')
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" amix/vimrc
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Awesome_version:
" Get this config, nice color schemes and lots of plugins!
"
" Install the awesome version from:
"
" https:
" Sections:
" -> General
" -> VIM user interface
" -> Colors and Fonts
" -> Files and backups
" -> Text, tab and indent related
" -> Visual mode related
" -> Moving around, tabs and buffers
" -> Status line
" -> Editing mappings
" -> vimgrep searching and cope displaying
" -> Spell checking
" -> Misc
" -> Helper functions
"
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => General
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Sets how many lines of history VIM has to remember
set history=500
" Enable filetype plugins
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 w :w!
" :W sudo saves the file
" (useful for handling the permission-denied error)
command W w !sudo tee % > /dev/null
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => VIM user interface
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Set 7 lines to the cursor - when moving vertically using j/k
set so=7
" Avoid garbled characters in Chinese language windows OS
let $LANG='en'
set langmenu=en
source $VIMRUNTIME/delmenu.vim
source $VIMRUNTIME/menu.vim
" Turn on the WiLd menu
set wildmenu
" Ignore compiled files
set wildignore=*.o,*~,*.pyc
if has("win16") || has("win32")
set wildignore+=.git\*,.hg\*,.svn\*
else
set wildignore+=*/.git.hg.svn.DS_Store
endif
"Always show current position
set ruler
" Height of the command bar
set cmdheight=2
" A buffer becomes hidden when it is abandoned
set hid
" Configure backspace so it acts as it should act
set backspace=eol,start,indent
set whichwrap+=<,>,h,l
" Ignore case when searching
set ignorecase
" When searching try to be smart about cases
set smartcase
" Highlight search results
set hlsearch
" Makes search act like search in modern browsers
set incsearch
" Don't redraw while executing macros (good performance config)
set lazyredraw
" For regular expressions turn magic on
set magic
" Show matching brackets when text indicator is over them
set showmatch
" How many tenths of a second to blink when matching brackets
set mat=2
" No annoying sound on errors
set noerrorbells
set novisualbell
set t_vb=
set tm=500
" Add a bit extra margin to the left
set foldcolumn=1
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Colors and Fonts
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Enable syntax highlighting
syntax enable
try
colorscheme desert
catch
endtry
set background=dark
" Set extra options when running in GUI mode
if has("gui_running")
set guioptions-=T
set guioptions-=e
set t_Co=256
set guitablabel=%M\ %t
endif
" Set utf8 as standard encoding and en_US as the standard language
set encoding=utf8
" Use Unix as the standard file type
set ffs=unix,dos,mac
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Files, backups and undo
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Turn backup off, since most stuff is in SVN, git et.c anyway...
set nobackup
set nowb
set noswapfile
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Text, tab and indent related
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Use spaces instead of tabs
set expandtab
" Be smart when using tabs ;)
set smarttab
" 1 tab == 4 spaces
set shiftwidth=4
set tabstop=4
" Linebreak on 500 characters
set lbr
set tw=500
set cindent
set cinkeys-=0#
set indentkeys-=0#
set wrap "Wrap lines
""""""""""""""""""""""""""""""
" => Visual mode related
""""""""""""""""""""""""""""""
" Visual mode pressing * or # searches for the current selection
" Super useful! From an idea by Michael Naumann
vnoremap * :call VisualSelection('', '')/=@/
vnoremap # :call VisualSelection('', '')?=@/
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Moving around, tabs, windows and buffers
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Map to / (search) and Ctrl- to ? (backwards search)
map /
map ?
" Disable highlight when is pressed
map :noh
" Smart way to move between windows
map j
map k
map h
map l
" Close the current buffer
map bd :Bclose:tabclosegT
" Close all the buffers
map ba :bufdo bd
map l :bnext
map h :bprevious
" Useful mappings for managing tabs
map tn :tabnew
map to :tabonly
map tc :tabclose
map tm :tabmove
map t :tabnext
" Let 'tl' toggle between this and the last accessed tab
let g:lasttab = 1
nmap tl :exe "tabn ".g:lasttab
au TabLeave * let g:lasttab = tabpagenr()
" Opens a new tab with the current buffer's path
" Super useful when editing files in the same directory
map te :tabedit =expand("%:p:h")/
" Switch CWD to the directory of the open buffer
map cd :cd %:p:h:pwd
" Specify the behavior when switching between buffers
try
set switchbuf=useopen,usetab,newtab
set stal=2
catch
endtry
" Return to last edit position when opening files (You want this!)
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
""""""""""""""""""""""""""""""
" => Status line
""""""""""""""""""""""""""""""
" Always show the status line
set laststatus=2
" Format the status line
set statusline=\ %{HasPaste()}%F%m%r%h\ %w\ \ CWD:\ %r%{getcwd()}%h\ \ \ Line:\ %l\ \ Column:\ %c
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Editing mappings
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Remap VIM 0 to first non-blank character
map 0 ^
" Move a line of text using ALT+[jk] or Command+[jk] on mac
nmap mz:m+`z
nmap mz:m-2`z
vmap :m'>+`mzgv`yo`z
vmap :m'<-2`>my`if has("mac") || has("macunix")
nmap
nmap
vmap
vmap
endif
" Delete trailing white space on save, useful for Python and CoffeeScript ;)
func! DeleteTrailingWS()
exe "normal mz"
%s/\s\+$//ge
exe "normal `z"
endfunc
autocmd BufWrite *.py :call DeleteTrailingWS()
autocmd BufWrite *.coffee :call DeleteTrailingWS()
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Ag searching and cope displaying
" requires ag.vim - it's much better than vimgrep/grep
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" When you press gv you Ag after the selected text
vnoremap gv :call VisualSelection('gv', '')
" Open Ag and put the cursor in the right position
map g :Ag
" When you press r you can search and replace the selected text
vnoremap r :call VisualSelection('replace', '')
" Do :help cope if you are unsure what cope is. It's super useful!
"
" When you search with Ag, display your results in cope by doing:
" cc
"
" To go to the next search result do:
" n
"
" To go to the previous search results do:
" p
"
map cc :botright cope
map co ggVGy:tabnew:set syntax=qfpgg
map n :cn
map p :cp
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => 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=
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Misc
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Remove the Windows ^M - when the encodings gets messed up
noremap m mmHmt:%s///ge'tzt'm
" Quickly open a buffer for scribble
map q :e ~/buffer
" Quickly open a markdown buffer for scribble
map x :e ~/buffer.md
" Toggle paste mode on and off
map pp :setlocal paste!
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Helper functions
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
function! CmdLine(str)
exe "menu Foo.Bar :" . a:str
emenu Foo.Bar
unmenu Foo
endfunction
function! VisualSelection(direction, extra_filter) range
let l:saved_reg = @"
execute "normal! vgvy"
let l:pattern = escape(@", '\\/.*$^~[]')
let l:pattern = substitute(l:pattern, "\n$", "", "")
if a:direction == 'gv'
call CmdLine("Ag \"" . l:pattern . "\" " )
elseif a:direction == 'replace'
call CmdLine("%s" . '/'. l:pattern . '/')
endif
let @/ = l:pattern
let @" = l:saved_reg
endfunction
" Returns true if paste mode is enabled
function! HasPaste()
if &paste
return 'PASTE MODE '
endif
return ''
endfunction
" Don't close window, when deleting a buffer
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("bdelete! ".l:currentBufNum)
endif
endfunction
set nocompatible "不要使用vi的键盘模式,而是vim自己的
source $VIMRUNTIME/mswin.vim
behave mswin "兼容windows下的快捷键
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" GVIM自身的设置
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
language messages zh_CN.utf-8 " 解决consle输出乱码
colorscheme desert " 灰褐色主题
set guioptions-=T " 隐藏工具栏
set guifont=Monospace\ 30 " 字体 && 字号
set noerrorbells " 关闭错误提示音
set nobackup " 不要备份文件
set linespace=0 " 字符间插入的像素行数目
set shortmess=atI " 启动的时候不显示那个援助索马里儿童的提示
set novisualbell " 不要闪烁
set scrolloff=3 " 光标移动到buffer的顶部和底部时保持3行距离
set mouse=a " 可以在buffer的任何地方 ->
set selection=exclusive " 使用鼠标(类似office中 ->
set selectmode=mouse,key " 在工作区双击鼠标定位)
set cursorline " 突出显示当前行
set whichwrap+=<,>,h,l " 允许backspace和光标键跨越行边界
set completeopt=longest,menu "按Ctrl+N进行代码补全
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 文本格式和排版
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set list " 显示Tab符,->
set listchars=tab:\|\ , " 使用一高亮竖线代替
set tabstop=4 " 制表符为4
set autoindent " 自动对齐(继承前一行的缩进方式)
set smartindent " 智能自动缩进(以c程序的方式)
set softtabstop=4
set shiftwidth=4 " 换行时行间交错使用4个空格
set noexpandtab " 不要用空格代替制表符
set cindent " 使用C样式的缩进
set smarttab " 在行和段开始处使用制表符
set nowrap " 不要换行显示一行
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 状态行(命令行)的显示
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set cmdheight=2 " 命令行(在状态行下)的高度,默认为1,这里是2
set ruler " 右下角显示光标位置的状态行
set laststatus=2 " 开启状态栏信息
set wildmenu " 增强模式中的命令行自动完成操作
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 文件相关
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set fenc=utf-8
set encoding=utf-8 " 设置vim的工作编码为utf-8,如果源文件不是此编码,vim会进行转换后显示
set fileencoding=utf-8 " 让vim新建文件和保存文件使用utf-8编码
set fileencodings=utf-8,gbk,cp936,latin-1
filetype on " 侦测文件类型
filetype indent on " 针对不同的文件类型采用不同的缩进格式
filetype plugin on " 针对不同的文件类型加载对应的插件
syntax on " 语法高亮
filetype plugin indent on " 启用自动补全
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 查找
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set hlsearch " 开启高亮显示结果
set nowrapscan " 搜索到文件两端时不重新搜索
set incsearch " 开启实时搜索功能
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 语言的编译和运行
" 支持的语言:java F5编译(保存+编译) F6运行
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
func! CompileCode()
exec "w"
if &filetype == "java"
exec "!javac -encoding utf-8 %"
endif
endfunc
func! RunCode()
if &filetype == "java"
exec "!java -classpath %:h; %:t:r"
endif
endfunc
" F5 保存+编译
map :call CompileCode()
" F6 运行
map :call RunCode()
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 实用功能
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"--------引号 && 括号自动匹配
:inoremap ( ()i
:inoremap ) =ClosePair(')')
:inoremap { {}i
:inoremap } =ClosePair('}')
:inoremap [ []i
:inoremap ] =ClosePair(']')
":inoremap < <>i
":inoremap > =ClosePair('>')
:inoremap " ""i
:inoremap ' ''i
:inoremap ` ``i
function ClosePair(char)
if getline('.')[col('.') - 1] == a:char
return "\"
else
return a:char
endif
endf
"--------启用代码折叠,用空格键来开关折叠
set foldenable " 打开代码折叠
set foldmethod=syntax " 选择代码折叠类型
set foldlevel=100 " 禁止自动折叠
nnoremap @=((foldclosed(line('.')) < 0) ? 'zc':'zo')
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 插件
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 打开文件浏览窗口 插件为WinManager
let g:winManagerWindowLayout='FileExplorer'
nmap :WMToggle
" MiniBufExplorer
let g:miniBufExplMapWindowNavVim = 1
let g:miniBufExplMapWindowNavArrows = 1
let g:miniBufExplMapCTabSwitchBufs = 1
let g:miniBufExplModSelTarget = 1
" Make VIM remember position in file after reopen
" if has("autocmd")
" au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
"endif
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""