Vim on Mac (iTerm) enters insert mode with every keystroke - vim

Any keystroke (except for Esc) puts vim into insert mode and inserts the character. Example: I sit in Normal mode, press k to go up, vim goes into insert mode and types k under the cursor. Same for : and other keys. Esc or Ctrl-[ returns to Normal mode, but the behavior persists.
This doesn't happen on entering vim, but on occasion (quite often) it gets into this mode and stays until I kill the terminal window. It's not global (so if I have another vim instance in another terminal, the state does not propagate) so it's probably not about the input modes / keyboard layouts.
Running a Mac M1 with macOS 12.
What could it be?
Update:
It happens after I hit Delete key in Insert mode (consistently).
I have a binding that exits Insert mode on a key sequence (inoremap saj <esc>l), and typing that exits Insert mode for good after it gets stuck. However, exiting through Esc or Ctrl-[ doesn't work as mentioned above.

Related

How to copy and paste in Vim's terminal mode?

I often want to copy text from a :terminal window to a normal text buffer. At the moment I exit the shell session and copy from the history.
There must be a better way to this.
Copy
To copy from a terminal window press CTRL-W N (This is a capital N)1 or CTRL-\ CTRL-N (this is not a capital N) to get into normal mode. From there you can use all usual vim commands to copy and paste stuff.
Entering insert mode will drop you back to your shell.
Paste
To paste from a register into the terminal window you have to be in Terminal-Job ("insert") mode.
Press CTRL-W " followed by the register.
:help Terminal-mode tells us:
When the job is running the contents of the terminal is under control of the
job. That includes the cursor position. Typed keys are sent to the job.
The terminal contents can change at any time. This is called Terminal-Job
mode.
Use CTRL-W N (or 'termkey' N) to switch to Terminal-Normal mode. Now the
contents of the terminal window is under control of Vim, the job output is
suspended. CTRL-\ CTRL-N does the same.
[...]
In Terminal-Normal mode you can move the cursor around with the usual Vim
commands, Visually mark text, yank text, etc. But you cannot change the
contents of the buffer. The commands that would start insert mode, such as
'i' and 'a', return to Terminal-Job mode.
See :h terminal-typing for more useful commands in terminal windows.
1Unfortunately the vim help doesn't tell you that it is a capital N, I kept the original notation
Can use Shift+Insert as a shortcut to paste from the clipboard into a running terminal session. Setup the mapping like
:tmap <S-Insert> <C-W>"+
The will result in pasting from the + register. Alternatively use the * register which sometimes works better in MS Windows.
Set the clipboard setting in your .vimrc to using system clipboard as the Vim's clipboard. Depends on your OS it may differ, assume you are using Mac OS: set clipboard=unnamedplus
Then you can use y command to copy then paste on another app just by Cmd + V, remember to exit the insert mode of the terminal by clicking any where or press Ctrl+\ Ctrl+n

Have to press "I" and spacebar to switch to insert mode in Vim

I have to press "i" and an extra spacebar in order to switch to Insert mode in Vim, which is pretty annoying.
You can see there are 5 "i"s on the screen in Normal mode, if I press spacebar, Vim will switch to Normal mode and 4 "i"s will be added to the file.
It has the same problem for "a", "s", "o" and other command key.
Please help.
Based on the underlining, this looks like a temporary input buffer of an IME (Input Method Editor; to insert non-English characters with a standard keyboard). The application (Vim) doesn't see the characters until you complete the composition (with the Space key). Only then does Vim receive the characters, and processes them.
You need to turn off the IME, at least for the terminal / Vim. There's usually an icon in the systray where you can select the input method, and often a keyboard shortcut like Super + Space toggles the state of it. Vim can interact with the IME, too, and only use its services for insert mode (to be able to write in Unicode scripts). Read more about it in :help mbyte-XIM (Linux) and :help mbyte-IME (Windows).

Switch automatically between normal and insert mode in vim

I am new to vim and have installed oh-my-vim and learnt these keybindings by googling:
Jump forward by word - w
Jump forward by word - b
Jump to end of sentence - A
Close current file without exiting - bd
Undo is u
Execute shell command with ! (bang)
of these typing Shift + a in Normal mode allows me to jump to end of sentence and also goes into insert mode soon as I start typing after it. I tried out Shift + w and Shift + b and
it also shows similar behaviour of going to insert mode as soon as I start typing after I use that keybinding.
What are the equivalents for these keybindings where I am not using a letter?
Jump to start of sentence - 0
Redo is Ctrl + R
Jump to previous line - ``
Hope I am clear in describing. Thanks!
I'm also a beginner and I am getting used to Vim recently.
Well, regarding your question, to switch from Normal to Insert, you could press:
i to insert (the cursor stays, effectively inserting to the left)
a to append (the cursor jumps after the character you were on)
I to insert from the beginning of the line. This is equivalent to pressing 0 in Normal mode, and then pressing i to enter Insert mode.
A to append to the end of the line. This is equivalent to pressing $ in Normal mode to get to the end of the line, and then pressing a to append in Insert Mode.
For Insert to Normal:
press <Esc> to get to Normal mode.
press <Ctrl-[> to get to Normal mode.
press <Ctrl-O> to temporarily enter Normal mode for one command. Upon execution it will automatically return to Insert mode.
Personally, to shift between Normal mode, I use <Ctrl-[> for consecutive commands in Normal mode and <Ctrl-O> if it's a one time deal. <Esc> is too far for my pinky :)
It's not entirely clear what you are asking. Shift+W and Shift+B do not automatically enter insert mode, instead they go back and forth on WORDS (vs words).
If you want to enter insert mode, just hit i. You could hit wi to insert after a word.
You mention 0, which goes to the beginning of a line. You can hit I (shift+I) to insert at the beginning of a line. Note that this inserts after the initial whitespace on the line, it is the same as hitting ^i. If you want to insert at the very beginning of the line, you'll need to use 0i.

vim pressing Esc key causing previous text to be pasted in insert mode

I am trying to figure out what is happening when I press Esc. What I notice is that, if I am in Insert mode Esc will paste the previously entered text. I am using vim 7.2 on a rhel 6 linux server connected via iTerm2
This happens when I press Esc two or more times, pressing Esc once will correctly exit insert mode.

How to make Vim return from insert mode to normal mode after a command is finished?

When Vim is in Insert mode and you press Ctrl-O it switches to normal mode for one command, then switches back to insert mode when the command is finished. Is there the opposite command? To switch to insert mode for typing a word (or a number of letters or a line), then switch it back to normal mode when the typing is finished?
Not that I know of, however, if you are repeating the same operation over and over again you can fake it by using the . command in normal mode after you've made the change once.
An alternative (in the same scenario) would be to record a macro and run it from normal mode.

Resources