我使用了很多插件 —— 也许其中一个就是罪魁祸首。这是我的 .vimrc,如果有人感兴趣的话:
" Character encoding (if this is not set, all manner of hell breaks loose when
" LC_CYTPE is set to anything unexpected.)
set encoding=utf-8
" Pre-plugin stuff
let g:Powerline_symbols = 'fancy' " Awesome forward-facing arrows for powerline
" let g:ctrlp_cmd = 'CtrlPMixed'
let g:ctrlp_switch_buffer = 2
let g:ctrlp_reuse_window = 'netrw\|help\|quickfix'
let g:ctrlp_clear_cache_on_exit = 1 " retain cache on exit (might mean I have to manually refresh every now and again)
let g:ctrlp_open_new_file = 't' " <c-y> opens file in new tab
let g:ctrlp_arg_map = 0 " for <c-z> and <c-o>
let g:ctrlp_extensions = ['tag', 'buffertag', 'quickfix', 'dir', 'rtscript', 'undo', 'line', 'changes', 'mixed', 'bookmarkdir']
let g:ctrlp_root_markers = ['Gemfile', 'README']
" Load vim plugins using vundle!
set rtp+=~/.vim/bundle/vundle
call vundle#rc()
Bundle 'gmarik/vundle'
Bundle 'mileszs/ack.vim'
Bundle 'kien/ctrlp.vim'
Bundle 'msanders/snipmate.vim'
Bundle 'majutsushi/tagbar'
Bundle 'altercation/vim-colors-solarized'
Bundle 'tpope/vim-commentary'
Bundle 'tpope/vim-endwise'
Bundle 'tpope/vim-eunuch'
Bundle 'tpope/vim-fugitive'
Bundle 'tpope/vim-markdown'
Bundle 'lokaltog/vim-powerline'
Bundle 'vim-ruby/vim-ruby'
Bundle 'SuperTab'
Bundle 'sjl/vitality.vim'
Bundle 'Syntastic'
set wildignore+=doc*,*.png,*.jpg,*.bmp,*.gif,*.jpeg
" enhance command line completion
set wildmenu
" make my leader the comma
let mapleader = ","
" we're running vim, not vi
set nocompatible
" enable per-directory .vimrc files and disable unsafe commands in them
set exrc
set secure
" folding stuff
set foldmethod=syntax
set foldcolumn=3
set foldlevelstart=99
" use exuberant ctags
let g:tagbar_ctags_bin = '/usr/local/bin/ctags'
let g:tagbar_compact = 1
map <leader>b :TagbarOpenAutoClose<CR>
set t_Co=256
" optimize for fast terminal connections
set ttyfast
" don't add empty newlines at the end of files
set binary
set noeol
set mouse=a
set expandtab
set tabstop=2
set shiftwidth=2
set autoindent
set smartindent
set autochdir
set showmode
" show the cursor position
set ruler
" show the (partial) command as it's being typed
set showcmd
" line numbers
set number
" start scrolling three lines before the horizontal window border
set scrolloff=3
" always show the status line
set laststatus=2
" make backspace delete over line brakes, auto indentation, and the place where insert mode began
set backspace=2
set switchbuf+=usetab
" Don't reset cursor to the start of the line when moving around
set nostartofline
" search stuff
set incsearch " highlight dynamically as pattern is typed
set ignorecase " ignore case of searches
set gdefault " adds the global flag to search/replace by default
set hlsearch " highlight search results
" use mac os clipboard as default paste register
" set clipboard=unnamed
" allow cursor beyond last character
set virtualedit=onemore
" history
set history=1000
" syntax stuff
syntax on " highlighting please
" Syntax coloring
set background=dark
let g:solarized_termcolors = 256
let g:solarized_termtrans = 1
colorscheme solarized
" Highlight current line
set cursorline
hi cursorline guibg=#333333
hi CursorColumn guibg=#333333
" don't show the intro message when starting vim
set shortmess=atI
" show the filename in the window titlebar
set title
" respect modeline in files
set modeline
set modelines=4
" disable error bells
set noerrorbells
set visualbell
" Resizing of windows
map + <C-w>+
map _ <C-w>-
map ) <C-w>>
map ( <C-w><
set equalalways
" Create directories if they don't exist
silent execute '!mkdir -p $HOME/.vimbackup'
silent execute '!mkdir -p $HOME/.vimswap'
silent execute '!mkdir -p $HOME/.vimviews'
" Directories for backups
set backup
set backupdir=$HOME/.vimbackup//
set directory=$HOME/.vimswap//
set viewdir=$HOME/.vimviews//
if exists("&undodir")
set undodir=$HOME/.vimundo//
endif
" make vim save view (state) (folds, cursor, etc) and then load view again.
au BufWinLeave * silent! mkview
au BufWinEnter * silent! loadview
" Use the same symbols as TextMate for tabstops and EOLs
set listchars=tab:▸\ ,eol:¬
" Make 'kj' in insert mode bring you back to edit mode
inoremap kj <Esc>
" Fold / unfold current block of code
map <leader>a za
" gundo
" map <leader>u :GundoToggle<CR> " Graphical representation of the undo tree
" find with Ack, but in current project
function! AckInProject(command, search)
execute ":".a:command." ".a:search." --nohtml --nosql ".system("git rev-parse --show-toplevel")
endfunction
command! -nargs=1 Projfind call AckInProject('Ack', '<args>')
command! -nargs=1 ProjfindAppend call AckInProject('AckAdd', '<args>')
map <leader>f :Projfind
map <leader>d :ProjfindAppend
" open new windows
map <leader>sl :vsplit<CR><C-W>l
map <leader>sh :vsplit<CR><C-W>h
map <leader>sj :split<CR><C-W>j
map <leader>sJ :split<CR><C-W>jG
map <leader>sk :split<CR>
map <leader>sK :split<CR>gg
map <leader>t :tabnew<CR>
" move between windows
map gj <C-w>j
map gk <C-w>k
map gl <C-w>l
map gh <C-w>h
map g= <C-w>=
" move between tabs
nnoremap <C-h> gT
nnoremap <C-l> gt
" move between quickfix errors
nnoremap <C-j> :cp<CR>
nnoremap <C-k> :cn<CR>
" ON-THE-FLY SETTINGS CHANGING
" Edit .vimrc (this file)
map <leader>sv :sp ~/.vimrc<CR><C-W>_
" Edit .zshrc
map <leader>sz :e ~/.zshrc<CR><C-W>_
map <leader>sr :source ~/.vimrc<CR><C-W>_
map <leader>si :set list!<CR> " Show/hide invisibles
map <leader>ss :set hlsearch!<CR> " Toggle search highlighting
" Vimux stuff
map <Leader>c :PromptVimTmuxCommand<CR>
map <Leader>C :RunLastVimTmuxCommand<CR>
map <Leader>rk :InspectVimTmuxRunner<CR>
map <Leader>rq :CloseVimTmuxRunner<CR>
map <Leader>rx :CloseVimTmuxPanes<CR>
vmap <Leader>rr "vy :call RunVimTmuxCommand(@v . "\n", 0)<CR>
nmap <Leader>rr vip<Leader>rr<CR>
" git
map <Leader>gg :Gstatus<CR>
map <Leader>gc :Gcommit<CR>
map <Leader>gb :Gblame<CR>
map <Leader>gp :Git pull<CR>
map <Leader>gP :Git push<CR>
" indentation in ruby
set cinoptions=:0,p0,t0
set cinwords=if,else,while,do,for,switch,case
" enable filetype detection
filetype on
filetype indent on
filetype plugin on
" commentary filetypes
autocmd FileType ruby set commentstring=#\ %s
autocmd FileType vim set commentstring=\"\ %s
" commenting w/ commentary
map <C-\> \\\
" syntastic error checking
let g:syntastic_auto_loc_list=1 " auto open error window when errors are detected
let g:syntastic_check_on_open=1 " check for errors on file open
let g:syntastic_error_symbol='✗'
let g:syntastic_warning_symbol='⚠'
map <leader>e :SyntasticCheck<CR>:Errors<cr><C-w>j
" strip trailing whitespace on save
" autocmd BufWritePre * :%s/\s\+$//e
答案1
事实证明它是我的 .vimrc 中的以下行:
set foldmethod=syntax
根据 vim 的帮助 ( :help foldmethod
),该syntax
设置导致 vim 使用语法高亮来确定如何自动折叠我的代码。每次我输入一个字符时,它肯定都在解析整个文件的语法。
不管怎样,Zoredache 建议我删除 .vimrc 文件中的所有内容,然后逐一添加回来,这有助于我找到问题所在。
答案2
我遇到了类似的问题并制作了插件
https://github.com/Konfekt/FastFold
在保存缓冲区时,它会根据您首选的折叠方法更新当前编辑缓冲区中的折叠,否则将保持原样(通过将折叠方法保持为手动)。