Now that we know how to open a file, edit it, move around in it, and save it, let us now look into how we can tweak it. My post to part 1 of vim basics can be found here: https://blog.lanlocked.xyz/post/vim-basics-part-1/. Part 2 can be found here: https://blog.lanlocked.xyz/post/vim-basics-part-2.
In this part, we'll cover visual mode, text objects (which will change how you think about editing), and then dive into customizing vim with your .vimrc configuration file.
Visual mode lets you select text before performing operations on it. It's more intuitive than some of vim's other modes and is great for when you need to see what you're working with.
There are three types of visual mode:
v " Character-wise visual mode
V " Line-wise visual mode (capital V)
Ctrl+v " Block visual mode
Character-wise visual mode (v):
Line-wise visual mode (V):
This is perfect for selecting entire lines:
Block visual mode (Ctrl+v):
This is one of vim's killer features - you can select rectangular blocks of text. Perfect for editing columnar data or adding the same text to multiple lines:
Example with block mode:
Let's say you have:
apple
banana
cherry
And you want to add "fruit: " to each line:
Result:
fruit: apple
fruit: banana
fruit: cherry
This is incredibly useful for commenting out blocks of code or editing configuration files.
Text objects are what separate vim beginners from vim power users. They let you operate on logical chunks of text rather than just characters or lines.
The syntax is:
Common text objects:
w " word
s " sentence
p " paragraph
t " tag (HTML/XML)
" " quoted text
' " single quoted text
( " parentheses content
{ " curly braces content
[ " square brackets content
Scopes:
i " inside (excludes delimiters)
a " around (includes delimiters)
Examples that will blow your mind:
diw " Delete inside word (cursor can be anywhere in the word)
daw " Delete around word (includes trailing space)
ci" " Change inside quotes
di( " Delete inside parentheses
ya{ " Yank around curly braces (includes the braces)
vi[ " Visual select inside square brackets
cit " Change inside tag (for HTML/XML)
Let me give you a real example. Say you have:
echo "Hello World"
With your cursor anywhere inside the quotes, type ci" and you can change the text inside the quotes. No need to navigate to the start, select, delete, etc. Just ci", type new text, and Esc.
Or let's say you have a function:
function do_something(param1, param2, param3) {
# code here
}
With your cursor anywhere inside the curly braces, type di{ and all the content inside is deleted. Type ci{ and you can start writing new function content immediately.
This is the vim "language" at its best. Once you internalize text objects, you'll find yourself thinking "change inside quotes" rather than "navigate to quote, select to end quote, delete, insert."
Here are some patterns I use daily:
Working with quotes:
ci" " Change text inside double quotes
ci' " Change text inside single quotes
di" " Delete text inside quotes
ya" " Yank text and quotes
Working with brackets/parens:
ci( " Change inside parentheses
ci{ " Change inside curly braces
ci[ " Change inside square brackets
da( " Delete around parentheses (including parens)
Working with words:
ciw " Change inside word (cursor anywhere in word)
caw " Change around word (includes trailing space)
diw " Delete inside word
Working with tags (HTML/XML):
cit " Change inside tag
dit " Delete inside tag
yat " Yank around tag (includes opening/closing tags)
For a sysadmin editing configs all day, text objects are a godsend. Need to change a value in quotes in a config file? ci" and done. Need to delete everything in a block? di{ and move on.
If you are running Linux, which I really hope you do, since I will not be going over how Apple nor Windows run it on their systems outside of WSL and WSL2, then you will have a vim config file labeled .vimrc in your home directory.
$ ls -lah ~ | grep vim
-rw------- 1 wretchedghost wretchedghost 49K Jun 13 10:31 .viminfo
-rw-r--r-- 1 wretchedghost wretchedghost 4.7K Apr 17 09:24 .vimrc
You might also have a .viminfo and even a .vim directory but for right now we will only focus on .vimrc. The default config for .vimrc has what the basics of what vim offers and has many, many, many tweakable features within. Some of the defaults are sane and don't require tweaking while others can be tweaked to your liking.
I usually don't like making too many tweaks to a program or add too many alias since as a system admin I run from system to system and can't always have the plugins and tweaks that I have on my main system. vim is the exception as I use this more often than almost any other program between tweaking config files to writing this blog to even writing excerpts to my various novel ideas.
Let me walk you through the settings I consider essential. Open your .vimrc:
$ vim ~/.vimrc
Basic Quality of Life Improvements:
" Enable line numbers
set number
" Enable relative line numbers (shows distance from current line)
set relativenumber
" Show command in bottom bar
set showcmd
" Highlight current line
set cursorline
" Enable syntax highlighting
syntax enable
" Use system clipboard for copy/paste
set clipboard=unnamedplus
The set number and set relativenumber combination is fantastic. You get the actual line number on your current line, but relative numbers everywhere else. This makes commands like 5j or 8k much easier because you can see exactly how many lines away something is.
Search Settings:
" Highlight search results
set hlsearch
" Search as you type
set incsearch
" Ignore case in searches
set ignorecase
" Unless search contains uppercase
set smartcase
The ignorecase and smartcase combo is brilliant. If you search for /hello, it matches "hello", "Hello", and "HELLO". But if you search for /Hello, it only matches "Hello". Smart!
Indentation and Tabs:
" Use spaces instead of tabs
set expandtab
" Number of spaces for a tab
set tabstop=4
" Number of spaces for autoindent
set shiftwidth=4
" Make backspace work like most programs
set backspace=indent,eol,start
" Auto-indent new lines
set autoindent
For config files and system admin work, I prefer 4 spaces. For Python or YAML, you might want 2 spaces. Adjust tabstop and shiftwidth to your preference.
File Handling:
" Enable file type detection
filetype plugin indent on
" Keep undo history between sessions
set undofile
" Don't create backup files
set nobackup
set nowritebackup
" Don't create swap files
set noswapfile
The undofile setting is amazing - you can undo changes even after closing and reopening a file. Some people don't like it, but I find it incredibly useful.
Interface Tweaks:
" Show line at 80 characters
set colorcolumn=80
" Always show status line
set laststatus=2
" Reduce timeout after escape
set timeoutlen=1000 ttimeoutlen=0
" Split to right and bottom (more natural)
set splitright
set splitbelow
" Show matching brackets
set showmatch
The colorcolumn at 80 characters is great for keeping your config files and scripts readable.
Here's a complete, commented .vimrc that combines everything above:
" Basic Settings
set number " Show line numbers
set relativenumber " Show relative line numbers
set showcmd " Show command in bottom bar
set cursorline " Highlight current line
syntax enable " Enable syntax highlighting
" Clipboard
set clipboard=unnamedplus " Use system clipboard
" Search
set hlsearch " Highlight search results
set incsearch " Search as you type
set ignorecase " Ignore case in searches
set smartcase " Unless uppercase is present
" Indentation
set expandtab " Tabs are spaces
set tabstop=4 " Tab width
set shiftwidth=4 " Indent width
set autoindent " Auto-indent new lines
set backspace=indent,eol,start " Better backspace behavior
" File Handling
filetype plugin indent on " Enable filetype detection
set undofile " Persistent undo
set nobackup " No backup files
set nowritebackup " No backup while editing
set noswapfile " No swap files
" Interface
set colorcolumn=80 " Show column at 80 chars
set laststatus=2 " Always show status line
set timeoutlen=1000 " Key timeout
set ttimeoutlen=0 " Escape key timeout
set splitright " Split right by default
set splitbelow " Split below by default
set showmatch " Show matching brackets
" Disable arrow keys to force hjkl usage (optional but helpful)
noremap <Up> <Nop>
noremap <Down> <Nop>
noremap <Left> <Nop>
noremap <Right> <Nop>
That last section (disabling arrow keys) is controversial, but it really helped me learn hjkl movement when I was starting out. You can remove it once you're comfortable.
After editing your .vimrc, you have two options:
I usually just type :so % when editing the .vimrc itself, which sources the current file.
Since you'll be editing configs across multiple systems, here are some portable settings I recommend:
Always include:
Consider skipping:
I keep my .vimrc in a git repository and can pull it down on new systems, but I make sure it doesn't depend on anything that needs to be installed separately.
I'm not covering plugins in this series since vanilla vim is incredibly powerful, but if you're interested, here are the most popular plugin managers:
Common plugins for sysadmins include:
But honestly, master vanilla vim first. You'll appreciate plugins more when you understand what they're actually doing for you.
Let's test what we've learned:
If you can do all that smoothly, you're well on your way to vim mastery!
We've covered a lot of ground in this three-part series. From opening and closing vim, to moving around efficiently, to editing like a pro with text objects, to customizing your environment with .vimrc.
The truth is, we've barely scratched the surface. Vim has been around since 1991 and has accumulated decades of features, tricks, and workflows. But what we've covered here will handle 95% of your daily editing needs.
My advice? Pick one or two new things from each article and force yourself to use them for a week. Don't try to learn everything at once. I still discover new vim tricks after over a decade of daily use.
The beauty of vim is that it grows with you. You can be productive on day one with just i, Esc, and :wq. But years later, you're still finding ways to work faster and more efficiently.
Visual Mode:
Text Objects (with action):
Essential .vimrc Settings:
Happy vimming, and remember: when in doubt, press Esc and :q!
This concludes the Vim Basics series. You now have all the tools you need to be dangerous with vim. Go forth and edit!