何为vimrc
当大家在编写shell脚本等编程文件时,往往需要向文件头部添加几行的注释代码,如需要使用到的shell、作者信息、版权信息等等。在多数时间里,这些注释文件不会更改,如果每次都需要手动来写入,未免有些浪费时间。
使用vimrc便可以解决上述问题。同时在解决上述问题的同时,还能进行一些vim配置环境的设置。
为什么我的vim这么难用?不能语法高亮,没有折行,不能打开多个窗口多个buffer,不能……
为什么别人用几个键就可以完成一个很复杂的功能,而我不能?
为什么别人的vim看起来和我的很不一样?
……
当你发出这些疑问时,你可能就需要去了解并配置一下属于你的vimrc了
vimrc的分类
在linux中,vimrc文件分两个,分别是/etc/vimrc
(全局性)和~/.vimrc
(当前用户),在此建议配置针对单个用户的vimrc
vimrc的具体实现
如何实现vimrc?对于特定用户,只需要在用户目录下编辑修改 .vimrc 文件即可,若不曾配置过vimrc,则需要手动创建,系统默认不创建该文件
vimrc内部配置
在此仅列出一部分代码,每条指令只稍作解释,具体用法及完整代码请自行查询
set nocompatible #关闭vi兼容模式
set history=100 #设置历史记录条数
filetype on #自动识别文件类型
filetype plugin on
filetype indent on
set autoread #自动读取文件内容
set hlsearch #搜索高亮
syntax enable #语法高亮
set cursorline #光标所在行添加下划线
hi cursorline guibg=#00ff00 #设置下划线属性
hi cursorcolumn guibg=#00ff00 #设置下划线属性
set foldenable #设置语句折叠(zf折叠,zo打开折叠,zc取消)
set foldmethod=manual
set foldcolumn=0
setlocal foldlevel=3
set foldclose=all
nnoremap <space> @=((foldclosed(line('.')) < 0) ? 'zc' : 'zo')<CR>
set expandtab #tab转换为空格
set tabstop=4 #1个tab对应4个位置
set ignorecase #忽略大小写
set encoding=utf-8 #设置字符集
#向文件头部写入注释内容(重要),针对特定类型文件,加上版权信息
#必须是新建文件
autocmd BufNewFile *.py,*.cc,*.sh,*.java exec ":call SetTitle()"
func SetTitle()
if expand("%:e") == 'sh'
call setline(1,"#!/bin/bash")
call setline(2,"#####################################")
call setline(3,"# File Name: ".expand("%"))
call setline(4,"# Version: v1.0")
call setline(5,"# Author:wkk")
call setline(6,"# Organization:NULL")
call setline(7,"#####################################")
endif
endfunc
优化精简后如下
set expandtab
set tabstop=4
set ignorecase
set hlsearch
set noerrorbells
set novisualbell
set nocompatible
filetype on
filetype plugin on
filetype indent on
autocmd BufNewFile *.py,*.cc,*.sh,*.java exec ":call SetTitle()"
func SetTitle()
if expand("%:e") == 'sh'
call setline(1,"#!/bin/bash")
call setline(2,"#####################################")
call setline(3,"# File Name: ".expand("%"))
call setline(4,"# Version: v1.0")
call setline(5,"# Author:wkk")
call setline(6,"# Organization:NULL")
call setline(7,"#####################################")
endif
endfunc