zoukankan      html  css  js  c++  java
  • 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()}
    1)vi -d file1 file2------文件比较功能,很方便
    2)编辑一个文件的同时打开另一个文件  :sp file 或者 :vsp  file   横向和竖向比较,ctrl+w进行文件之间的切换,用于复制比较等还是不错
    3)函数原型查找、数据结构原型查找,现在一般的vi安装后会自带ctags,用于简单的查找也很方便,在源代码的目录中执行 $ctags -R*,执行完以后会在代码的目录下创建一个tag文件,在阅读代码的时候如果想查找函数的原型、数据的定义直接用ctrl+]跳转,返回用ctrl+t。很简单、很方便。
    补充俩,跟踪文件
    :$         直接到达文件底部
    :Num    直接到达Num行
    :0         直接到达顶部

    在有tag存在下,直接vi -t 函数名字,可以直达文件中的函数位置
     
     

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

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

    http://blog.chinaunix.net/uid-20564848-id-73068.html
    https://www.ibm.com/developerworks/cn/linux/l-vim-script-1/#resources

  • 相关阅读:
    JavaScrip 数组/字典/循环
    初识javaScript
    css内容补充之其它
    position
    css的存在形式
    CSS选择器
    Html的Head内标签
    Linux设置FQDN
    saltstack 全面介绍
    jQuery文档处理
  • 原文地址:https://www.cnblogs.com/guochaoxxl/p/12079094.html
Copyright © 2011-2022 走看看