I got that one some time ago from https://github.com/garybernhardt/dotfiles/blob/master/.vimrc... which offers several other useful functions and settings.
function! InsertTabWrapper()
let col = col('.') - 1
if !col || getline('.')[col - 1] !~ '\k'
return "\<tab>"
else
return "\<c-p>"
endif
endfunction
inoremap <expr> <tab> InsertTabWrapper()
inoremap <s-tab> <c-n>