Skip to main content
added 4 characters in body
Source Link
3N4N
  • 5.9k
  • 1
  • 21
  • 49

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 or<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 you'll<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.

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 or . 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 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.

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.

add updates
Source Link
3N4N
  • 5.9k
  • 1
  • 21
  • 49

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 or . 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 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.

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.

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 or . 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 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.

Source Link
3N4N
  • 5.9k
  • 1
  • 21
  • 49

Change the backspace behavior in neovim

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.