1

当我遇到问题时,我试图在 Vim 7.3 中自定义我的状态行。

我正在尝试将 SVN 信息放在状态行中,所以我做了这样的事情:

function! DrawStatusLine()
    let svn = system("svn info")
    let l:status = " "
    let l:status = l:status . svn
    let l:status = l:status . "%t"       "tail of the filename
    let l:status = l:status . "%*"
    let l:status = l:status . "[%{strlen(&fenc)?&fenc:'none'}," "file encoding
    let l:status = l:status . "%{&ff}]" "file format
    let l:status = l:status . "%h"      "help file flag
    let l:status = l:status . "%m"      "modified flag
    let l:status = l:status . "%r"      "read only flag
    let l:status = l:status . "%="      "left/right separator
    let l:status = l:status . "%c,"     "cursor column
    let l:status = l:status . "%l/%L"   "cursor line/total lines
    let l:status = l:status . "\ %P"    "percent through file
    return l:status
endfunction

set statusline=%!DrawStatusLine()

但是在调用 sytem() 之后,光标移动得非常缓慢。system似乎每次我移动光标时都会被调用(实际上,它似乎每次在窗口中发生某些事情时都会被调用)。

你知道我为什么会出现这种行为吗?

这是我的其余部分.vimrc,我没有使用外来插件,而且我在 Cygwin (Windows XP) 下。

" When started as "evim", evim.vim will already have done these settings.
if v:progname =~? "evim"
    finish
endif

" Use Vim settings, rather than Vi settings (much better!).
" This must be first, because it changes other options as a side effect.
set nocompatible

" Management of console or GUI settings.
if has("gui_running")
    " We are in gVim
    " Linux
    if has("gui_gtk2")
        :set guifont=DejaVu\ Sans\ Mono\ 11
    " Windows
    elseif has("gui_win32")
        :set guifont=DejaVu_Sans_Mono:h11:cANSI:
    endif
else
    " We are in a console
    set background=dark
endif

" Manage colors.
if filereadable($VIMRUNTIME . "/colors/wombat256.vim") ||
            \ filereadable($VIM . "/vimfiles/colors/wombat256.vim") ||
            \ filereadable($HOME . "/.vim/colors/wombat256.vim")
    colorscheme wombat256
elseif filereadable($VIMRUNTIME . "/colors/wombat.vim") ||
            \ filereadable($VIM . "/vimfiles/colors/wombat.vim") ||
            \ filereadable($HOME . "/.vim/colors/wombat.vim")
    colorscheme wombat
else
    colorscheme desert
endif

" allow backspacing over everything in insert mode
set backspace=indent,eol,start
set history=50 " keep 50 lines of command line history
set ruler " show the cursor position all the time
set showcmd " display incomplete commands
set incsearch " do incremental searching

" For Win32 GUI: remove 't' flag from 'guioptions': no tearoff menu entries
let &guioptions = substitute(&guioptions, "t", "", "g")

" In many terminal emulators the mouse works just fine, thus enable it.
if has('mouse')
    set mouse=a
endif

" Switch syntax highlighting on, when the terminal has colors
" Also switch on highlighting the last used search pattern.
if &t_Co > 2 || has("gui_running")
    syntax on
    set hlsearch
endif

" Only do this part when compiled with support for autocommands.
if has("autocmd")

    " Enable file type detection.
    " Use the default filetype settings, so that mail gets 'tw' set to 72,
    " 'cindent' is on in C files, etc.
    " Also load indent files, to automatically do language-dependent indenting.
    filetype plugin indent on

    " Put these in an autocmd group, so that we can delete them easily.
    augroup vimrcEx
        autocmd!

        " For all text files set 'textwidth' to 78 characters.
        autocmd FileType text setlocal textwidth=78

        " When editing a file, always jump to the last known cursor position.
        " Don't do it when the position is invalid or when inside an event handler
        " (happens when dropping a file on gvim).
        " Also don't do it when the mark is in the first line, that is the default
        " position when opening a file.
        autocmd BufReadPost *
                    \ if line("'\"") > 1 && line("'\"") <= line("$") |
                    \   exe "normal! g`\"" |
                    \ endif

    augroup END

else

    set autoindent " always set autoindenting on

endif " has("autocmd")

