I am trying to make a function that creates a new line and hits the tab key (aka 2 spaces / 4 spaces; what ever is set). I can do it easily with a keybinding:

:inoremap <leader>ind <esc>o<tab>

But when I try to execute this from a function:

function! Indent_New_Line()
  execute "normal! o<tab>"

:inoremap <leader>ind <esc>:call Indent_New_Line()<cr>

the <tab> gets printed literally instead of a tab char:

|def foo

Any ideas as to why this is happening? I couldn't find very much about this..

Any help is appreciated, Thanks!

1 Answer 1


All you need to do is to insert a \ before the <tab>

Now the code will look like this instead:

execute "normal! o\<tab>"
  • Awesome thank you. What is weird is that I tried this already and the first time it didn't work, but now it does.. I'm not sure what I changed since then but at least it's working now. Thank you! Jan 29, 2017 at 17:46
  • @MitchellWoodbine This won't work if you are not inside a string. For example :normal o\<tab> will not work since it is not inside a string. \<tab> works inside a string because the ` escapes the <tab>` (or at least according to my understanding)
    – user41805
    Jan 29, 2017 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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