The issue you're having is due to the 'paste'
option having an effect on a series of other options and not simply disabling auto-indent. (See :help 'paste'
for a list of all options that are affected by it.)
When you try to undo it by simply running :set ai
, you're still leaving behind most of the other side-effects, in particular 'expandtab'
having been reset, which probably explains why you're seeing this specific side-effect of getting automatic indentation with tabs instead of spaces.
In order to prevent this issue, use :set nopaste
instead of :set ai
to disable paste mode.
You might want to consider adopting the vim-unimpaired plug-in, which includes a few commands (]op
, [op
and yop
) to start insert in 'paste'
mode and automatically leave 'paste'
mode once you leave insert mode, so you don't need to use an explicit command to leave 'paste'
mode.
I also add an Insert mode binding for Ctrl+E to start 'paste'
mode from Insert mode:
inoremap <C-e> <C-g>u<C-o>:execute "normal \<Plug>unimpairedPaste"<cr>
(Note that the above mapping depends on the vim-unimpaired plug-in being available. The <C-g>u
at the beginning breaks the undo sequence, so that if you use u
to undo after a paste it will only undo the paste itself and not what you inserted while in Insert mode before pasting.)
If you prefer not to load a Vim plug-in, you can accomplish something similar to the part of disabling 'paste'
at the end of an Insert with an autocmd
for the InsertLeave
event:
augroup Paste
autocmd!
autocmd InsertLeave * set nopaste
augroup END
The above will execute a :set nopaste
every time you leave Insert mode.
You can also use the 'pastetoggle'
option to set a hotkey to enter or leave 'paste'
mode from Insert mode. For example:
set pastetoggle=<C-e>
But personally I prefer the inoremap
mentioned above, because typically I prefer to leave Insert mode and have the autocmd
disable paste mode rather than switch it on and off while staying in Insert mode. Additionally, I like the feature of the mapping that breaks the undo sequence. So for those reasons I typically end up avoiding 'pastetoggle'
, but it might work well for you.
:set ai
instead of:set nopaste
to undo the effects of'paste'
mode? I think that might be related to your issue. Do you still reproduce if you use:set nopaste
in that step instead?:help 'paste'
you'll see that it resets quite a few options, including'expandtab'
, so maybe that's what's happening here?:set nopaste
works in the expected manner. I've always used:set ai
and it worked the way I wanted before. Very strange. Not sure what was different. Thanks. I'll just have to remember:set nopaste
in future :)