" Convenient command to see the difference between the current buffer and the
" file it was loaded from, thus the changes you made.
" Only define it when not defined already.
if !exists(":DiffOrig")
    command DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis
                \ | wincmd p | diffthis
endif

" Update the path with the dir where we opened Vim
set path=.,$PWD/**
" Now that we set the path to be recursive, disable
" the option that looking for completion in included files.
" Indeed, it can slow the process hard. We use tags instead.
set complete-=i

" Allow editing everywhere
set virtualedit=all

" No bells
set errorbells
set novisualbell
set vb t_vb=

" Show status bar
set laststatus=2
let loaded_matchparen = 1

" Draw the status line.
" Status line that rocks.
function! DrawStatusLine()
    let svn = system("svn info")
    let l:status = " "
    let l:status = l:status . "%t"       "tail of the filename
    let l:status = l:status . "%*"
    let l:status = l:status . "[%{strlen(&fenc)?&fenc:'none'}," "file encoding
    let l:status = l:status . "%{&ff}]" "file format
    let l:status = l:status . "%h"      "help file flag
    let l:status = l:status . "%m"      "modified flag
    let l:status = l:status . "%r"      "read only flag
    let l:status = l:status . "%="      "left/right separator
    let l:status = l:status . "%c,"     "cursor column
    let l:status = l:status . "%l/%L"   "cursor line/total lines
    let l:status = l:status . "\ %P"    "percent through file
    return l:status
endfunction

set statusline=%!DrawStatusLine()

" Highlight current line
set cursorline

" Add visible lines when start or end of the screen (3 lines)
set scrolloff=3

" Backup
set nobackup

" No preview in ins-completion.
set completeopt=menu

" Commands completion on status line.
set wildmenu

" Don't redraw while executing macros
set lazyredraw

" K = :help
set keywordprg=

" Diff always vertical
set diffopt+=vertical

" Use utf-8
set encoding=utf-8
set fileencoding=utf-8

" Remember buffer changes when jumping around.
set hidden

"""""""""""""""""
" Developpement "
"""""""""""""""""

" Line numbers
set nu

" Tabulation of 4 spaces
set expandtab
set smarttab
set shiftwidth=4
set softtabstop=4
set tabstop=4

" Show when a line exceeds 80 chars
highlight Overlength ctermbg=red ctermfg=white guibg=#592929

" Highlight Tabs and Spaces
" highlight Tab ctermbg=darkgray guibg=darkgray
" au BufWinEnter * let w:m2=matchadd('Tab', '/[^\t]\zs\t\+/', -1)
highlight Space ctermbg=darkblue guibg=darkblue
augroup matches
    autocmd!
    autocmd BufWinEnter * match Overlength /\%81v.*/
    autocmd BufWinEnter * let w:m3=matchadd('Space', '\s\+$\| \+\ze\t', -1)
    " Matches are memory greedy, shut them when the window is left
    " Mybe it is redondant.
    autocmd BufWinLeave * call clearmatches()
augroup END

set list listchars=tab:\ \ ,trail:.


" Redraw status line when saving.
" autocmd BufWritePost * set statusline=%!DrawStatusLine()

