0

First I will describe what I am trying to achieve:

In general I use set iskeyword+=- as I see this more usable (one should always put spaces around operators when needed). This is quite helpful, but has one significant disadvantage - it also causes i_^w to behave not as expected. So I am trying to write function that would reset 'iskeyword' to default value, modify it a little and then fire the same functionality as i_^w, however I cannot find how to handle "deletion" part.

My attempt

func! smart#delete_word() abort
    let l:iskeyword = &iskeyword

    try
        set iskeyword&vim
        set iskeyword-=_

        if col('.') == col('$') - 1
            normal! dvb
        else
            normal! db
        end
    finally
        let &iskeyword = l:iskeyword
    endtry
endfunc

However db/dvb trick isn't always working as expected and sometimes there are some leftovers at the end of the line or some additional characters are deleted.

So question is, how to achieve the same behaviour as i_^w in function (worth mentioning that i_^w itself will be remapped to this function, so it wouldn't be a solution to just feedkeys("\<C-w>").

  • One trick would be to use norm! i^w where you insert ^w with <c-v><c-w> – statox Jun 28 '18 at 11:29
  • @statox unfortunately that doesn’t work as it moves cursor one character backwards and exec "norm! a\<c-w>" doesn’t work when started in the middle of the line. – Hauleth Jun 28 '18 at 11:33
  • 1
    feedkeys("\<C-w>", 'n') doesn't map (similar to normal!) – Mass Jun 28 '18 at 13:52
  • @Mass for whatever reason feedkeys("\<C-w>", 'n') didn't work as expected as it deleted single character at the time. However it can be bug in NeoVim. – Hauleth Jun 28 '18 at 23:21
1

Ok, I have found the solution that seems to work without problems:

if col('.') == col('$') - 1
     exec "norm! a\<C-w>\<right>"
else
     exec "norm! i\<C-w>\<right>"
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.