0

I find I forget to leave INSERT or REPLACE to navigate around the file with cursor keys, Page Up, Page Down, etc. Or, I walk off from the computer to make my coffee, or whatever.

Then when my brain comes back to editing the file, it's forgotten which mode Vim was left in. I want to reduce this cognitive load by just having Vim go to normal mode as soon as I touch the cursor keys or Page Up or Page Down.

Can Vim work this way?

3
  • it's forgotten which mode Vim was left in It is helpful to have some visual indicator to indicate which mode Vim is in. Do you have, for example, --INSERT-- at the bottom left corner; Or does the cursor change it's appearance when you're in insert mode?
    – husB
    Commented Mar 10, 2022 at 4:11
  • I agree that there should be an indicator for different modes. You can check :help guicursor.
    – leaf
    Commented Mar 11, 2022 at 9:33
  • Yes. There is an indicator. Usually either --INSERT-- or something similar. But I find my brain is not thinking about the indicator. It's thinking about a bug, or feature request, or ... Maybe a different color or something would work better for me. Commented Mar 11, 2022 at 9:44

2 Answers 2

3

You can use the following mappings:

inoremap <left>     <esc><left>
inoremap <right>    <esc><right>
inoremap <up>       <esc><up>
inoremap <down>     <esc><down>
inoremap <PageUp>   <esc><<PageUp>
inoremap <PageDown> <esc><PageDown>

Or just for fun:

let escapeInsertModeKeys = [ '<left>', '<right>', '<up>', '<down>', '<PageUp>', '<PageDown>']

for key in escapeInsertModeKeys
    execute 'inoremap ' . key . ' <Esc>' . key
endfor

However I think what you are describing is a sign that you didn't grok Vim. To make the best out of Vim you are supposed to spend most of your time in normal mode. You should only be in insert mode when you actually need to type some characters in your buffer. After each edit you should go back to normal mode: That will allow you to have a better undo history (so that u and ctrl-r and the dot command work really efficiently) as well as a better navigation thanks to all the available motions in normal mode.

So my advice would be not to use these mappings but instead force yourself to leave insert mode as often as possible.

This is also why some people remap <CapsLock> to <Esc> (not in Vim, at the OS level) or use mappings like the following: To make it easier to switch from insert mode to normal mode

" Switch to normal mode when you type jk in a buffer
inoremap <silent> jk <Esc>
0

I get the impression that you are entering and staying in INSERT mode, and then moving around with the arrow keys to do your edits, much like Notepad. This works, but overlooks much of the power of Vim.

I have used Vim's key mapping ability to break bad habits, which this scenario could be considered to be. Doing this will help you become a more efficient Vimmer: disable your navigation keys in INSERT mode. This will force you to go into NORMAL mode to move around the file, and your edits will be limited to very small chunks that can be undone or repeated easily. Here is the code that does this:

" Break bad habits.
inoremap <left>     <Nop>
inoremap <right>    <Nop>
inoremap <up>       <Nop>
inoremap <down>     <Nop>
inoremap <PageUp>   <Nop>
inoremap <PageDown> <Nop>

After you become accustomed to this way of working, you can remove the mappings from your .vimrc, because it will be in your muscle memory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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