VIM: Tab as omnicomplete, but not at beginning of line

6

1

I have configured Tab as Omnicompetion in VIM 7.3:

inoremap <Tab> <C-n>

This is convenient, but I would really like to have regular tab back at the beginning of a line! Is there any way to check if the preceding character is whitespace or a line beginning, and if so to insert a Tab character rather than omnicomplete?

Thanks!

dotancohen

Posted 2012-05-12T14:17:33.440

Reputation: 9 798

Answers

12

I have been using this for some time now:

function! InsertTabWrapper()
    let col = col('.') - 1
    if !col || getline('.')[col - 1] !~ '\k'
        return "\<tab>"
    else
        return "\<c-p>"
    endif
endfunction

inoremap <tab> <c-r>=InsertTabWrapper()<cr>

This lets you use the tab key normally when the cursor is at the beginning of a line or not on a word, otherwise it executes the control-p completion key—you may change it to <c-n> for your use, although I find <c-p> more useful.

[Although I have modified it, the original idea for this probably came from the Vim Users' mailing list, but I did not keep any notes about where I got it.]

Heptite

Posted 2012-05-12T14:17:33.440

Reputation: 16 267

Thank you, this is exactly what I was looking for! Apparently I am not the first! – dotancohen – 2012-05-12T19:22:20.443

What is the \k? – dotancohen – 2012-05-12T19:24:27.817

1It matches a 'keyword' character. See ":help /\k". – Heptite – 2012-05-12T23:51:19.040

Thank you, I have never seen the Character Classes help section. – dotancohen – 2012-05-13T07:30:54.273

I don't know why it doesn't work for me!! Can you help? – Wazery – 2014-05-20T07:48:01.920

BTW, it's an idea of Gary Bernhardt. – Wazery – 2014-05-20T07:49:38.543

@Wazery: I need to know more than "it doesn't work" to help you. – Heptite – 2014-05-20T19:31:15.790

Tell me what do you want to know, I am using Macvim and I inserted this snippet into my vimrc, restarted Macvim and tried to write a keyword then hitting tab, but it didn't autocomplete it. If you need a specific detail tell me. Here is my complete vimrc file http://codeshare.io/zUqRP.

– Wazery – 2014-05-21T08:16:59.117

Does the keyword already exist within the file or files "included"? Do you get an error? Do you get a beep? Do you get a tab character? Basically, never just say "it doesn't work." Tell people what you expect, and what you are getting, and what you have tried to resolve it. Include details of your "input" or dataset or whatever is applicable, etc. – Heptite – 2014-05-21T08:54:59.243

2

I can think of somewhat convoluted functions that go back one step, compare characters and act accordingly, but I'd rather address the old "what are you really trying to do?" question:

I suggest you use Ctrl+T to indent a line in insert mode instead, if that is all you want the tab button for. I personally find it semantically very confusing to have a position dependent key like that.

Daniel Andersson

Posted 2012-05-12T14:17:33.440

Reputation: 20 465

Thank you, I did not know about Ctrl-T. That does what I need. Thanks! – dotancohen – 2012-05-12T14:34:31.983

Daniel, thank you. I do believe that you had taught me the proper way to use VIM, but Heptite did post the funtion that does what I was looking for. – dotancohen – 2012-05-12T19:23:37.443