I have the following function that inserts tabs if cursor is at the start of a line and required amount of spaces otherwise.
function! SpecialTab() abort
if (col('.') == 1) || (matchstr(getline('.'), '\%'.(col('.') - 1).'c.') =~ '\t')
return "\<Tab>"
else
return repeat("\<Space>", (&tabstop - (virtcol('.') % &tabstop) + 1))
endif
endfunction
inoremap <expr> <Tab> SpecialTab()
If you use :set backspace=indent,eol,start
with :set expandtab
, one backspace can delete a tabs worth of spaces if you originally inserted a tabs worth of spaces by tapping the key in the first place. But if you manually typed required amount of spaces, one backspace just deletes one space.
I want to get that with my function. I don't have enough experience to understand C source code and I don't understand how backspace characteristics is maintained. Any help would be appreciated.
Edit
I have just checked that vim actually doesn't check whether the spaces were inserted with pressing <Tab>
or <Space>
. It just behaves that way until you escape from the insert mode. After you have escaped from insert mode and returned back to insert mode, if you try to use <BS>
you'll see that it is deleting &tabstop
amount (i.e. assuming the spaces are integer multiple of &tabstop
) of spaces no matter how you inserted it.
\<c-v>\<tab>
instead of\<tab>
. But i'm afread that will create a new series of problem.et
in the function? How would that help? Tabs are not the problem here, I can delete one tab (visualizing:h ts
worth of spaces) with one backspace key in insert mode. My problem is with deleting tabstop worth of spaces when pressing backspace in insert mode.