vim配置无插件

Posted guochaoxxl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了vim配置无插件相关的知识,希望对你有一定的参考价值。

其实,vim插件会影响编辑器的启动速度,虽然有些插件影响不大,我依然觉得不够,其实通过简易的状态栏,可以显示必要的信息,能自定义颜色和背景甚至透明就足够了。

一、自定义状态栏
其实以下内容可以写在一行上,分开是为了方便添加内容和解释方便而已。
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 "这一行和set statusline=%1是对应的,其他以此类推,实现了vim的背景透明 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的自动添加功能,即可实现针对不同的文件,添加不同的信息,非常方便。通过自定义一个函数,根据时机调用函数即可:

"创建文件头
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


还有括号补全,引号补全,编译系统设置和一些快捷键设置,配置文件里都写的很清楚了。

全部的配置信息如下:
" 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=1x7" 
" let &t_SR = "<Esc>]50;CursorShape=2x7" 
" let &t_EI = "<Esc>]50;CursorShape=0x7" 
"打开文件,光标回到上次编辑的位置
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检查时间
function SetTimeOfDayColors()
    let currentHour = strftime("%H")
    echo "currentHour is " . currentHour
    if currentHour < 6 + 0
      let colorScheme = "darkblue"
    elseif currentHour < 12 + 0
      let colorScheme = "morning"
    elseif currentHour < 18 + 0
      let colorScheme = "shine"
    else
      let colorScheme = "evening"
    endif
    echo "setting color scheme to " . colorScheme
    execute "colorscheme " . colorScheme
endfunction
set statusline=%<%f %h%m%r%=%-20.(line=%l,col=%c%V,totlin=%L%)\\%h%m%r%=%-40(,bytval=0x%B,%n%Y%) %{strftime("%c")}%=0x%B %P
set statusline += %{SetTimeOfDayColors()}

 

 
 

https://www.cnblogs.com/shanql/articles/6048412.html

https://blog.csdn.net/hunter___/article/details/81749346
https://www.jianshu.com/p/fdf405c65f2d 




以上是关于vim配置无插件的主要内容,如果未能解决你的问题,请参考以下文章

从零开始配置vim(27)——代码片段

vim代码片段插件ultisnips的使用

VIM 代码片段插件 ultisnips 使用教程

无插件Vim配置文件vimrc推荐与各VIM配置项解释

vim配置无插件

无插件Vim技巧