Vim Basics: Part 3

Vim Basics: Part 3

Building on the Basics

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: Selecting Text

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):

  1. Place your cursor at the start of text you want to select
  2. Press v
  3. Use movement keys (hjkl, w, e, etc.) to expand selection
  4. Perform an action (d to delete, y to yank, c to change)

Line-wise visual mode (V):

This is perfect for selecting entire lines:

  1. Press V (capital)
  2. Use j or k to select multiple lines
  3. Press d to delete, y to yank, or > to indent

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:

  1. Press Ctrl+v
  2. Use movement keys to select a block
  3. Press I to insert at the start of each line in the block
  4. Type your text
  5. Press Esc and watch it apply to all lines

Example with block mode:

Let’s say you have:

apple
banana
cherry

And you want to add “fruit: " to each line:

  1. Place cursor on ‘a’ in apple
  2. Press Ctrl+v
  3. Press jj to select down two more lines
  4. Press I (capital i)
  5. Type “fruit: "
  6. Press Esc

Result:

fruit: apple
fruit: banana
fruit: cherry

This is incredibly useful for commenting out blocks of code or editing configuration files.

Text Objects: The Game Changer

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.”

Common Text Object Workflows

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.

Customizing Vim with .vimrc

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.

My Essential .vimrc Settings

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.

My Personal .vimrc

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.

Testing Your .vimrc

After editing your .vimrc, you have two options:

  1. Close and reopen vim
  2. Run :source ~/.vimrc to reload the config in your current session

I usually just type :so % when editing the .vimrc itself, which sources the current file.

Config Tips for System Admins

Since you’ll be editing configs across multiple systems, here are some portable settings I recommend:

Always include:

  • set number - Essential for navigating config files
  • syntax enable - Makes configs much more readable
  • set hlsearch - Finding values in large configs
  • set expandtab and proper tab settings - Consistency across systems

Consider skipping:

  • Plugins - Most servers won’t have them
  • Complex mappings - Hard to remember when not using daily
  • Heavy customizations - Keep it simple for quick edits

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.

A Quick Note on Vim Plugins

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:

  • vim-plug - Simple and fast
  • Vundle - Popular and well-documented
  • Pathogen - Older but still used

Common plugins for sysadmins include:

  • nerdtree - File explorer
  • vim-fugitive - Git integration
  • vim-airline - Better status line
  • fzf.vim - Fuzzy file finder

But honestly, master vanilla vim first. You’ll appreciate plugins more when you understand what they’re actually doing for you.

Practice Exercise

Let’s test what we’ve learned:

  1. Open your .vimrc: vim ~/.vimrc
  2. Use visual mode (V) to select a few lines
  3. Delete them with d
  4. Undo with u
  5. Find a setting in quotes (like “80” if you have colorcolumn)
  6. Use ci" to change the value
  7. Save with :w
  8. Source your config with :so %

If you can do all that smoothly, you’re well on your way to vim mastery!

Wrapping Up

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.

Quick Reference for Part 3

Visual Mode:

  • v - character-wise
  • V - line-wise
  • Ctrl+v - block-wise

Text Objects (with action):

  • ciw - change inside word
  • ci" - change inside quotes
  • di( - delete inside parentheses
  • ya{ - yank around braces
  • cit - change inside tag

Essential .vimrc Settings:

  • set number - line numbers
  • set relativenumber - relative line numbers
  • set hlsearch - highlight searches
  • syntax enable - syntax highlighting
  • set expandtab - spaces not tabs
  • :so % - reload .vimrc

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!