Look, Vim has a reputation. People joke about not being able to quit it. But once you get past that, it’s honestly one of the fastest editors out there — if you know a few tricks.
I’ve been collecting small Vim snippets over the years, and I figured it’s time to put them all in one place. These aren’t fancy plugins. They’re just little configs and hacks that make everyday editing way smoother. Think of this as a “copy, paste, and go” kind of guide.
Let’s get into it.
The Basics: Setting Up Your .vimrc
Before anything else, you need a solid foundation. Your .vimrc file is basically Vim’s settings file — it lives in your home directory and loads every time you open Vim. Here’s a starter config that makes life easier:
set hidden
set nowrap
set termguicolors
filetype on
let mapleader = ' '
let maplocalleader = '\'
nnoremap ; :
What does all this do?
set hidden— This lets you switch between open files (buffers) without Vim yelling at you to save first. Super handy when you’re jumping between files.set nowrap— Stops long lines from wrapping to the next line. If you’re working with data or wide code, this keeps things clean.set termguicolors— Turns on true color support in your terminal. Your color schemes will actually look the way they’re supposed to.filetype on— Tells Vim to detect what kind of file you’re editing (Python, JavaScript, etc.) so it can apply the right syntax highlighting and indentation.let mapleader = ' '— Sets your leader key to the spacebar. The leader key is like a custom modifier — you press it before other keys to trigger your own shortcuts. Space is easy to hit, so it’s a popular choice.nnoremap ; :— This one’s subtle but great. It maps the semicolon to colon in normal mode. Since you use:constantly to run commands, this saves you from hitting Shift every single time.
Handy Mappings from this config
map <c-l> :.w !pbcopy<CR><CR>
noremap <leader>aa gg<S-v><S-g>
<c-l>(Ctrl+L) — Copies the current line to your system clipboard usingpbcopy. This is a macOS thing. If you’re on Linux, swappbcopywithxclip -selection clipboard. Really useful when you need to grab a single line quickly.<leader>aa— Selects the entire file. It goes to the top, enters visual line mode, then jumps to the bottom. Basically “select all.” Great for when you want to copy or format the whole file at once.
Auto-Indent Your Entire File
Ever open a file and the indentation is all over the place? Maybe you pasted something weird, or someone on your team doesn’t believe in consistent tabs. Here’s the fix:
noremap <leader>= gg<S-v><S-g>=
How it works, step by step:
gg— Jump to the first line of the file<S-v>(Shift+V) — Enter visual line mode<S-g>(Shift+G) — Select all the way to the last line=— Auto-indent everything that’s selected
So when you press Space then = (assuming space is your leader), Vim re-indents your entire file in one shot. It uses whatever indentation rules Vim knows for that file type. For Python, HTML, JavaScript — it just works.
When to use this: After pasting code from the web, after a messy merge, or honestly just whenever things look off.
Ctrl+Click Style Navigation
You know how in VS Code you can Ctrl+Click on an import to jump to that file? You can get something similar in Vim:
noremap <leader>gg gdf/gf
augroup suffixes
autocmd!
let associations = [
\["javascript", ".js,.javascript,.es,.esx,.json"],
\["python", ".py,.pyw"]
\]
augroup END
What’s going on here:
<leader>gg— This mapping doesgd(go to definition/first occurrence) thenf/(find the next slash) thengf(go to file under cursor). It’s a quick way to follow file paths in your code.- The
suffixesaugroup — This tells Vim which file extensions to try when you usegf. So if your JavaScript code saysimport something from './utils', Vim will know to look forutils.js,utils.es, etc.
When to use this: When you’re navigating a project and want to quickly jump into imported files without leaving Vim. It’s not perfect like a full LSP setup, but for quick browsing it’s solid.
Swap Lines Up and Down
This one’s a classic. You want to move a line up or down — like reordering function arguments or shuffling list items. Instead of delete-paste-move-paste, here’s a cleaner way:
function! s:swap_lines(n1, n2)
let line1 = getline(a:n1)
let line2 = getline(a:n2)
call setline(a:n1, line2)
call setline(a:n2, line1)
endfunction
function! s:swap_up()
let n = line('.')
if n == 1
return
endif
call s:swap_lines(n, n - 1)
exec n - 1
endfunction
function! s:swap_down()
let n = line('.')
if n == line('$')
return
endif
call s:swap_lines(n, n + 1)
exec n + 1
endfunction
noremap <silent> <C-Up> :call <SID>swap_up()<CR>
noremap <silent> <C-Down> :call <SID>swap_down()<CR>
The breakdown:
swap_linesis a helper that takes two line numbers and swaps their content.swap_upmoves the current line one position up (unless you’re already at the top).swap_downmoves the current line one position down (unless you’re at the bottom).Ctrl+UpandCtrl+Downtrigger the swap.
When to use this: Reordering imports, reorganizing lists, moving code blocks around. It feels natural — like dragging a line with your arrow keys.
Align Text Without Plugins
This is probably the nerdiest one, but it’s so satisfying. Say you have code like this:
class Products(models.Model):
name = models.CharField()
price = models.DecimalField()
description = models.TextField()
created_at = models.DateTimeField()
And you want all the = signs to line up neatly. Here’s the trick — no plugins needed:
0f=20i<Space><Esc>020lvf=hx
Step by step:
0— Go to the first columnf=— Find the next=on the current line20i<Space><Esc>— Insert 20 spaces before the=0— Go back to the first column20l— Move forward 20 columnsvf=hx— Select and delete everything between your cursor and the=
The result? Your = sign ends up exactly at column 20. Do this on each line and everything lines up perfectly.
When to use this: When you want clean, aligned assignments or hash maps and you don’t want to install an alignment plugin. It’s a bit manual, but once you get the muscle memory, it’s fast. You can also record it as a macro (qq to start, q to stop, @q to replay) and run it on multiple lines.
Putting It All Together
Here’s the thing about Vim — you don’t need 50 plugins to be productive. A handful of smart mappings and a clean .vimrc can take you really far. Start with the basics:
- Set up your
.vimrcwith the essentials (hidden buffers, leader key, true colors) - Map common actions like select-all and copy-to-clipboard
- Learn the indent trick — you’ll use it more than you think
- Add line swapping — it’s a small thing that saves a lot of time
- Try the alignment hack — even if just to impress yourself
The best part? All of these are just plain Vimscript. No dependencies, no package managers, no compatibility issues. They work everywhere Vim runs.
Happy Vimming. And yes, it’s :wq to quit. You’re welcome.
