What is the reverse key of 'C-i' in vim insert mode?

Viewed 134

In vim's insert mode, 'C-t' and 'C-d' can insert or delete Tab/4whitespace from the beginning of current line and:

'C-i' can insert Tab/4whitespace from current cursor.

BUT, how can we delete 4 whitespace from current cursor? What is the reverse operation of 'C-i' ?

3 Answers

There is no built-in command that does what you want.

One way to do something like it "manually" is:

<C-c>ciw
  • <C-c> to leave insert mode without triggering autocommands, leaving the cursor on the last <Space>,
  • ciw to cut the sequence of <Spaces>, which Vim treats as a "word", and enter insert mode.

Now that we have a working macro, it is trivial to map to something convenient:

inoremap <key> <C-c>ciw

And we can make it even better by preventing it from polluting registers with useless s:

inoremap <key> <C-c>"_ciw

Note that this mapping works for any sequence of <Tab>s and/or <Space>s as well.


Now, as noted by @Eineki, the mapping above is not a true inverse <C-i> because it deletes all the contiguous whitespace before the cursor, regardless of tabulations.

If you only pressed <C-i> once, then that mappings will effectively act as an inverse <C-i>, but if you have more than a &shiftwidth of <Space>s, the whole sequence will be deleted, which is not what a true inverse <C-i> would do.

A true inverse <C-i> would delete <Space>s up to the previous tabulation, which sounds a tad less trivial.

Here's my suggestion for the a tad less trivial true inverse (as defined by romainl) of TAB:

function! BAT()
        while getline(".")[col(".")-2] =~ "\\s"
                normal X
                if virtcol(".")%&tabstop == 1
                        break
                endif
        endwhile
endfunction
imap <S-Tab> <C-O>:call BAT()<CR>

You can use <C-o>4dh (or <C-o>4d<Left> if you prefer arrows) to delete 4 characters to the left of the current insert position.

Note: <C-o> allows you to temporarily use normal-mode commands from insert-mode.

Related