一个简单的vim配置文件

这篇博客介绍了如何配置Vim编辑器,包括设置自动展开tab为4空格,状态栏及高亮当前行,文件类型自动添加头注释,快捷键Ctrl+F5编译运行特定文件类型,以及集成ctags和cscope进行代码跳转和搜索。此外,还详细展示了.vimrc配置文件内容,帮助提升Vim的编程体验。
摘要由CSDN通过智能技术生成

以下展示一个简单的vim配置文件,
该配置提供以下功能:

  • 设置tab自动展开,宽度为4空格
  • 设置状态栏及高亮当前行
  • 设置自动命令为新建.h,.hpp,.c,.cpp,shell,python,makefile文件自动添加头
  • 设置快捷键,对于.c,.cpp,shell,python文件,当按下Ctrl+F5是自动编译运行
  • 设置tags文件自动跳转功能,设置cscope符号查找功能,便于源码阅读
  • 开启plugin,indent,支持对编程文件使用=命令一键格式化代码缩进

请保存到用户宿主目录下的~/.vimrc

"" ---------.vimrc----------------
set fileencodings=usc-bom,utf-8,GB18030,gbk,big5
set nu

"" the general indentation setting
set tabstop=4
set shiftwidth=4
set softtabstop=4
" set expandtab
set noautoindent

"" match and display for searching
set ignorecase
set smartcase
set incsearch
set nohlsearch
" set nowrapscan
" match parenthethese
set showmatch
set matchtime=5
set magic

set autoread
"" scroll off board below or above
set scrolloff=9

"" Use Vim defaults instead of 100% vi compatibility
set nocompatible

set laststatus=2
set statusline=[%n]\ %F%m%r%h\ %=%{&encoding}\ %l,%c\ %p%%\ %L\ 

"" show cursor line and highlight cursor line
set cursorline
" highlight CursorLine cterm=NONE ctermbg=LightGray ctermfg=NONE
if tolower(strpart($TERM,0,5)) == 'xterm'
    highlight CursorLine cterm=NONE ctermbg=LightGray ctermfg=None
else
    highlight CursorLine cterm=NONE ctermbg=None ctermfg=None
endif

syntax reset
syntax on

