Right, let’s talk about your .vimrc. This is the file where you stop fighting Vim and start making it work for you. Think of it less like a settings menu and more like a series of commands you’re shouting at Vim before you even open a file, telling it exactly how to behave. Without this, you’re basically raw-dogging a text editor from 1991, and nobody wants that.

We’re going to build a sane, useful foundation. You’ll put this file in your home directory (~/.vimrc). It’s just a text file. Vim reads it on startup, and that’s that.

The Non-Negotiable Basics

Let’s start with the holy trinity of settings that every sane person enables. If you see someone without these on, they’re either a masochist or a liar.

" This must be the first line. It fixes a bunch of historical quirks and makes Vim behave in a less insane way.
set nocompatible

" Line numbers. 'nu' shows the absolute line number for the current line and relative numbers for others.
" This is crucial for motions like '5j' (move down 5 lines). 'rnu' makes that motion intuitive.
set number
set relativenumber

" Syntax highlighting. Without this, it's all monochrome misery. Vim is smart enough to figure out the file type.
syntax on

" Sensible tabs and indentation. This tells Vim a <Tab> should be 4 spaces wide, and that when you press
" <Tab> in insert mode or use the '>>' command, it should insert 4 spaces. 'expandtab' is the key here.
set tabstop=4
set shiftwidth=4
set expandtab

The expandtab directive is a hill I will die on. It inserts spaces when you press the Tab key. Why? Because if you don’t, you get a lovely mixture of tabs and spaces that will make your code look like a dog’s breakfast in any other editor or environment. Just use spaces. Trust me.

Why nocompatible is Your First Command

This isn’t just a suggestion; it’s a requirement. compatible mode makes Vim behave like its ancient ancestor, Vi, for the sake of some crusty old UNIX admin who hasn’t logged in since 1985. It disables pretty much every useful feature that makes Vim, well, Vim. Setting nocompatible turns on all those modern innovations you actually want, like command-line history and visual selection. It’s so important that if you don’t have a .vimrc, Vim assumes you’re a neanderthal and sets compatible for you. By creating a .vimrc, you automatically get nocompatible, but explicitly stating it is a good practice that prevents weirdness.

Taming the Wild Tab

The tab/indentation settings are where most people get tripped up, so let’s break it down:

  • tabstop=4: This is a display setting. It says “render a tab character as 4 columns wide on my screen.”
  • shiftwidth=4: This is an operation setting. It says “when I indent a line with >> or <<, move it by 4 columns.”
  • expandtab: This is the magic one. It says “when I press the Tab key in insert mode, don’t insert a tab character; insert spaces instead.”

Here’s the critical part: if you have expandtab set (which you should), then tabstop becomes almost purely a display setting for when you’re working on a file that someone else, who clearly didn’t read this section, polluted with actual tab characters. Your shiftwidth is what actually controls the size of your indentation.

Making Search Less Dumb

Vim’s default search is… fine. But we can make it brilliant with two settings.

" As you type your search pattern, highlight matches instantly. This is a game-changer.
set incsearch

" Ignore case when searching... unless you use a capital letter. This is smart case-sensitivity.
set ignorecase
set smartcase

The smartcase option is wonderfully clever. If you search for /function, it will match “function”, “Function”, and “FUNCTION”. But the moment you get fancy and search for /Function, it says “ah, you care about case now!” and will only match “Function”. It’s the best of both worlds.

The Edge Case You Will Hit: Filetype Detection

Here’s a weird one. You might find that your indentation settings work perfectly for Python but not for a Makefile. That’s because Makefiles require actual tab characters. It’s in the spec. If you have expandtab on, Vim will helpfully insert spaces, and then make will yell at you.

The solution is to let Vim be smarter than you. We can set options based on the file type.

" This enables filetype detection, which allows for plugin loading and indent files.
filetype plugin indent on

" Now, we can override our global 'expandtab' rule for specific file types.
" This says: for a 'make' file type, do NOT expand tabs to spaces.
autocmd FileType make setlocal noexpandtab

The setlocal part is important. It means this setting only applies to the current buffer (the file you’re editing), not your entire Vim session. This is the correct, non-destructive way to handle these exceptions. You can do this for any filetype (e.g., yaml, html, javascript).