美文网首页配置文件
vim配置(无插件,含状态栏,背景透明等)

vim配置(无插件,含状态栏,背景透明等)

作者: __倔强的贝吉塔 | 来源:发表于2019-08-01 22:09 被阅读71次

    状态栏

    我不是很喜欢用插件,因为我觉得插件会影响我编辑器的启动速度,虽然我知道有些插件影响得很小。比方说airline,它只会延迟100ms,但是我依然不想用,大概是有洁癖。所以我自己写了一个简易的状态栏,可以展示必要的信息,同时也可以自定义颜色。

    set statusline=%1*\%<%.50F\             "显示文件名和文件路径 (%<应该可以去掉)
    set statusline+=%=%2*\%y%m%r%h%w\ %*        "显示文件类型及文件状态
    set statusline+=%3*\%{&ff}\[%{&fenc}]\ %*   "显示文件编码类型
    set statusline+=%4*\ row:%l/%L,col:%c\ %*   "显示光标所在行和列
    set statusline+=%5*\%3p%%\%*            "显示光标前文本所占总文本的比例
    hi User1 cterm=none ctermfg=25 ctermbg=0 
    hi User2 cterm=none ctermfg=208 ctermbg=0
    hi User3 cterm=none ctermfg=169 ctermbg=0
    hi User4 cterm=none ctermfg=100 ctermbg=0
    hi User5 cterm=none ctermfg=green ctermbg=0
    
    • %< 如果状态行过长,在何处换行
    • %F 完整文件路径名
    • %.50F 文件路径名长度不超过50,超过则进行缩写
    • %= 在此之后的内容,显示在状态栏上时右对齐
    • %y 文件类型
    • %m 如果缓冲区已修改则表示为[+]
    • %r 如果缓冲区为只读则表示为[RO]
    • %h 如果为帮助缓冲区显示为[Help]
    • %w 如果为预览窗口则显示为[Preview]
    • %{&ff} 显示文件系统类型
    • %{&fenc} 显示文件编码
    • %l 光标所在行数
    • %L 文件总行数
    • %c 光标所在列数
    • %p 当前行数占总行数的的百分比
    • cterm:设置粗体,斜体,正体;ctermfg:前景色;ctermbg:背景色

    %number *\ ... \%*hi User<number>对应,hi User<number>后面设置的颜色样式会应用到%number *\ ... \%*中的...部分

    vim效果图

    背景透明

    从上图我们不仅可以看到最下面的状态栏,还可以发现vim的背景是透明的。vim的背景透明是通过hi Normal ctermfg=252 ctermbg=none实现的,但是这条语句依赖于你的终端透明,它需要你的终端可以设置透明背景,我这里是ubuntu系统自带的终端。

    创建新文件自动加上文件头

    我喜欢在代码文件开头写一些注释,比如作者信息,因此我还希望我的vim可以根据我新创建的文件类型,自动选择相应的文件头。这个功能我觉得很棒。这个功能实现起来也非常简单,定义一个函数然后调用就可以了。

    "创建文件头
    autocmd BufNewFile *.py,*.tex exec ":call SetTitle()"
    func! SetTitle() 
        if &filetype == 'python'
            call setline(1,"#!/usr/bin/env python3")
            call append(line("."),"# -*- coding:UTF-8 -*-")
            call append(line(".")+1,"##########################################################################")
            call append(line(".")+2, "# File Name: ".expand("%"))
            call append(line(".")+3, "# Author: stubborn vegeta")
            call append(line(".")+4, "# Created Time: ".strftime("%c"))
            call append(line(".")+5, "##########################################################################")
        endif
        if &filetype == 'plaintex'
            call setline(1,"% -*- coding:UTF-8 -*-")
            call append(line("."),"%#########################################################################")
            call append(line(".")+1, "% File Name: ".expand("%"))
            call append(line(".")+2, "% Author: stubborn vegeta")
            call append(line(".")+3, "% Created Time: ".strftime("%c"))
            call append(line(".")+4, "%#########################################################################")
        endif
        normal Go 
    endfunc
    

    下面分别是我新建一个python文件和新建一个LaTeX文件的效果图。


    python LaTeX

    此外,还有括号补全,编译系统设置和一些快捷键设置,配置文件里都写的很清楚了。我没有设置引号补全,因为我写python时经常用三个引号,补全之后反而很麻烦。

    " 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
    
    " Vim will load $VIMRUNTIME/defaults.vim if the user does not have a vimrc.
    " This happens after /etc/vim/vimrc(.local) are loaded, so it will override
    " any settings in these files.
    " If you don't want that to happen, uncomment the below line to prevent
    " defaults.vim from being loaded.
    " let g:skip_defaults_vim = 1
    
    " 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 on
    endif
    
    " 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)
    
    " Source a global configuration file if available
    if filereadable("/etc/vim/vimrc.local")
      source /etc/vim/vimrc.local
    endif
    let mapleader=" "           " 设置leader键为空格键
    set nocompatible            " 不以兼容模式运行
    set encoding=utf-8          " utf-8编码
    set helplang=cn             " 中文帮助文档
    set number              " 显示行号
    set wrap                " 自动换行
    set showcmd             " 显示输入信息
    set cursorline              " 显示光标所在行
    set wildmenu                " 显示补全提示
    set hlsearch                " 高亮搜索结果
    "set foldenable                 " 允许折叠 
    "set foldmethod=manual          " 手动折叠  
    "打开vim运行nohlsearch,取消高亮
    exec "nohlsearch"       
    set ts=4                " 设置tab键长度为四个空格
    set expandtab           " 设置tab键替换为四个空格键
    " 将文件中的tab键替换成空格
    map <LEADER>    :retab!<CR>
    set incsearch               " 一边输入一边高亮  
    set ignorecase              " 忽略大小写
    set smartcase               " 智能大小写 
    set laststatus=2            " 设置状态栏在倒数第2行
    
    " 设置状态栏格式
    "set statusline=%<%F%=%y%m%r%h%w%{&ff}\[%{&fenc}]0x%02B@%040h#%n\(%3l/%3L,%3c\|%3v\)%3p%%
    set statusline=%1*\%<%.50F\             "显示文件名和文件路径 
    set statusline+=%=%2*\%y%m%r%h%w\ %*        "显示文件类型及文件状态
    set statusline+=%3*\%{&ff}\[%{&fenc}]\ %*   "显示文件编码类型
    set statusline+=%4*\ row:%l/%L,col:%c\ %*   "显示光标所在行和列
    set statusline+=%5*\%3p%%\%*            "显示光标前文本所占总文本的比例
    hi User1 cterm=none ctermfg=25 ctermbg=0 
    hi User2 cterm=none ctermfg=208 ctermbg=0
    hi User3 cterm=none ctermfg=169 ctermbg=0
    hi User4 cterm=none ctermfg=100 ctermbg=0
    hi User5 cterm=none ctermfg=green ctermbg=0
    
    set mouse=a             " 启用鼠标
    set backspace=indent,eol,start      " 退格键可以退到上一行
    set scrolloff=5             " 光标行上下移动范围各缩小5行
    "set ruler
    "set transparency=11
    " 设置背景透明
    hi Normal ctermfg=252 ctermbg=none
    "寻找下一搜索结果,并将其置于屏幕中心
    noremap = nzz               
    "寻找上一搜索结果,并将其置于屏幕中心
    noremap - Nzz
    "取消高亮
    noremap <LEADER><CR> :nohlsearch<CR>
    map s <nop>
    "保存
    map S :w<CR>
    "退出
    map Q :q<CR>
    "右分屏,聚焦右窗口
    map sl :set splitright<CR>:vsplit<CR>
    "左分屏,聚焦左窗口
    map sh :set nosplitright<CR>:vsplit<CR>
    "上分屏,聚焦上窗口
    map sk :set nosplitbelow<CR>:split<CR>
    "下分屏,聚焦下窗口
    map sj :set splitbelow<CR>:split<CR>
    "光标移至右窗口
    map <LEADER>l <C-w>l
    "光标移至上窗口
    map <LEADER>k <C-w>k
    "光标移至下窗口
    map <LEADER>j <C-w>j
    "光标移至左窗口
    map <LEADER>h <C-w>h
    "窗口上移
    map <up> :res +5<CR>
    "窗口下移
    map <down> :res -5<CR>
    "窗口左移
    map <left> :vertical resize-5<CR>
    "窗口右移
    map <right> :vertical resize+5<CR>
    "新建标签页
    map <C-n> :tabe<CR>
    "前一标签页
    map t- :-tabnext<CR>
    "后一标签页
    map t= :+tabnext<CR>
    "重新加载vim配置文件
    map rc :source $MYVIMRC<CR>
    "寻找两个相等的单词
    map <LEADER>fd /\(\<\w\+\>\)\_s*\1
    "替换占空符<++>
    map <LEADER><LEADER> <ESC>/<++><CR>:nohlsearch<CR>c4l
    "全选
    map <C-a> ggVG
    "打开我的vimrc
    map <LEADER>rc :e ~/.vimrc<CR>
    "复制到系统剪切板
    map +y "+y
    "从系统剪切板粘贴
    map +p "+p
    
    set list
    set listchars=tab:>-,trail:-
    "自动匹配括号
    :inoremap ( ()<ESC>i
    :inoremap ) <c-r>=ClosePair(')')<CR> 
    :inoremap { {}<ESC>i
    :inoremap } <c-r>=ClosePair('}')<CR>
    :inoremap [ []<ESC>i
    :inoremap ] <c-r>=ClosePair(']')<CR>
    :inoremap < <><ESC>i
    :inoremap > <c-r>=ClosePair('>')<CR>
    function! ClosePair(char)
        if getline('.')[col('.') - 1] == a:char
            return "\<Right>"
        else
            return a:char
        endif
    endfunction
    
    " let &t_SI = "\<Esc>]50;CursorShape=1\x7" 
    " let &t_SR = "\<Esc>]50;CursorShape=2\x7" 
    " let &t_EI = "\<Esc>]50;CursorShape=0\x7" 
    "打开文件,光标回到上次编辑的位置
    au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
    "创建文件头
    autocmd BufNewFile *.py,*.tex exec ":call SetTitle()"
    
    func! SetTitle() 
        if &filetype == 'python'
            call setline(1,"#!/usr/bin/env python3")
            call append(line("."),"# -*- coding:UTF-8 -*-")
            call append(line(".")+1,"##########################################################################")
            call append(line(".")+2, "# File Name: ".expand("%"))
            call append(line(".")+3, "# Author: stubborn vegeta")
            call append(line(".")+4, "# Created Time: ".strftime("%c"))
            call append(line(".")+5, "##########################################################################")
        endif
        if &filetype == 'plaintex'
            call setline(1,"% -*- coding:UTF-8 -*-")
            call append(line("."),"%#########################################################################")
            call append(line(".")+1, "% File Name: ".expand("%"))
            call append(line(".")+2, "% Author: stubborn vegeta")
            call append(line(".")+3, "% Created Time: ".strftime("%c"))
            call append(line(".")+4, "%#########################################################################")
        endif
        "autocmd BufNewFile * normal G 
        normal G 
    endfunc
    " 设置注释快捷键 
    map <LEADER>r :call Note()<CR>
    func! Note()
        if &filetype == 'python'
            normal 0i# 
        endif
        if &filetype == 'vim'
            normal 0i" 
        endif
        if &filetype == 'plaintex'
            normal 0i% 
        endif
    endfunc
    " 设置取消注释  
    map <LEADER>t 0df j
    "编译运行
    map <F5> :call RunPython()<CR>
    func! RunPython()
        exec "W"
        if &filetype == 'python'
           " exec "!time python3.6 %"
        "exec ":set splitbelow<CR>:split<CR>"
        exec "!time python3.6 %"
    
        endif
        if &filetype == 'dot'
        exec "!dot % -T png -o %.png"
        exec "!feh %.png"
        endif
    endfunc
    
    "colorscheme molokai
    set t_Co=256            "开启256色支持
    "set background=dark
    

    相关文章

      网友评论

        本文标题:vim配置(无插件,含状态栏,背景透明等)

        本文链接:https://www.haomeiwen.com/subject/qijhdctx.html