Why Use Esc in Vim

Why use Esc in vim?

I have mapped Escape to otherwise unused Caps Lock. This is a common method to streamline vim's usage. This article has some alternative solutions, too. The same article links to tweaks to make the Escape-Capslock switch system-wide if you prefer.

Why does pressing escape go back one character?

I found the following answer in this question on the Unix & Linux Stack Exchange:

In insert mode, the cursor is between characters, or before the first
or after the last character. In normal mode, the cursor is over a
character (newlines are not characters for this purpose). This is
somewhat unusual: most editors always put the cursor between
characters, and have most commands act on the character after (not,
strictly speaking, under) the cursor. This is perhaps partly due to
the fact that before GUIs, text terminals always showed the cursor
on a character (underline or block, perhaps blinking). This
abstraction fails in insert mode because that requires one more
position (posts vs fences).

Switching between modes has to move the cursor by a half-character, so
to speak. The i command moves left, to put the cursor before the
character it was over. The a command moves right. Going out of
insert mode (by pressing Esc) moves the cursor left if
possible (if it's at the beginning of the line, it's moved right
instead).

I suppose the Esc behavior sort of makes sense. Often,
you're typing at the end of the line, and there Esc can
only go left. So the general behavior is the most common behavior.

Think of the character under the cursor as the last interesting
character, and of the insert command as a. You can repeat
a Esc without moving the cursor, except that
you'll be bumped one position right if you start at the beginning of a
non-empty line.

Credits to the original author.

If you would like to edit this behavior, you could follow the advice from @ib. in this answer:

Although I would not recommend changing the default cursor mechanics,
one way of achieving the behavior in question is to use the following
Insert-mode mapping.

:inoremap `^

Here the Esc key is overloaded in Insert mode to
additionally run the `^ command which moves the cursor to the
position where it had been the last time Insert mode was left. Since
in this mapping it is executed immediately after leaving Insert mode
with Esc, the cursor is left one character to the right as
compared to its position with default behavior.

Unlike some other workarounds, this one does not require Vim to be
compiled with the +ex_extra feature.

Map Esc key in Vim

For a single session, you could just enter the following keystrokes exactly:

esc:imapspace<f4>space<esc>enter

The esc key ensures that you're in command mode, colon starts a line command, and the imap maps the F4 key to ESCAPE.

However, if you want this retained for every session, you'll need to put in in your Vim start-up file.

The location of this varies depending on your environment (for my Linux box, it's at $HOME/.gvimrc for gvim, $HOME/.vimrc for vim). You'll need to find it and add the line:

imap <f4> <esc>

One trick you can use is to start a naked Vim session (vim without an argument) then enter

:e $MYVIMRC

which will open up your current start-up file.

Also,

:echo $HOME

should tell you the location of it under Windows.

Using Esc in vim insert mode results to Esc in text

It looks like you have the < flag in your 'cpoptions'. Check with :set cpo?. To turn it off :set cpo-=<, but pay attention that you're not accidentally running in compatible mode; you don't want that!

Disable the recognition of special key codes in <>
form in mappings, abbreviations, and the "to" part of
menu commands.

With regards to your mapping, there's no need to exit and re-enter insert mode. You can use <Left> (assuming you've fixed the literal insertion issue):

:map ( i() <Left><Left>

Vim VS Code Extension: How to remap i in normal mode to Escape key?

In Vim, the i keybinding is used to go into insert mode from normal mode. The esc key is usually used to go into normal mode. Setting i as esc is definitely an anti-pattern. Usually people set CapsLock as esc key since it's nearer to the home row in keyboard and you don't have to reach as far as esc key, and since it's annoying sometimes.
I have also seen some people using jj over esc.

Nevertheless, to answer your question, just open Preferences: Open Keyboard Shortcut from command palette(Ctrl+Shift+p) and search for vim_escape. Then double click on it and enter the key i and then press enter again. That should get you the desired result.

Again, suggesting you to not go ahead with this remapping.

Using Ctrl + [ instead of ESC in Vim

Vim cannot distinguish ESC and Ctrl-[. If you :h keycode, you will see:

notation    meaning         equivalent  decimal value(s)    ~
-----------------------------------------------------------------------

<Esc> escape CTRL-[ 27 *escape* *<Esc>*

but you can pull your ESC keycap out. :)

VIM: Doesn't VIM 'replace' use of CTRL with ESC?

When vi was originally written, it was written on this keyboard layout:

Sample Image

Note that the Esc is where Tab is on most modern day keyboard layouts and was much less of a stretch. This also explains why hjkl are the proper "arrow" keys in vim and why some of the other common keys in vim may seem like unusual selections.



Related Topics



Leave a reply



Submit