Vim 在启动时出现意外的按键,这可能是什么原因造成的?

Vim 在启动时出现意外的按键,这可能是什么原因造成的?

每当我用 vim 打开一个文件时,vim 会话2c都会自动/以编程方式“按下”开始。这意味着,如果我开始尝试通过按下 向下移动文件j,则会执行该操作2cj,这意味着两行将被删除。因此,每次我启动 vim 会话时,我都必须<esc>先按下 才能正常继续。这变得很乏味,并且会破坏我的 vim 肌肉记忆。

我多次查看过我的 .vimrc 文件,但还是不明白是什么原因导致的。也许这和 .vimrc 文件没有任何关系。不过,这是我的 .vimrc 文件:

" vim settings


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Vundle Settings - https://github.com/gmarik/vundle
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

set nocompatible " not compatible with vi
filetype off
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()

" let vundle manage vundle
Bundle 'gmarik/vundle'

" my vim plugins
"Bundle 'kien/ctrlp.vim'
"Bundle 'scrooloose/nerdtree'
"Bundle 'mileszs/ack.vim'
" Bundle 'scrooloose/syntastic'
"Bundle 'Raimondi/delimitMate'
"Bundle 'tpope/vim-commentary'
"Bundle 'tpope/vim-endwise'
Bundle 'tpope/vim-fugitive'
"Bundle 'othree/html5.vim'
"Bundle 'pangloss/vim-javascript'
"Bundle 'itspriddle/vim-jquery'
"Bundle 'tpope/vim-markdown'
"Bundle 'mmalecki/vim-node.js'
"Bundle 'Lokaltog/vim-powerline'
"Bundle 'tpope/vim-ragtag'
"Bundle 'tpope/vim-surround'
" Bundle 'sjl/vitality.vim'
"Bundle 'duff/vim-scratch'
"Bundle 'cakebaker/scss-syntax.vim'
"Bundle 'kchmck/vim-coffee-script'
"Bundle 'groenewege/vim-less'
"Bundle 'nono/vim-handlebars'
"Bundle 'tsaleh/vim-matchit'
"Bundle 'benmills/vimux'
"Bundle 'mattn/zencoding-vim'
"Bundle 'int3/vim-taglist-plus'

filetype plugin indent on


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => General
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

