0

That's my .vimrc

set noerrorbells
set tabstop=4 softtabstop=0 noexpandtab shiftwidth=4
set smartindent
set number relativenumber
set nowrap
set noswapfile
set hlsearch
set smartcase
set incsearch
set undodir=~/.vim/undodir
set undofile
set guioptions+=a
set backspace=indent,eol,start
set clipboard=unnamedplus

noremap jk <Esc>
noremap <C-A> <Esc>ggVG
nnoremap confe :e $MYVIMRC<CR>
nnoremap confr :source $MYVIMRC<CR>

imap <C-BS> <C-W>

And when I call :map that's what Vim returns

n  confr       * :source $MYVIMRC<CR>
n  confe       * :e $MYVIMRC<CR>
v  gx            <Plug>NetrwBrowseXVis
n  gx            <Plug>NetrwBrowseX
   jk          * <Esc>
v  <Plug>NetrwBrowseXVis * :<C-U>call netrw#BrowseXVis()<CR>
n  <Plug>NetrwBrowseX * :call netrw#BrowseX(netrw#GX(),netrw#CheckIfRemote(netrw
#GX()))<CR>
   <C-A>       * <Esc>ggVG
v  <C-X>         "*d
v  <C-Del>       "*d
v  <S-Del>       "*d
v  <C-Insert>    "*y
v  <S-Insert>    "-d"*P
n  <S-Insert>    "*P    

When I press jk in insert mode and visual mode, nothing happens. When I press Ctrl-A in normal mode it works, but it doesn't work in insert mode. Can someone please point out my mistakes?

1
2

You should have a look at :h map-modes and keep in mind that you always want your map command to be mode specific even if occasionally that makes you write a very similar command twice it avoids a lot of issues.

From the doc the :map (or :noremap) command only works in Normal, Visual, Select and Operator-pending modes. That's why jk doesn't work in insert mode and <C-a> only works in normal mode. Also when you create visual mode mappings you almost always want to use xnoremap and not vnoremap because unless you know what you are doing you probably don't want your mapping to work in select mode.

What you want is probably the following:

" Only remap jk to <Esc> in insert mode, it doesn't really make sense in other modes
inoremap jk <Esc>
" You can also do it in visual mode, but avoid select mode
xnoremap jk <Esc>
" In normal mode no need to use escape before a motion like gg
nnoremap <C-a> ggVG
" I would advise against this mapping but if you really want it
inoremap <C-a> <Esc>ggVG

These should work, however if I may give my opinion as someone who's been playing with Vim for a few years now:

  • Remapping <C-a> in normal mode is probably a bad idea because :h CTRL-A is useful to increment numbers (Like <C-x> to decrement)

  • Having an insert mode mapping which goes to select mode seems like a smell: I think it shows that you spend most of your time in insert mode rather than using insert mode only to do some edits before going back to normal mode to do all of your other manipulations (move, select, delete, yank, ...)

  • Your confe and confr mappings will quickly become extremely painful because your will use the c command pretty often to change text, I would recommend either turning them into ex commands or into <leader> mapping:

    nnoremap <silent> <leader>e :e $MYVIMRC<CR>
    " or
    command! Confr :e $MYVIMRC
    

Check the help

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.