" Special indentation for switch / case
" Indentation when in unclosed (.
set cino=l1,(0

" Load Doxygen syntax
let g:load_doxygen_syntax=1

"""""""""""""""""
" Taglist
"""""""""""""""""
let Tlist_Use_Right_Window=1

"""""""""""""""""
" cscope
"""""""""""""""""
if has("cscope") && executable("cscope") && !has("gui_win32")
    set csto=0
    set cst
    set nocsverb
    " add any database in current directory
    if filereadable("cscope.out")
        cs add cscope.out
    endif
    " abbreviations
    cnoreabbrev csf cs find
    set csverb
endif

"""""""""""""
"  Mapping  "
"""""""""""""

" Don't use Ex mode, use Q for formatting
map Q gq

" CTRL-U in insert mode deletes a lot.  Use CTRL-G u to first break undo,
" so that you can undo CTRL-U after inserting a line break.
inoremap <C-U> <C-G>u<C-U>

" With a map leader it's possible to do extra key combinations
" like <leader>w saves the current file
let mapleader = ","
let g:mapleader = ","

" After repeating command, return where we were.
map . .`[

" Switch tab.
noremap <A-h> gT
noremap <A-l> gt
" For dummy terminals
noremap <Esc>h gT
noremap <Esc>l gt

" Remap the Esc command
inoremap kj <Esc>
inoremap lk <Esc>

" Better for wrapped lines
nnoremap j gj
nnoremap k gk

" omnicompletion : words
inoremap <leader>, <C-x><C-o>

" Turn off highlighting in search.
nmap <leader>/ :nohlsearch<CR>

" edit .vimrc
nmap <silent> <leader>ev :tabnew $HOME/.vimrc<CR>
" source .vimrc
nmap <silent> <leader>sv :so $HOME/.vimrc<CR>

nnoremap <silent><leader>dh :call SVNDiff()<CR>

" Build C symbols.
function! BuildSymbols()
    if has("cscope") && executable("cscope") && !has("gui_win32")
        " kill all connection.
        execute "cs kill -1"
        execute "!ctags -R && cscope -Rb"
        execute "cs add cscope.out"
    else
        execute "!ctags -R"
    endif
endfunction

" Run Vim diff on HEAD copy in SVN.
function! SVNDiff()
    let fn = expand("%:p")
    let newfn = fn .  ".HEAD"
    let catstat = system("svn cat " . fn . " > " . newfn)
    if catstat == 0
        execute 'diffsplit ' . newfn
        execute 'set filetype=c'
    else
        echo "*** ERROR: svn cat failed for ". fn . " (as " . newfn . ")"
    endif
endfunction

" Build symbols with F2.
nnoremap <F2> :call BuildSymbols()<CR>

" Taglist with F3
nnoremap <F3> :TlistToggle<CR>

" Open a explorer on a vertical split of 26.
nnoremap <F4> :26Vexplore<CR>

" When you forgot to open the file as sudo.
cmap w!! %!sudo tee > /dev/null %
4

3 回答 3

5

'statusline'值一直在评估,它怎么能显示光标位置之类的信息?!不要在那里做耗时的事情;即使是长的 Vimscript 片段也会显着减慢 Vim,system()这是你能做的最糟糕的事情。

相反,在状态行(例如)中包含一个(缓冲区本地)变量%{exists('b:svn_info')?b:svn_info:''},并使用适当的自动命令来设置和更新它:

:autocmd BufRead * let b:svn_info = system('svn info')
于 2012-11-27T15:01:30.607 回答
2

现有答案的一个补充:有些插件已经自己拥有缓存(如aurum)或仅在 BufEnter 上完成一次工作(如VCSCommand)。提到的两个都能够替换您的SVNDiff功能。

VCSCommand 将提供文件状态(只有未知和新的)或(如果状态都不是)版本、存储库以及存储库中是否存在较新的版本。我必须在这里重复一遍,在您切换到另一个缓冲区然后再返回之前,它不会更新状态。

Aurum 更加灵活*,但除非您愿意每 N 秒经历一次输入延迟或切换到 mercurial,否则您将被迫仅使用文件状态和分支**(最后一个左侧是当前修订信息)。颠覆状态总是映射到八种变化无常的状态之一。除非您使用 +python 编译 vim,否则它会使用缓存方法(并且缓存失效会使您遭受延迟),但是使用 +python 情况有所不同:每 N 秒在一个单独的进程中获取状态,并且不会因任何延迟而打扰您除非你做了一些导致缓存失效的事情(目前使用 +python,你会在失效发生的那一刻遭受滞后,并且不会注意到它认为这是你的操作的一部分导致了失效,但是没有滞后被认为是发生的当状态行无效时)。

* 从文档中,我猜 VCSCommand 作者打算让用户自己编写更好的状态行,但我没有看到任何可以帮助他们的东西。也许我只是在寻找错误的位置。

** 此处的分支表示“存储库根 URL 中不存在的根目录 URL 的尾随部分”。根目录是嵌套最少的目录,其.svn子目录和存储库根 URL 等于当前目录(包含当前缓冲区的目录)之一。

即使您不愿意使用 aurum,也可以随意借用重复执行的命令或缓存实现。

于 2012-11-27T16:07:24.827 回答
1

这是预期的行为:每次移动光标时都会更新整个状态行,因此svn info每次执行某些操作时都会执行。可能每秒多次。

这显然是一种浪费,因为当前文件的 svn 状态不会每秒改变 10 次。

n您应该缓存此信息,并且仅在写入时或每分钟或沿该行的某些时间检索它。

于 2012-11-27T14:53:36.310 回答