为什么重新映射<Esc>会使光标跳转?


9

.vimrc

inoremap ii <esc>                               " ii to go back into command mode

问题是,在之后ii,光标会向右跳35列。唯一会影响Esc以下内容的行:

nnoremap <esc><esc> :noh<return><esc>

我尝试将其注释掉,但无济于事。


完整.vimrc

set nocompatible
filetype off


" Vundle ---------------------------------------------------------------------
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'gmarik/Vundle.vim'
Plugin 'bling/vim-airline'
Plugin 'vim-airline/vim-airline-themes'
Plugin 'scrooloose/nerdtree'
Plugin 'scrooloose/nerdcommenter'
Plugin 'scrooloose/syntastic'
Plugin 'freitass/todo.txt-vim'
Plugin 'plasticboy/vim-markdown'
Plugin 'tpope/vim-fugitive'                     " :GitCommands
Plugin 'tpope/vim-git'                          " allows to see branch in airline
Plugin 'airblade/vim-gitgutter'                 " git status in SL area
Plugin 'ctrlpvim/ctrlp.vim'                     " TODO: learn more about this
Plugin 'Shougo/neocomplete.vim'                 " TAB completion
" Plugin 'tmhedberg/SimpylFold'                 " should we add FastFold?
Plugin 'spf13/vim-autoclose'                    " autoclose brackets(properly)
Plugin 'godlygeek/tabular'                      " V -> : -> /SYMBOL_TO_LINE-UP
Plugin 'terryma/vim-multiple-cursors'           " steep learning curve
Plugin 'Yggdroot/indentLine'                    " shows | intendation
Plugin 'mhinz/vim-startify'                     " start screen
Plugin 'hashivim/vim-vagrant'
Plugin 'pearofducks/ansible-vim'
Plugin 'whatyouhide/vim-gotham'
Plugin 'ryanoasis/vim-devicons'                 " glyph icons everywhere
call vundle#end()


" System ---------------------------------------------------------------------
set lazyredraw                                  " do we need this?
set ttyfast                                     " theoretically make things faster
set mouse=a                                     " enable mouse
set guicursor=a:blinkon0                        " don't blink with the cursor
set termencoding=utf-8                          " terminal is UTF-8
set encoding=utf8                               " ...as well as the editor
set fileformat=unix                             " *nix format
set clipboard=unnamed                           " Fix clipboard in *nix
set wildmenu
set wildmode=longest:full,full
set wildignore=*.o,*.pyc,*.DS_Store
set magic                                       " needed for regexp
set hidden                                      " allow switching buffers w/o saving
set nojoinspaces                                " don't join spaces on paste
set formatoptions-=r formatoptions-=c formatoptions-=o " don't autoextend comments
set lines=40 columns=90                         " default size for GUI
set gdefault                                    " replace all without /g
set virtualedit=all                             " keep cursor horizontal pos. when scrolling

set backup                                      " backups ON
set undofile                                    " persistent undo
set backupdir=~/.vim/backup
set directory=~/.vim/tmp
set undolevels=1000                             " max number of undos
set undoreload=10000                            " max lines to to save for undo
set history=1000                                " 1000 of history (what?)

set incsearch                                   " incremental search
set smartcase                                   " ... and case sensitive if Uppercase is present

set expandtab                                   " spaces instead of tabs
set smarttab                                    " smart tabs/intends
set tabstop=4                                   " TAB = 4 spaces
set softtabstop=4
set smartindent
set autoindent
set shiftwidth=4
set shiftround                                  " round to 4

let NERDSpaceDelims=1                           " add space before comment
let NERDTreeMapOpenInTab='\r'                   " open files in new tab
let g:vim_markdown_folding_disabled=1           " don't fold *.md

let g:neocomplete#enable_at_startup = 1         " NeoComplete
let g:neocomplete#enable_auto_select = 1
let g:neocomplete#enable_smart_case = 1
let g:neocomplete#min_keyword_length = 2        " min symbols before autocomplete


" Visuals --------------------------------------------------------------------
filetype plugin indent on                       " enable filetype plugins
syntax enable                                   " enable syntax highlighting
let python_highlight_all=1                      " full python highlighting
colorscheme gotham256                           " GUI colours
let g:airline_theme = "gotham256"               " airline theme
if has("gui_running")
    set cul
