3

I started studying VIM around 3 days ago. And now I'm stuck creating macros.

On the beginning of learning VIM I created mapping: jk -> ESC for convenience (inoremap jk <ESC>). Now my macros works correctly only if I pressed ESC; with jk they don't work properly.

For example I create macros to add : to the beginning and end of line:

'I' + ':' + 'ESC' + 'A' + ':' + 'ESC'

@a macros: I exited insert mode with jk.
@b macros: I exited insert mode with <ESC>.

If apply @a to the line example, I don't get the colon at the end... I end up with:

:example

If apply @b to the line example, I do get the colon at the end... I end up with:

:example:

Output of command - :registers (macros aren't the same): enter image description here

~/.vimrc :

1 syntax on " highlight syntax 
2 set number " show line numbers
3 set hlsearch " highlight all results
4 set noswapfile " disable the swapfile
5 set ignorecase " ignore case in search
6 set incsearch " show search results as you type
7 " set spell spelllang=en_us " misspelled words are automatically underlined
8 
9 inoremap jk <ESC> " type 'jk' for leaving insert mode

Q: How to make behavior of 'jk' and 'ESC' equal when recording macros.

P.S. Sorry if the explanation is not smooth, this is my first question and I tried to make it as simple as possible.

woojiq
  • 71
  • 1
  • 9
  • 1
    Using `$ vim -Nu NONE` (without a config), both recordings result in `:example:`, here. How do you play back `@a` and `@b`, exactly? – romainl Jan 29 '22 at 12:28
  • @romainl I go to the line what I want to change and type `@a` or `@b` in normal mode. – woojiq Jan 29 '22 at 12:48
  • Same method here. Can you try without your config? – romainl Jan 29 '22 at 13:04
  • But without config `jk` won't work. Btw, I don't use any plugins, only some commands in .vimrc (`syntax on; number; hlsearch; noswapfile; ignorecase; incsearch; inoremap jk `) – woojiq Jan 29 '22 at 13:27
  • 1. You can test your mapping on the command-line. 2. That `syntax on; number; hlsearch; noswapfile; ignorecase; incsearch; inoremap jk ` doesn't look like proper syntax. Can we add your `vimrc` to your question? – romainl Jan 29 '22 at 14:16
  • @romainl Thanks. The problem was in my `.vimrc`. My comment to inoremap was in the same line. When I moved the comment up, everything worked. Also the comment began to be highlighted I'll add the link to post that helped me. – woojiq Jan 29 '22 at 14:34

1 Answers1

3

You need to move the comment up.

Instead of:

inoremap jk <ESC> " type 'jk' for leaving insert mode

Do:

" type 'jk' for leaving insert mode
inoremap jk <ESC>

Vim interprets this comment as part of your mapping.

See "Why does remapping make the cursor jump?" for additional information.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
woojiq
  • 71
  • 1
  • 9