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?


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.

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

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

  • 3
    I answered his question, but you've actually figured out what the underlying problem is way better than I did. Goodjob.
    – Rich
    Feb 12 '15 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. Dec 6 '19 at 22:34

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.