DaleSchool

vimrc: Your Own Setup

Beginner20min

Learning Objectives

  • Explain the role and location of the .vimrc file
  • Set basics like set number and set relativenumber
  • Create key mappings with map and nmap
  • Configure and use the Leader key

Working Code

Do you turn on line numbers, tweak tab widths, or adjust search options every time you open Vim? Write it once in .vimrc and Vim applies it automatically on startup.

Create ~/.vimrc:

" show line numbers
set number
set relativenumber

" indent settings
set tabstop=2
set shiftwidth=2
set expandtab

" search settings
set ignorecase
set smartcase
set incsearch
set hlsearch

Save the file and reopen Vim — the settings are already active.

Try It Yourself

Start from an empty .vimrc and add one line at a time. Like adjusting your seat and mirrors before driving, tune Vim to fit you before you start working.

vim ~/.vimrc

Add each section in turn and observe the effect.

Step 1: Display basics

set number             " show line numbers
set relativenumber     " relative line numbers (relative to current line)
set cursorline         " highlight the current line
syntax on              " syntax highlighting

After saving (:w), run :source % to apply immediately.

Step 2: Editing

set tabstop=2          " tab = 2 spaces
set shiftwidth=2       " auto-indent = 2 spaces
set expandtab          " convert tabs to spaces
set autoindent         " auto-indent new lines

Step 3: Search

set ignorecase         " case-insensitive search
set smartcase          " case-sensitive when pattern has uppercase
set incsearch          " search as you type
set hlsearch           " highlight search results

Step 4: System integration

set clipboard=unnamedplus  " Vim yank → system clipboard
set mouse=a                " enable mouse support

Step 5: Leader key and mappings

let mapleader=" "          " use Space as the Leader key
nmap <leader>w :w<CR>      " Space+w = save
nmap <leader>q :q<CR>      " Space+q = quit
nmap <leader>h :noh<CR>    " Space+h = clear search highlight

Now instead of typing :w Enter you can save with Space w.

"Why?"

.vimrc is like the seat-memory button in a car. Adjusting the seat, mirrors, and steering every time is wasteful. Configure once, and every time you start up, it's right for you — that's what .vimrc does for Vim.

Why start from an empty file? Copy-pasting someone else's .vimrc wholesale drags in settings you don't understand, which causes problems. Adding one line at a time helps you verify "what does this change?" as you go.

What's the Leader key? Vim's default bindings use almost every key, so adding new shortcuts often conflicts. The Leader key is your personal shortcut namespace. Set let mapleader=" " to make Space your Leader, then combinations like Space+w or Space+q are free for you to define.

nmap vs. nnoremap: nmap is a recursive mapping — a mapped key can trigger another mapping. nnoremap is non-recursive, making it safer. In practice, prefer nnoremap. This lesson uses nmap for brevity, but in your own .vimrc, use nnoremap.

Deep Dive

Useful extras
set scrolloff=8        " keep 8 lines visible around the cursor
set signcolumn=yes     " always show the sign column on the left
set updatetime=250     " faster response (default 4000ms)
set splitright         " new vertical split goes to the right
set splitbelow         " new horizontal split goes below
set nowrap             " don't wrap long lines
set wildmenu           " enhanced command-line completion
Inspecting settings with :set

You can tweak settings inside Vim without editing .vimrc:

:set number            ← turn on line numbers
:set nonumber          ← turn them off
:set number?           ← check the current value
:set number!           ← toggle
:set all               ← show every setting

Experiment in Vim, and once you like something, add it to .vimrc.

Build a .vimrc from scratch, step by step:

  1. Open or create vim ~/.vimrc.
  2. Add this minimum configuration:
set number
set relativenumber
set tabstop=2
set shiftwidth=2
set expandtab
set ignorecase
set smartcase
set incsearch
set hlsearch
syntax on
let mapleader=" "
nmap <leader>w :w<CR>
nmap <leader>q :q<CR>
  1. Save with :w.
  2. Apply immediately with :source %.
  3. Check that line numbers appear.
  4. Press Space w to confirm the Leader mapping works.
  5. Add one extra setting you like.

Challenge: add nnoremap <leader>e :e ~/.vimrc<CR> so you can open .vimrc with Space e.

question: Where does the .vimrc file live? answers:

question: How do you apply a change to .vimrc without restarting Vim? answers:

question: How do you make the Leader key be Space? answers: