MageWind
MageWind

Reputation: 832

VIM: Inserting New Line then Pressing Escape Moves Cursor to Beginning of Line

When I am editing a python file, after I insert a new line then press <ESC>, the cursor moves all the way to the beginning of the line (ie. column 0). How do I stop this behavior? It's really annoying when I want to paste something on the new line.

Here are my relevant .vimrc settings:

set softtabstop=2
set expandtab
set shiftwidth=2
set smartindent

Upvotes: 1

Views: 1050

Answers (5)

Ben
Ben

Reputation: 8905

If you already pressed Esc or are coming back to the line later, it is very easy to get the indent back for editing or pasting.

Just use cc to edit the line, re-applying the automatic indent and landing you in insert mode. Then you can start typing or use CTRL+R+ for example to paste from the system clipboard from the current cursor position.

Note Vim will remove the indent again if you stop editing the line without entering any text. This is intentional as it prevents wasteful trailing whitespace littering the file.

Upvotes: 1

DavidEG
DavidEG

Reputation: 5947

You can add mapping so when the auto indentation is triggered a single character is inserted and then deleted:

:nnoremap o ox^H
:nnoremap O Ox^H
:inoremap <enter> <enter>x^H

^H must be introduced by pressing ctrl+vctrl+h. So do not copypaste.

Upvotes: 1

Tom
Tom

Reputation: 460

From normal mode, a quick == will indent the line to whatever position it should be at using your indentation settings. At least, that's how it's working for me. My relevant indent section in my vimrc:

if has("autocmd")
  filetype plugin indent on
endif

Upvotes: 1

merlin2011
merlin2011

Reputation: 75545

If your goal is to paste something on the new line after just starting it using o or O, you do not have to enter normal mode for it.

Just type Control-R " to paste from the default register directly from insert mode.

More generally, you can type Control-R <RegisterName> to paste from that register from insert mode.

Upvotes: 4

Matthew Strawbridge
Matthew Strawbridge

Reputation: 20610

If you edit the line (e.g. by pressing SpaceBackspace) before pressing Esc then Vim will leave the indentation intact.

Upvotes: 1

Related Questions