1

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>"
endfunction

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

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

|def foo
|<tab>
|end

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

Any help is appreciated, Thanks!

2

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! – Mitchell Woodbine Jan 29 '17 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) – Cows quack Jan 29 '17 at 17:48

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.