filetype plugin indent on
if has("autocmd")
    """ This autocommand jumps to the last known position in a file
    """	just after opening it, please verify the permission of ~/.viminfo
    """ Or just rm ~/.viminfo use: sudo rm ~/.viminfo
    au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") | exe "normal! g`\"" | endif
endif

if !exists("autocommands_loaded")
    let autocommands_loaded = 1

    "" set for makefile
    au BufEnter,BufNewFile,BufRead *.mk,[Mm]akefile set noexpandtab

    "" set for shell
    au BufEnter,BufNewFile,BufRead *.sh set tabstop=4 softtabstop=4 shiftwidth=4 expandtab

    "" set for python
    au BufEnter,BufNewFile,BufRead *.py set tabstop=4 softtabstop=4 shiftwidth=4 expandtab
    " au FileType python set tabstop=4 softtabstop=4 shiftwidth=4 expandtab

    "" set for .c/.h/.cpp/.hpp
    au BufEnter,BufNewFile,BufRead *.[ch],*.[ch]pp set tabstop=4 softtabstop=4 shiftwidth=4 expandtab

    " add header comments for .h .c .hpp .cpp .mk .sh new file
    au BufNewFile *.[ch],*.[ch]pp exec ":call SetNewFileHeader()"
    au BufNewFile *.mk,[Mm]akefile exec ":call SetNewFileHeader()"
    au BufNewFile *.sh,*.py exec ":call SetNewFileHeader()"

endif


"" search and load the tags automaticly for ctags plugin
"" use this command to generate tags file: ctags -R *
"" should install ctags firstly
" if executable("/usr/bin/ctags")
if system("which ctags 2>/dev/null") != ""
    set tags=tags;
endif

"" automatically change directory
"" It will change to the directory containing the file
"" which was opened or selected.
if exists("+autochdir")
    set autochdir
else
    if has("autocmd")
        autocmd BufEnter * silent! lcd %:p:h:gs/ /\\/
    endif
endif

"" for cscope plugin setting
"" should install cscope firstly
if has("cscope")
    "" before use please generate the cscope.out file by:
    "" cscope-indexer -r
    set csprg=/usr/bin/cscope
    "" use :cstag instead of the default :tag behavior
    "" use ":cs find g" not ":tag"
    "" cst is the condensation of cscopetag
    set cst
    "" if csto=1 <C-]> use tags file firstly, then cscope.out file
    "" we recommend tags file first when scanning C language code
    set csto=1
    "" do not display msg when add cscope.out file
    set nocsverb
    "" display the depth of matched file path
    set cspc=6

    "" search and add cscope.out file automaticlly
    if filereadable("cscope.out")
        cs add cscope.out
    else
        "" search cscope.out anywhere
        let cscope_file=findfile("cscope.out",".;")
        let cscope_pre=matchstr(cscope_file,".*/")
        if !empty(cscope_file) && filereadable(cscope_file)
            exe "cs add" cscope_file cscope_pre
        endif
        set csverb
    endif

    "" map the shortcut key
    "" find symbol
    nmap <leader>s :cs find s <C-R>=expand("<cword>")<CR><CR>
    "" find global definition
    nmap <leader>g :cs find g <C-R>=expand("<cword>")<CR><CR>
    "" find called by this function
    nmap <leader>d :cs find d <C-R>=expand("<cword>")<CR><CR>
    "" find callers
    nmap <leader>c :cs find c <C-R>=expand("<cword>")<CR><CR>
    "" find text string
    nmap <leader>t :cs find t <C-R>=expand("<cword>")<CR><CR>
    "" find egrep pattern
    nmap <leader>e :cs find e <C-R>=expand("<cword>")<CR><CR>
    "" find and open file
    nmap <leader>f :cs find f <C-R>=expand("<cfile>")<CR><CR>
    "" find files #include this file
    nmap <leader>i :cs find i <C-R>=expand("<cfile>")<CR>$<CR>

endif


" SetNewFileHeader for new buffer
func SetNewFileHeader()
	" set author_name here
	let $author_name = $USER

    if &filetype == 'make'
        call setline(1,  "####################################################")
		call setline(2,  "# Author      : ".$author_name )
        call setline(3,  "# Created date: ".strftime('%F %T'))
        call setline(4,  "####################################################")
        call setline(5,  "")
        call setline(6,  "#target: prerequisites ")
        call setline(7,  "#\tcommand")
        call setline(8,  "")

    elseif &filetype == 'sh'
        call setline(1,  "#! /bin/bash")
        call setline(2,  "#")
        call setline(3,  "####################################################")
        call setline(4,  "# Author      : ".$author_name )
        call setline(5,  "# Created date: ".strftime('%F %T'))
        call setline(6,  "####################################################")
        call setline(7,  "")

    elseif &filetype == 'python'
        call setline(1,  "#! /usr/bin/env python3")
        call setline(2,  "# coding=utf-8")
        call setline(3,  "####################################################")
        call setline(4,  "# Author      : ".$author_name )
        call setline(5,  "# Created date: ".strftime('%F %T'))
        call setline(6,  "####################################################")
        call setline(7,  "")
        call setline(8,  "if __name__ == '__main__':")
        call setline(9,  "    print(\"hello ".expand('%:t')."\")")
        call setline(10, "")

    else
		" set title for *.h,*.hpp,*.c,*.cpp
		call setline(1, "/*****************************************")
		call setline(2, " * Copyright (C) ".strftime('%Y')." * Ltd. All rights reserved.")
		call setline(3, " *")
		call setline(4, " * File name   : ".expand('%:t'))
		call setline(5, " * Author      : ".$author_name )
		call setline(6, " * Created date: ".strftime('%F %T'))
		call setline(7, " * Description : ")
		call setline(8, " *")
		call setline(9, " *******************************************/")
		call setline(10,"")

        if expand("%:e") == 'hpp'
            call append(line("$"), "#ifndef __".toupper(expand("%:t:r"))."_H__")
            call append(line("$"), "#define __".toupper(expand("%:t:r"))."_H__")
            call append(line("$"), "")
            call append(line("$"), "#include <iostream>")
            call append(line("$"), "#include <sstream>")
            call append(line("$"), "#include <string>")
            call append(line("$"), "#include <vector>")
            call append(line("$"), "#include <queue>")
            call append(line("$"), "#include <map>")
            call append(line("$"), "#include <memory>")
            call append(line("$"), "#include <algorithm>")
            call append(line("$"), "#include <chrono>")
            call append(line("$"), "#include <atomic>")
            call append(line("$"), "#include <mutex>")
            call append(line("$"), "#include <condition_variable>")
            call append(line("$"), "#include <numeric>")
            call append(line("$"), "#include <functional>")
            call append(line("$"), "#include <future>")
            call append(line("$"), "#include <thread>")
            call append(line("$"), "")
            call append(line("$"), "")
            call append(line("$"), "#endif //__".toupper(expand("%:t:r"))."_H__")
            call append(line("$"), "")

        elseif expand("%:e") == 'h'
            call append(line("$"), "//#pragma once")
            call append(line("$"), "#ifndef __".toupper(expand("%:t:r"))."_H__")
            call append(line("$"), "#define __".toupper(expand("%:t:r"))."_H__")
            call append(line("$"), "")
            call append(line("$"), "#include <stdio.h>")
            call append(line("$"), "#include <stdlib.h>")
            call append(line("$"), "#include <string.h>")
            call append(line("$"), "")
            call append(line("$"), "#ifdef __cplusplus")
            call append(line("$"), "extern \"C\"")
            call append(line("$"), "{")
            call append(line("$"), "#endif")
            call append(line("$"), "")
            call append(line("$"), "")
            call append(line("$"), "#ifdef __cplusplus")
            call append(line("$"), "}")
            call append(line("$"), "#endif")
            call append(line("$"), "#endif //__".toupper(expand("%:t:r"))."_H__")
            call append(line("$"), "")

        elseif &filetype == 'c'
            call append(line("$"), "#define LOG_TAG \"".toupper(expand("%:t:r"))."\"")
            call append(line("$"), "#include \"".expand("%:t:r").".h\"")
            call append(line("$"), "")
            call append(line("$"), "#include <stdio.h>")
            call append(line("$"), "#include <stdlib.h>")
            call append(line("$"), "#include <string.h>")
            call append(line("$"), "")
            call append(line("$"), "int main(int argc, char *argv[]) {")
            call append(line("$"), "    (void)argc;")
            call append(line("$"), "    (void)argv;")
            call append(line("$"), "")
            call append(line("$"), "    return 0;")
            call append(line("$"), "}")
            call append(line("$"), "")

        elseif &filetype == 'cpp'
            call append(line("$"), "#define LOG_TAG \"".toupper(expand("%:t:r"))."\"")
            call append(line("$"), "#include \"".expand("%:t:r").".h\"")
            call append(line("$"), "")
            call append(line("$"), "#include <iostream>")
            call append(line("$"), "#include <sstream>")
            call append(line("$"), "#include <string>")
            call append(line("$"), "#include <vector>")
            call append(line("$"), "#include <queue>")
            call append(line("$"), "#include <map>")
            call append(line("$"), "#include <memory>")
            call append(line("$"), "#include <algorithm>")
            call append(line("$"), "#include <chrono>")
            call append(line("$"), "#include <atomic>")
            call append(line("$"), "#include <mutex>")
            call append(line("$"), "#include <condition_variable>")
            call append(line("$"), "#include <numeric>")
            call append(line("$"), "#include <functional>")
            call append(line("$"), "#include <future>")
            call append(line("$"), "#include <thread>")
            call append(line("$"), "")
            call append(line("$"), "int main(int argc, char *argv[]) {")
            call append(line("$"), "    (void)argc;")
            call append(line("$"), "    (void)argv;")
            call append(line("$"), "")
            call append(line("$"), "    return 0;")
            call append(line("$"), "}")
            call append(line("$"), "")

        endif

    endif
endfunc

"" Quick run
func! QuickCompileRun()
    exec "w" 
    " echo &filetype
    if &filetype == 'c' 
        exec '!g++ % -o %<'
        exec '!time ./%<'
    elseif &filetype == 'cpp'
        exec '!g++ -std=c++11 % -o %< -lpthread'
        exec '!time ./%<'
    elseif &filetype == 'java' 
        exec "!javac %" 
        exec "!time java %<"
    elseif &filetype == 'python'
        exec '!time python %'
    elseif &filetype == 'sh'
        exec '!time bash %'
    elseif &filetype == 'html'
        exec "!firefox % &"
    elseif &filetype == 'go'
        exec "!go build %<"
        exec "!time go run %"
    elseif &filetype == 'markdown'
        exec "!~/.vim/plugin/markdown.pl % > %.html &"
        exec "!firefox %.html &"
        exec "!rm %.html "
    endif
endfunc 
map <C-F5>  :call QuickCompileRun()<CR>

请确认已经安装vim,或者

sudo apt install vim
sudo apt install vim-scripts
sudo apt install vim-common

建议安装ctagscscope,便于vim中代码跳转。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值