vim编辑器(vimrc)的配置

来源:互联网 发布:软件开发工程师职责 编辑:程序博客网 时间:2024/06/07 10:27

关于vim的编辑器的配置文件

vim编辑器的配置文件为vimrc的文件, 在/etc/vim/vimrc 或用户目录~/.vimrc(以点开头的隐藏文件,当ls -a时,才会显示出所有隐藏的文件), 如果在用户目录下没有的话可以将/etc/vim/vimrc下的复制到用户目录~/ ,用户目录下vimrc配置文件的优先级更高. 但是在当前用户下配置的.vimrc仅对当前用户有效.在.vimrc文件中我们可以自定义多种初始化设置,每次打开vim会读取该配置文件.

Linux系统下,Tab键默认为8个字符,要修改为4个字符的方式, 和其他的设置, 在.vimrc配置文件中添加:

  • set tabstop=4
    tabstop 表示一个 tab 显示出来是4个空格,默认 8。
  • set shiftwidth=4
    shiftwidth 表示每一级缩进的空格数为4
  • set autoindent
    设置自动缩进:即每行的缩进值与上一行相等;使用 noautoindent 取消设置:
  • set cindent
    使用 C/C++ 语言的自动缩进方式
  • set number或set nu
    显示行号
  • set hlsearch
    高亮搜索结果
  • syntax on
    语法高亮
  • set showmatch, set sm
    括号匹配高亮显示
  • set ic
    忽略字符大小写

vim中自动添加文件的作者、时间信息等.

按F2在文件头部自动添加

因为初学vim,简单配置了自己的vimrc,贴出来供大家参考下:
” 为注释行

" All system-wide defaults are set in $VIMRUNTIME/debian.vim and sourced by" the call to :runtime you can find below.  If you wish to change any of those" settings, you should do it in this file (/etc/vim/vimrc), since debian.vim" will be overwritten everytime an upgrade of the vim packages is performed." It is recommended to make changes after sourcing debian.vim since it alters" the value of the 'compatible' option." This line should not be removed as it ensures that various options are" properly set to work with the Vim-related packages available in Debian.runtime! debian.vim" Uncomment the next line to make Vim more Vi-compatible" NOTE: debian.vim sets 'nocompatible'.  Setting 'compatible' changes numerous" options, so any other options should be set AFTER setting 'compatible'."set compatible" Vim5 and later versions support syntax highlighting. Uncommenting the next" line enables syntax highlighting by default.if has("syntax")  syntax onendif" If using a dark background within the editing area and syntax highlighting" turn on this option as well"set background=dark" Uncomment the following to have Vim jump to the last position when" reopening a file"if has("autocmd")"  au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif"endif" Uncomment the following to have Vim load indentation rules and plugins" according to the detected filetype."if has("autocmd")"  filetype plugin indent on"endif" The following are commented out as they cause vim to behave a lot" differently from regular Vi. They are highly recommended though."set showcmd        " Show (partial) command in status line."set showmatch      " Show matching brackets."set ignorecase     " Do case insensitive matching"set smartcase      " Do smart case matching"set incsearch      " Incremental search"set autowrite      " Automatically save before commands like :next and :make"set hidden     " Hide buffers when they are abandoned"set mouse=a        " Enable mouse usage (all modes)set tabstop=4     set shiftwidth=4set autoindentset cindentset number" Source a global configuration file if availableif filereadable("/etc/vim/vimrc.local")  source /etc/vim/vimrc.localendif"SET Comment START (Addtitles也就是添加标题)"表示遇到这些文件类型自动创建 autocmd BufNewFile *.php,*.js,*.cpp,*.c exec ":call SetComment()" |normal 10Go"map <F2> :call SetComment()<CR>:10<CR>omap <F2> :call SetComment()<cr>'s  func SetComment()    if expand("%:e")=='php'        call setline(1,"<?php")    elseif expand("%:e")=='js'        call setline(1,'//JavaScript file')    elseif expand("%:e")=='cpp'        call setline(1,'//C++ file')    elseif expand("%:e")=='c'        call setline(1,'//C file')    endif        call append(1,'/******************************************************')    call append(2,'*')    call append(3,'* Filename      : '.expand("%:t"))        call append(4,'* Author        : Liuamin')        call append(5,'* Email         : 502935795@qq.com')        call append(6,'* Create        :'.strftime("%Y-%m-%d %H:%M:%S"))        call append(7,'* Last modified :'.strftime("%Y-%m-%d %H:%M:%S"))        call append(8,'* Description   :')        call append(9,'* ****************************************************/')    echohl WarningMsg | echo "Successful in adding the copyright." | echohl Noneendfunc"SET Comment END"Updata Last modified and Filename(更新最近修改时间和文件名)function UpdataTitle()   normal m'   execute '/# *Last modified:/s@:.*$@\=strftime(":\t%Y-%m-%d %H:%M:%S")@'   normal ''   execute '/# *Filename:/s@:.*$@\=":\t\t".expand("%:t")@'   execute "noh"   normal 'k   echohl  WaringMsg | echo "Successful in updating the copy right."| echohl Noneendfunction"判断前10行代码中,是否有Last modified这个单词"如果没有的话,代表没有添加过Title,需要新添加,此处屏蔽掉Setcomment(),没有不需要添加"如果有的话,只需要更新即可function UpdataTitle2()    len n=1        while n<7        len line=getline(n)        if line =~'^\s*\*\s*\S*Last\s*modified\s*:\s*\S*.*$'              call UpdataTitle()            return        endif        len n=n+1    endwhile    call SetComment()endfunction"autocmd FileWritePre,BufWritePre *.php,*.js,*.cpp,*c ks|call UpdataTitle2() |'s