14

I've noticed while editing along a line in a .txt file in insert mode the cursor will move to the start of the line following a write using key sequence:

<esc>:w

I'd like to change this behavior such that the cursor will stay in position following a :write. Is this possible? I don't need the cursor to remain in insert mode, I'd just like it to maintain its last position following a write.

I am using gVim 7.4 on windows. My .vimrc is very basic, I don't believe any of my settings interfere with this behavior. I have also removed sourcing of mswin.vim and example.vim from my .vimrc (as bundled with the official vim.org windows installer).

After reading the comments below I looked at the issue again and realized the cursor only slides to the far left after a write on lines which are entirely made up of trailing white space. In other words, the cursor only slides to the far left upon esc-:w when the line is a hanging indent with no other characters besides spaces. The .vimrc is handling indent behaviors with these settings:

set tabstop=4
set softtabstop=4
set shiftwidth=4
set expandtab
set autoindent 

So, a new line created below an indented line will contain 4 trailing white spaces as the first 4 spaces of the line (which I want to keep). Upon the 'esc' key press the cursor slides to the far left of the buffer.

Is there a way to retain cursor position upon hitting 'esc' to return to normal mode, on a line made up of trailing white spaces (as indentation)?

6
  • 3
    I've never noticed this behaviour in Vim on any platform ... What happens if you start gVim like: gvim -u NONE -U NONE (you can either use cmd.exe, or make a shortcut)? This will prevent loading your (g)vimrc files. Commented Feb 17, 2015 at 17:39
  • 1
    Is that your complete vimrc file? My first suspicion would be that an autocmd in your vimrc file is removing trailing whitespace. Does the behaviour change when you start gVim without any vimrc file (see above comment)? Commented Feb 17, 2015 at 18:17
  • The above is not my complete .vimrc, there are other settings too. Running gVim without a .vimrc (:scriptnames shows no .vimrc or .gvimrc) solves my problem. You're absolutely right, I need to look through my .vimrc or something else that is sourced that might be causing this.
    – Jim
    Commented Feb 17, 2015 at 18:28
  • Apparently this elimination of whitespace-on-new-lines is a desirable behavior, I'm reading here it's meant to keep the file clean of whitespace.
    – Jim
    Commented Feb 17, 2015 at 18:42
  • That's about opening new lines ... Not about writing to a file? Commented Feb 17, 2015 at 19:05

1 Answer 1

12

Looking at the documentation for autoindent has an answer as to why and how to work around it. :help 'autoindent':

Copy indent from current line when starting a new line (typing <CR> in Insert mode or when using the "o" or "O" command). If you do not type anything on the new line except <BS> or CTRL-D and then type <Esc>, CTRL-O or <CR>, the indent is deleted again. Moving the cursor to another line has the same effect, unless the 'I' flag is included in 'cpoptions'.

In other words, if you want it to not lose the indent, type something and then backspace it before hitting Esc, and the leading space will remain.

Alternatively, if you just want to be back at the indent-level when you go back into insert mode and are using 'cindent' as well, use Shift-S instead of i, which will clear the (already empty) line, and start at the appropriate indentation level. This is not as general a solution as the one above, but I prefer this when I'm writing C code, so that my files don't actually get saved with white-space only lines.

3
  • This is a definitive answer to my question from the original documentation. Thanks for the summary and narrative - I would not have thought to look into :h autoindent for this information. Thanks also @Carpetsmoker for discussion leading to a solution.
    – Jim
    Commented Feb 17, 2015 at 21:07
  • 1
    This solved a problem I was having involving remapping o and O to not leave insert mode, but to keep indentation. This can be done by doing nnoremap o o <BS><Esc><DEL>, and nnoremap O O <BS><Esc><DEL>
    – Max Coplan
    Commented Jan 20, 2020 at 17:50
  • Thanks so much!
    – Max Coplan
    Commented Jan 20, 2020 at 17:50

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.