I was introduced to Vim by a friend maybe about six or seven years ago. I opened it, felt confused and after a few impatient switches between insert and normal mode, decided that this is bizarre and somehow managed to get the hell out of it. “Is it considered cool because it’s hard to understand what’s going on?” I remember thinking.

Fast forward couple of years from that point and I was again told by the same friend about Vim. This time about its awesome plugins. YouCompleteMe, Nerdtree, Vundle (along with other plugins from tpope) among many others were brought to my attention. Listening to him, it felt like anything you wanted to do on a text editor to make yourself faster, there was a Vim plugin for that. But it still wasn’t clear to me why I should add an additional cognitive load of learning weird combinations (gg, G, w, 3d) to move around when I am already struggling with coming up with ideas for writing non fiction or just programming. But I decided to give vimtutor a try. It’s a small set of instructions inside a text file that teaches you some basic functionalities of Vim by asking you to perform those functions right inside the tutorial. You learn by editing and navigating the tutorial text file itself. I was beginning to enjoy it a little bit. I followed this by learning to touch type as that seemed essential to use vim efficiently.

Even though I was becoming comfortable with Vim’s very basic movement and editing commands, I still regularly craved mouse for scrolling or moving to a location inside different paragraphs across my file. Then my friend shared this with me:

Do you grok Vim?

I still vaguely remember my excitement while reading that response. Vim is not a text editor. It’s a language.

The “Zen” of vi is that you’re speaking a language.

And that was all the convincing I needed to give it a proper shot. I immediately started using it for both my non-fiction writings and programming. Whenever I was curious about a new text editor on the scene (like Brackets or Atom), first thing I would check is if it has a VIM plugin. It wasn’t like my productivity skyrocketed immediately (in fact it slowed down initially) but I knew that regular usage and dicovering of new “verbs” in this “language” will make things easier overtime. Plugins were galore and I spent countless hours exploring the right ones for writing prose. Every new machine I was given, one of my first actions on it would be to import my .vimrc file. To my detriment, there was a phase when I was using less time actually using Vim and more time exploring plugins or reading articles or watching videos on powerful ways people use Vim.

That’s enough back story which was simply a filler for my today’s post. TIL about convering tab to spaces in Vim (I knew this existed but didn’t really paid attention to it). This is one of those frustrations, solutions to which should be understood early on in your Vim journey. Numerous languages or HTML template engines will throw errors on you if you mix tabs and spaces in the same file. So the only way to efficiently get around this is to completely get rid of tab character. It serves no particular purpose, especially when Vim allows you to use them as spaces.

So here are some useful commands you should add in your .vimrc file:

  1. for inserting spaces even when you press the Tab key:

    set expandtab
    # if you ever wanna go back:
    set noexpandtab

    You can even type this in Vim’s command line mode (same command preceded by “:”) but it will only change the new tab characters to spaces, not the existing ones. If you want to change all existing tabs to spaces from within a Vim document, simply do:

  2. To insert a set number of spaces when you press Tab key:

    set tabstop=2 # for 2 spaces
  3. If your programming language needs indentation, you can set the right amount of indentation (usually 2 or 4 spaces). Do this:

    set shiftwidth=2
  4. If you want to press backspace and go back a “tab”, you can use:

    set softtabstop=2 # press backspace/delete and go back 2 spaces
  5. What if you wanted different tab settings for different files? Let’s say you want to convert tab to spaces only for Python files:

    autocmd FileType * set noexpandtab | set shiftwidth=4
    autocmd FileType python set expandtab | set shiftwidth=2

As with everyt tactic in Vim, there are hundreds of sub-tactics. If you are a visual learner learn better via programming screencasts, consider watching Vimcast’s episode on tabs and spaces (actually watch everything there).