endif

set title                                       " set terminal title
set t_Co=256                                    " 256 colors terminal
set guifont=Meslo\ LG\ S\ Regular\ for\ Powerline\ Nerd\ Font\ Complete\ Windows\ Compatible:h12
set guioptions=L                                " disable right scrlbar
set guioptions=R                                " disable left scrlbar
set number                                      " show line numbers
set nowrap                                      " don't wrap long lines
set fillchars+=vert:\?                          " vertical unicode split
set shortmess+=I                                " remove startup message

set splitbelow                                  " Natural splits
set splitright

set hlsearch                                    " highlight search results
set showmatch                                   " show matching brackets

set noerrorbells                                " disable bells
set novisualbell
set vb t_vb=

set laststatus=2                                " always show airline
let g:airline_powerline_fonts=1                 " use glyph fonts
let g:airline#extensions#tabline#enabled=1      " enable tabline (top)
let g:airline#extensions#tabline#fnamemod=':t'  " filename only

set completeopt-=preview                        " don't show doc. window by default
let g:SimplylFold_docstring_preview=1           " keep docstring unfolded.

let g:gitgutter_sign_added = '?'                " Gitgutter
let g:gitgutter_sign_modified = '•'
let g:gitgutter_sign_removed = '?'
let g:indentLine_char = '¦'                     " intendation vertical lines


" Hotkeys --------------------------------------------------------------------
nnoremap <F1> <nop>                             " F1 does noothing
map <F2> :NERDTreeToggle<CR>                    " F2 to open NerdTree
set pastetoggle=<F3>                            " F3 to switch in paste mode
map <F4> :mksession! ~/.vim/vim_session<CR>     " F4 to write current session
map <F5> :source ~/.vim/vim_session<CR>         " F5 to load prev. session

inoremap ii <esc>                               " ii to go back into command mode
let mapleader = "\<Space>"                      " remap leader key from '\' to Space
map <leader>cd :cd %:p:h<cr>:pwd<cr>            " CWD to the dir of the open buffer
nnoremap <esc><esc> :noh<return><esc>           " 2xESC clears search highlights

nnoremap <C-J> <C-W><C-J>                       " switch splits with CTRL+hjkl
nnoremap <C-K> <C-W><C-K>
nnoremap <C-L> <C-W><C-L>
nnoremap <C-H> <C-W><C-H>

nmap <leader>l :bn<CR>                          " next buffer by Space+l
nmap <leader>h :bp<CR>                          " previous buffer by Space+h
nmap <leader>q :bp <BAR> bd #<CR>               " close buffer by Space+q

vnoremap < <gv                                  " leave txt selected after <>
vnoremap > >gv

set t_ku=OA                                   " fix arrow keys
set t_kd=OB
set t_kr=OC
set t_kl=OD

nmap <leader>f1 :set foldlevel=0<CR>            " change folding levels
nmap <leader>f2 :set foldlevel=1<CR>
nmap <leader>f3 :set foldlevel=2<CR>
nmap <leader>f4 :set foldlevel=3<CR>

nnoremap <silent> <leader>gs :Gstatus<CR>       " GIT shortcuts
nnoremap <silent> <leader>gd :Gdiff<CR>
nnoremap <silent> <leader>gc :Gcommit<CR>
nnoremap <silent> <leader>gb :Gblame<CR>
nnoremap <silent> <leader>gl :Glog<CR>
nnoremap <silent> <leader>gp :Git push<CR>
nnoremap <silent> <leader>gr :Gread<CR>
nnoremap <silent> <leader>gw :Gwrite<CR>
nnoremap <silent> <leader>ge :Gedit<CR>
nnoremap <silent> <leader>ga :Git add %:p<CR>

set backspace=eol,start,indent                  " esoteric backspace fix
set whichwrap+=<,>                              " move to another line with <- ->

if bufwinnr(1)                                  " resize splits with +-
  map + <C-W>+
  map - <C-W>-
endif

" NeoComplete
" <CR>: close popup and save indent.
inoremap <silent> <CR> <C-r>=<SID>my_cr_function()<CR>
function! s:my_cr_function()
    " return neocomplete#close_popup() . "\<CR>"
    " For no inserting <CR> key.
    return pumvisible() ? neocomplete#close_popup() : "\<CR>"