"""" Abbreviations
abbr funciton function
abbr teh the
abbr tempalte template

"set winwidth=120

set autoread " detect when a file is changed

" make backspace behave in a sane manner
set backspace=indent,eol,start

" set a map leader for more key combos
let mapleader = ","
let g:mapleader = ","

" change history to 1000
set history=10000

" Tab control
set smarttab
set expandtab
set shiftwidth=4
set tabstop=4
set softtabstop=4

" faster redrawing
set ttyfast

" allow matching of if/end, etc. with %
" runtime macros/matchit.vim

" highlight conflicts
match ErrorMsg '^\(<\|=\|>\)\{7\}\([^=].\+\)\?$'

" Enable file type detection and do language dependent indenting
filetype plugin indent on

" file type specific settings
if has("autocmd")
    autocmd FileType yaml setlocal ts=2 sts=2 sw=2 expandtab
    autocmd FileType make setlocal ts=8 sts=8 sw=8 noexpandtab
    autocmd FileType ruby setlocal ts=2 sts=2 sw=2 expandtab
    autocmd FileType html setlocal ts=4 sts=4 sw=4 expandtab
    autocmd FileType jade setlocal ts=2 sts=2 sw=2 noexpandtab
    "autocmd WinEnter * setlocal cursorline
    "autocmd WinLeave * setlocal nocursorline

    " automatically resize panes on resize
    autocmd VimResized * exe "normal! \<c-w>="
    autocmd BufWritePost .vimrc source $MYVIMRC
    " save all files on focus lost, ignoring warnings about untitled buffers
    autocmd FocusLost * silent! wa
    autocmd BufNewFile,BufRead *.json set filetype=javascript
    autocmd BufNewFile,BufRead *.ejs set filetype=html
endif

" code folding settings
set foldmethod=syntax " fold based on indent
set foldnestmax=10 " deepest fold is 10 levels
set nofoldenable " don't fold by default
set foldlevel=1

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => User Interface
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

set wildmenu " enhanced command line completion
set hidden " current buffer can be put into background
set showcmd " show incomplete commands
set noshowmode " don't show which mode disabled for PowerLine
set wildmode=list:longest " complete files like a shell
set scrolloff=10 " lines of text around cursor
set shell=/bin/zsh
set ruler " show postiion in file
set cmdheight=1 " command bar height

set title " set terminal title

" Searching
set ignorecase " case insensitive searching
set smartcase " case-sensitive if expresson contains a capital letter
set hlsearch
set incsearch " set incremental search, like modern browsers
set nolazyredraw " don't redraw while executing macros

set magic " Set magic on, for regex

set showmatch " show matching braces
set mat=2 " how many tenths of a second to blink

" switch to line when editing and block when not
" let cursor_to_bar   = "\<Esc>]50;CursorShape=1\x7"
" let cursor_to_block = "\<Esc>]50;CursorShape=0\x7"
" let &t_SI = cursor_to_bar
" let &t_EI = cursor_to_block

" error bells
set noerrorbells
set visualbell
set t_vb=
set tm=500

" switch syntax highlighting on
syntax on

set background=dark
colorscheme Tomorrow-Night-Bright

" set number " show line numbers
set relativenumber " show relative line numbers

set wrap " turn on line wrapping
"set nowrap "turn off line wrapping
set wrapmargin=8 " wrap lines when coming within n characters from side
set linebreak " set soft wrapping
set showbreak=… " show ellipsis at breaking

set autoindent " automatically set indent of new line

set encoding=utf8
set t_Co=256 " Explicitly tell vim that the terminal supports 256 colors"
try
    lang en_US
catch
endtry

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Files, backups, and undo
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

"set nobackup
"set nowritebackup
"set noswapfile
set backupdir=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp
set directory=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => StatusLine
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
set laststatus=2 " show the satus line all the time

" the status line is set by the powerline plugin

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Mappings
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" General mappings/shortcuts for functionality
" Additional, plugin-specific mappings are located under
" the plugins section

" clear search results on escape
nnoremap <silent> <esc> :nohlsearch<cr><esc>

" remap esc
inoremap jk <esc>

" remove extra whitespace
nmap <leader><space> :%s/\s\+$<cr>

" shortcut to save
nmap <leader>, :w<cr>

" set paste toggle
set pastetoggle=<F6>

" toggle paste
map <leader>v :set paste!<cr>

" edit ~/.vimrc
map <leader>e :e! ~/.vimrc<cr>

" clear highlighted search
noremap <space> :set hlsearch! hlsearch?<cr>

" toggle invisible characters
set invlist
set listchars=tab:▸\ ,eol:¬,trail:⋅,extends:❯,precedes:❮
set showbreak=↪
nmap <leader>l :set list!<cr>

" Textmate style indentation
vmap <leader>[ <gv
vmap <leader>] >gv
nmap <leader>[ <<
nmap <leader>] >>

" buffer shortcuts
nmap <leader>n :bn<cr> " go to next buffer
nmap <leader>p :bp<cr> " go to prev buffer
nmap <leader>q :bd<cr> " close the current buffer
" switch between current and last buffer
nmap <leader>. <c-^>

" enable . command in visual mode
vnoremap . :normal .<cr>

" Window movement shortcuts
" move to the window in the direction shown, or create a new window
function! WinMove(key)
    let t:curwin = winnr()
    exec "wincmd ".a:key
    if (t:curwin == winnr())
        if (match(a:key,'[jk]'))
            wincmd v
        else
            wincmd s
        endif
        exec "wincmd ".a:key
    endif
endfunction

map <silent> <C-h> :call WinMove('h')<cr>
map <silent> <C-j> :call WinMove('j')<cr>
map <silent> <C-k> :call WinMove('k')<cr>
map <silent> <C-l> :call WinMove('l')<cr>

map <leader>wc :wincmd q<cr>

" equalize windows
map <leader>= <C-w>=

" toggle cursor line
nnoremap <leader>i :set cursorline!<cr>

" scroll the viewport faster
nnoremap <C-e> 3<C-e>
nnoremap <C-y> 3<C-y>

" moving up and down work as you would expect
nnoremap <silent> j gj
nnoremap <silent> k gk

" toggle search result highlighting on/off
map  <F12> :set hls!<CR>
imap <F12> <ESC>:set hls!<CR>a
vmap <F12> <ESC>:set hls!<CR>gv

function! ToggleNuMode()
    if (&rnu == 1)
        setlocal nu
    else
        setlocal rnu
    endif
endfunction

nnoremap <leader>/ :call ToggleNuMode()<cr>

" find out what syntax stack a statement belongs to
nmap <leader>s :call <SID>SynStack()<cr>
function <SID>SynStack()
    if !exists("*synstack")
        return
    endif
    echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')
endfunction

function! ApplyLocalSettings(dirname)
    " convert windows paths to unix style
    let l:curDir = substitute(a:dirname, "\\", "/", "g")

    " walk to the top of the dir tree
    let l:parentDir = strpart(l:curDir, 0, strridx(l:curDir, "/"))
    if isdirectory(l:parentDir)
        call ApplyLocalSettings(l:parentDir)
    endif

    " now walk back down the path and source .vimsettings as you find them.
    " child directories can inherit from their parents
    let l:settingsFile = a:dirname . "/.vimrc.local"
    if filereadable(l:settingsFile)
        exec ":source" . l:settingsFile
    endif
endfunction
" autocmd! BufEnter * call ApplyLocalSettings(expand("<afile>:p:h"))

" smart tab completion
function! Smart_TabComplete()
    let line = getline('.')                         " current line

    let substr = strpart(line, -1, col('.')+1)      " from the start of the current
    " line to one character right
    " of the cursor
    let substr = matchstr(substr, "[^ \t]*$")       " word till cursor
    if (strlen(substr)==0)                          " nothing to match on empty string
        return "\<tab>"
    endif
    let has_period = match(substr, '\.') != -1      " position of period, if any
    let has_slash = match(substr, '\/') != -1       " position of slash, if any
    if (!has_period && !has_slash)
        return "\<C-X>\<C-P>"                         " existing text matching
    elseif ( has_slash )
        return "\<C-X>\<C-F>"                         " file matching
    else
        return "\<C-X>\<C-O>"                         " plugin matching
    endif
endfunction
inoremap <tab> <c-r>=Smart_TabComplete()<CR>

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" => Plugins
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" Powerline settings
" must have modified font installed to enable fancy
" let g:Powerline_symbols = 'fancy'

" let g:Powerline_stl_path_style = 'filename'

" close NERDTree after a file is opened
let g:NERDTreeQuitOnOpen=1
" Toggle NERDTree
nmap <silent> <leader>k :NERDTreeToggle<cr>
" expand to the path of the file in the current buffer
nmap <silent> <leader>y :NERDTreeFind<cr>

" map fuzzyfinder (CtrlP) plugin
nmap <silent> <leader>t :CtrlP<cr>
nmap <silent> <leader>r :CtrlPBuffer<cr>

" Fugitive Shortcuts
nmap <silent> <leader>gs :Gstatus<cr>

" CtrlP ignore patterns
let g:ctrlp_custom_ignore = {
            \ 'dir': '\.git$\|node_modules$\|\.hg$\|\.svn$',
            \ 'file': '\.exe$\|\.so$'
            \ }

" search the nearest ancestor that contains .git, .hg, .svn
let g:ctrlp_working_path_mode = 2

我并不指望任何人都会读完所有这些内容,但我真的不确定其中的哪一部分可能会导致这个问题。

是否有人知道什么原因导致这些按键在 vim 会话开始时自动添加?

编辑

我在 Mac 上安装了 OS X 10.8,我使用苹果的默认终端程序。我也在使用 oh-my-zsh,但我认为这不会有什么影响。

答案1

这可能是来自终端的 termresponse 序列。尝试启动 vim 作为

$ vim --cmd 'set t_RV='

您也可以尝试在 vim 中执行此操作:

:echo v:termresponse

看看它是否包含2c

当我在后台的服务器上从台式机上的终端仿真器运行 vim 时,我曾经遇到过类似的问题。termresponse 序列从终端返回到 vim 需要很长时间,以至于部分序列被接受为好像来自键盘。我最终禁用了 termresponse 查询,并在 ~/.vimrc 中“手动”设置了“ttymouse”。请参阅

:help 'ttymouse'
:help t_RV
:help v:termresponse

编辑

我从该系统中找到了 ~/.vimrc 文件。下面是我修复该问题时使用的内容。


set t_RV= ttymouse=xterm2   " Temporary (?) kludge to avoid problems
                            " when starting vim with "-c grep
                            " something" where the termresponse puts
                            " vim into insert mode.  [2006-01-18]
                            " 'ttymouse' must be set to "xterm2"
                            " manually to get the mouse to drag window
                            " borders because vim needs the
                            " termresponse to set it correctly
                            " automatically.

相关内容