2
votes

I've just started learning Vim. I use Vim in gnome-terminal (on Ubuntu 14.04). I want to map kj keys to Esc in my vimrc file. I added the following two lines in my vimrc file (~/.vimrc):

inoremap kj <Esc>
cnoremap kj <Esc>

Question 1: I sourced my vimrc file (even restarted the terminal), but when I quickly type kj in insert mode, it just types those two letters kj and does not go to normal mode. Am I doing something wrong? What is the correct way to map Esc key to something else?

Question 2: How should I edit my vimrc to map caps key to Esc?

In Vim,

:echo $COLORTERM

displays 'gnome-terminal'

For reference, here is my complete vimrc file (without comments):

set t_Co=256
colorscheme wombat256mod
syntax on
set hlsearch
set incsearch
set number
set autoindent
set ts=4
set ignorecase
set title
set scrolloff=5

" Map <ESC> key to kj
inoremap kj <Esc>
cnoremap kj <Esc>

set paste
set cursorline
2

2 Answers

3
votes

Insert mode keymaps will not work while paste mode is active, and you have it active by default:

set paste

You will probably be better off removing that line so paste mode is not active, and adding an easy way to turn it on when you want it. I use this:

set pastetoggle=,p

Paste mode defaults to off, and I can toggle it easily using ,p

From :help 'paste:

When the 'paste' option is switched on (also when it was already on):
        - mapping in Insert mode and Command-line mode is disabled
        - abbreviations are disabled
        - 'textwidth' is set to 0
        - 'wrapmargin' is set to 0
        - 'autoindent' is reset
        - 'smartindent' is reset
        - 'softtabstop' is set to 0
        - 'revins' is reset
        - 'ruler' is reset
        - 'showmatch' is reset
        - 'formatoptions' is used like it is empty
These options keep their value, but their effect is disabled:
        - 'lisp'
        - 'indentexpr'
        - 'cindent'
1
votes

On 2)

Not exactly answering the question, as this maps CapsLock to escape for all applications, not just Vim.

xmodmap - <<EOD
! Set caps lock as escape, losing Caps Lock
remove Lock = Caps_Lock
keysym Caps_Lock = Escape
EOD

I put that script in PATH somewhere as capswap with a chmod +x. Until you log out, no more CapsLock and an Esc is where it should be for vi, near the left pinky finger.

To allow Esc to locks caps, (which I have found, even as a COBOL programmer is rarely, if ever, necessary)

xmodmap - <<EOD
! Set caps lock as escape, and escape as CapsLock
remove Lock = Caps_Lock
keysym Caps_Lock = Escape
keysym Escape = Caps_Lock
add Lock = Caps_Lock
EOD

By the by, I got used to calling it capswap, but I actually removed the "swapping" part a long time ago, as in the first listing, and just run without CapsLocking. Your preferences may lead to different choices. The first script gives the equivalent of two Esc keys.