Mapping “jj” to <ESC> causes cursor to try to jump forward

谁说胖子不能爱 提交于 2019-12-11 01:45:09

问题


I have begun debugging my .vimrc file after migrating to windows (see related question here). In Ubuntu, I mapped the jj key combination to ESC like so

inoremap jj <Esc>                       

Although pressing jj does help get me out of insert mode, it tries to seek forward 10 or 15 characters (I haven't actually counted). Here are some other things that may be relevant,

  • ; enters command mode but q; does not give me the command mode history, I have to type q:
  • I have mapped SHIFT + J (<S-J>) to previous buffer command (bp). It too moves the cursor left/right. Similarly for SHIFT + K (<S-K>) mapped to bn
  • j and k commands behave normally
  • It seems to be only with cases where the ENTER or ESC buttons are triggered

Here is my vimrc file in case it helps

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

" Swap ; and :  Convenient.
nnoremap ; :
nnoremap : ;

" Create Blank Newlines and stay in Normal mode
nnoremap <silent> zj o<Esc>
nnoremap <silent> zk O<Esc>

"Make cursor move as expected with wrapped lines:
inoremap <Down> <C-o>gj
inoremap <Up> <C-o>gk

"Map Shift+ J to previous buffer
noremap <S-J> :bp<CR>           

"Map Shift + K to next buffer
noremap <S-k> :bn<CR>               

"Turn on syntax (I guess)
syntax on

" Needed for Syntax Highlighting and stuff
filetype on
filetype plugin on
syntax enable
set grepprg=grep\ -nH\ $*

" Tell vim to remember certain things when we exit
"  '10  :  marks will be remembered for up to 10 previously edited files
"  "100 :  will save up to 100 lines for each register
"  :20  :  up to 20 lines of command-line history will be remembered
"  %    :  saves and restores the buffer list
"  n... :  where to save the viminfo files
" set viminfo='10,\"100,:20,%,n~/.viminfo

"Restore cursor position
function! ResCur()
  if line("'\"") <= line("$")
    normal! g`"
    return 1
  endif
endfunction

augroup resCur
  autocmd!
  autocmd BufWinEnter * call ResCur()
augroup END

" Fast saving
noremap <leader>w :w!<cr>

" Fast editing of the .vimrc
" map <leader>e :e! ~/.vimrc<cr>
" noremap <leader>e :e! c:\\Users/username/.vimrc<cr>

" When vimrc is edited, reload it
" autocmd! bufwritepost vimrc source ~/.vim_runtime/vimrc

"These two lines display the file name at the bottom
set modeline                        
set ls=2

"Default for checking marks is 4 seconds, make it faster
set updatetime=100          

"Persistent Undo
" set undodir=~/.vim/undodir
set undodir=c:\\Users\user\vim\undodir
set undofile
set undolevels=10000    "maximum number of changes that can be undone
set undoreload=10000 "maximum number lines to save for undo on a buffer reload

"Keep undo history when switching buffers
set hidden

"Don't use vi-compatibility mode
set nocompatible                    

"Use the smart version of backspace (jumps over tabs apparently instead of
"spaces 
set backspace=2                     

"Use spaces instead of tabs
set expandtab                       

"Line Numbers
set number                                              

"Makes unnamed clipboard accesible to X window
set clipboard=unnamedplus

"Number of spaces to use for each step of (auto)indent.
set shiftwidth=4

"This shows what you are typing as a command
set showcmd

"Not too sure what this does
set smarttab

"Indent every time you press enter
set autoindent
set smartindent                                          

"Use C style indent instead (note this causes problems with non C code)
" set cindent

"Cursor Always in middle
"NOTE This causes problems with word wrap of long lines as they are not
"displayed correctly
set scrolloff=999                       

"Always display row/column info 
set ruler                           

"make word wrap wrap words, not character
set formatoptions=l
set lbr

"Use ... when word wrapping
set showbreak=...

"enable status line always
set laststatus=2

"
" statusline
" cf the default statusline: %<%f\ %h%m%r%=%-14.(%l,%c%V%)\ %P
" format markers:
"   %< truncation point
"   %n buffer number
"   %f relative path to file
"   %m modified flag [+] (modified), [-] (unmodifiable) or nothing
"   %r readonly flag [RO]
"   %y filetype [ruby]
"   %= split point for left and right justification
"   %-35. width specification
"   %l current line number
"   %L number of lines in buffer
"   %c current column number
"   %V current virtual column number (-n), if different from %c
"   %P percentage through buffer
"   %) end of width specification
set statusline=%f%m%r%h%w[%n]\ [F=%{&ff}][T=%Y]\ %=[LINE=%l][%p%%]

"set it up to change the status line based on mode
if version >= 700
  au InsertEnter * hi StatusLine term=reverse ctermbg=4
  au InsertLeave * hi StatusLine term=reverse ctermbg=2
endif

"start searching as you type
set incsearch

"Map jj to escape
inoremap jj <Esc>                       

"Highlight search strings
set hlsearch 

" Set off the other paren
highlight MatchParen ctermbg=4

"Ignore case when searching
set ignorecase 

"But remember case when capitals used
set smartcase

" Use english for spellchecking, but don't spellcheck by default
if version >= 700
   set spl=en spell
   set nospell
endif

"Show matching brackets when text indicator is over them
set showmatch 

"How many tenths of a second to blink
"Does not seem to change anything
set mat=2 

"Highlight current line
set cul

"adjust highlight color
hi CursorLine term=none cterm=none ctermbg=232

"enable 256 color
set t_Co=256

"Do not want spell checking in my commented blocks
let g:tex_comment_nospell= 1

if &t_Co == 256
    " colorscheme xoria256
    colorscheme desert
else
    colorscheme peachpuff
endif

" Restore cursor position to where it was before
augroup JumpCursorOnEdit
   au!
   autocmd BufReadPost *
            \ if expand("<afile>:p:h") !=? $TEMP |
            \   if line("'\"") > 1 && line("'\"") <= line("$") |
            \     let JumpCursorOnEdit_foo = line("'\"") |
            \     let b:doopenfold = 1 |
            \     if (foldlevel(JumpCursorOnEdit_foo) > foldlevel(JumpCursorOnEdit_foo - 1)) |
            \        let JumpCursorOnEdit_foo = JumpCursorOnEdit_foo - 1 |
            \        let b:doopenfold = 2 |
            \     endif |
            \     exe JumpCursorOnEdit_foo |
            \   endif |
            \ endif
   " Need to postpone using "zv" until after reading the modelines.
   autocmd BufWinEnter *
            \ if exists("b:doopenfold") |
            \   exe "normal zv" |
            \   if(b:doopenfold > 1) |
            \       exe  "+".1 |
            \   endif |
            \   unlet b:doopenfold |
            \ endif
augroup END

function! Time()
  return strftime("%c", getftime(bufname("%")))
endfunction

" Font size
if has("gui_running")
  if has("gui_gtk2")
    set guifont=Inconsolata\ 12
  elseif has("gui_macvim")
    set guifont=Menlo\ Regular:h14
  elseif has("gui_win32")
    set guifont=Consolas:h14:cANSI
  endif
endif

回答1:


You have trailing whitespace in your command. (Actually a bunch of mappings have it)

Delete it.

The trailing whitespace is added to the command and moves the cursor forward equal to the number of spaces.

An easy way to do this on the whole file is to run

:%s/\s\+$//



回答2:


What's the point of a comment like this one:

"Turn on syntax (I guess)
syntax on

or this one:

"Keep undo history when switching buffers
set hidden

or this one:

"Not too sure what this does
set smarttab

or this one:

"These two lines display the file name at the bottom
set modeline                        
set ls=2

(especially considering you enable the statusline a second time later)

Also…


"enable 256 color
set t_Co=256

if &t_Co == 256
    colorscheme desert
else
    colorscheme peachpuff
endif
  1. Thanks to set t_Co=256 you will never see peachpuff.
  2. set t_Co=256 has nothing to do in your vimrc. Set up your terminal emulator instead.

"Always display row/column info 
set ruler

" statusline
set statusline=%f%m%r%h%w[%n]\ [F=%{&ff}][T=%Y]\ %=[LINE=%l][%p%%]

Your custom statusline overrides 'ruler'. Remove set ruler from your config.


set smartindent

is not "smart" at all. Remove it from your config.


"Don't use vi-compatibility mode
set nocompatible

is both wrong and useless. Remove it from your config.


"Turn on syntax (I guess)
syntax on

" Needed for Syntax Highlighting and stuff
filetype on
filetype plugin on
syntax enable
  • syntax on implies filetype on,
  • filetype plugin on implies filetype on,
  • you already did syntax on so there's no need to do syntax enable.

Use this instead:

filetype plugin indent on
syntax on

noremap <S-k> :bn<CR>

should be:

nnoremap K :bn<CR>

but K — keyword lookup — is rather useful so that's not a very good idea.


noremap <S-J> :bp<CR>

should be:

nnoremap J :bp<CR>

but J — join — is too useful to be remapped to anything.


let g:mapleader = ","

is useless and can be removed safely.



来源:https://stackoverflow.com/questions/27683792/mapping-jj-to-esc-causes-cursor-to-try-to-jump-forward

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!