endfunction

" <TAB>: completion.
inoremap <expr><TAB>  pumvisible() ? "\<C-n>" : "\<TAB>"


" Macros ---------------------------------------------------------------------

" Emulate any other editor with tabs
try
 set switchbuf=useopen,usetab,newtab
catch
endtry

" Close vim if NerdTree is the only buffer left
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTreeType") && b:NERDTreeType == "primary") | q | endif

" Return to last edit position when opening files
autocmd BufReadPost *
    \ if line("'\"") > 0 && line("'\"") <= line("$") |
    \   exe "normal! g`\"" |
    \ endif

" Python with virtualenv support
py << EOF
import os
import sys
if 'VIRTUAL_ENV' in os.environ:
  project_base_dir = os.environ['VIRTUAL_ENV']
  activate_this = os.path.join(project_base_dir, 'bin/activate_this.py')
  execfile(activate_this, dict(__file__=activate_this))
EOF

" Enable omni completion.
autocmd FileType css setlocal omnifunc=csscomplete#CompleteCSS
autocmd FileType html,markdown setlocal omnifunc=htmlcomplete#CompleteTags
autocmd FileType javascript setlocal omnifunc=javascriptcomplete#CompleteJS
autocmd FileType python setlocal omnifunc=pythoncomplete#Complete
autocmd FileType xml setlocal omnifunc=xmlcomplete#CompleteTags

" Enable heavy omni completion.
if !exists('g:neocomplete#sources#omni#input_patterns')
  let g:neocomplete#sources#omni#input_patterns = {}
endif

" format JSON code
com! FormatJSON %!python -m json.tool

" BG fix for tmux
set t_ut=

1
可能不是解决方案,但应避免在映射中出现一些意外的副作用inoremap ii <Esc>。我认为这不会解决您的问题,但这仍然是最佳做法。也可以请您完整填写.vimrc:这可以帮助我们帮助您;-)
statox

@statox,更改为inoremap,效果相同。添加了到vimrc的链接。谢谢:]
asv

Answers:


20

您的问题是您将注释放在行尾,并以空格分隔。Vim会将这些空格解释为命令和映射的一部分。

如果您更换:

inoremap ii <esc>                               " ii to go back into command mode

通过

" ii to go back into command mode
inoremap ii <Esc>

您将解决您的问题。

一般而言,请勿在您的行末添加注释.vimrc


另外,这是一种以.vimrc适当格式转换您的方法。

编辑使用此命令。(与原始字符相比,\r可以直接复制换行符,而不必使用它^M。谢谢@Sato!)

 :%s/\(^.*\)\s\+\(".*\)/\2\r\1

原始命令

 :%s/\(^.*\)\s\+\(".*\)/\2^M\1

(要^M正确插入,请使用Ctrl-vEnter插入真实的换行符)

此命令将捕获在行尾包含注释的行的开头,并用包含注释的两行和命令替换整行。

(请注意,该命令似乎使函数混乱,my_cr_function因此您可能要检查它是否会造成麻烦,.vimrc也许可以替换%为某些范围并.vimrc逐步更改)


@Sato还建议此链接有关注释和多行的工作方式,值得阅读。


2
@fruglemonkey是正确的,但我不建议您这样做是一个好习惯:以这种方式使用bar是-我认为-是潜在的难以发现的bug的来源。忘记酒吧并花费大量时间试图找出导致问题的原因很容易。我认为将评论放在单独的行中比较安全:-)
statox

3
对于它的价值,这篇文章解释了VimL中注释的确切含义。
佐藤桂2016年

4
Yon也可以用^M替换替换字符串\r
佐藤桂2016年

4
遗憾的是\r,并非在所有情况下都有效,因为您必须\n在搜索模式中使用它。但这确实在这里起作用。
佐藤桂2016年

1
如果您阅读Sato的链接,则会看到“某些命令定义允许在其上下文中使用注释,因此set ai " Set &autoindent可以"像换行符或bar一样起作用,但这不适用于:let。”。在某些情况下,可以在行末,但我想我从未见过.vimrc使用它们的好方法,因此,我建议您不要使用它们。
statox
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.