26

I have set expandtab in my .vimrc file to convert tab to multiple space characters. However some files (like Makefile) need actual the tab character inserted.

Is there an easy way to force the insertion the tab while I am typing?

2 Answers 2

46

Instead of just pressing Tab, first press Ctrl-V and then press Tab.

This can be used to insert a variety of special chars. See :help i_CTRL-V for details.

Ctrl-V also works in command-line mode (:help c-CTRL-V), and even in some other programs entirely. (e.g. bash, mutt.)

If you have Ctrl-V mapped to something else, try Ctrl-Q. This has the same effect in Vim as Ctrl-V, but some terminals use it for control flow, in which case Vim won't ever see it.

1
  • 6
    Or <c-v>009 in case of broken Tab key or hardcore ASCII purist :-) Commented Feb 11, 2015 at 15:38
25

Rather than change your behavior (which is error prone).
I would set up vim to behave differently for this file type.

So in my normal editing I expand tabs. But specifically for make files I set the noexpandtab so that tabs are retained.

Add the following to your ~/.vimrc file:

" Normal action
set expandtab

if has("autocmd")

    " If the filetype is Makefile then we need to use tabs
    " So do not expand tabs into space.
    autocmd FileType make   set noexpandtab

endif
2
  • 4
    I answered his question, but you've actually figured out what the underlying problem is way better than I did. Goodjob.
    – Rich
    Commented Feb 12, 2015 at 9:50
  • 2
    I think both answers are valuable. Because sometimes you're logged in somewhere remote and don't want to mess with the vim config but just quickly want to edit a Makefile. Commented Dec 6, 2019 at 22:34

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.