Vim Text Objects: Using Vim Text Objects

Once you learn to think in text objects, you'll never be the same
Dayne Dayne (57)
Total time: 5 minutes 

Any vim user knows that there are underlying concepts worth understanding. While learning the specific keystrokes is useful, I find that knowing the why is crucial to actually adopting new features. Vim's text objects are one of these concepts and they are very worth understanding.

Text objects are vim's way of transcending the individual character to allow the user to think in words, sentences, sentences, and paragraphs. They are "contexts" in which your cursor exists and which you can act upon in various ways.

Note: if you are a complete novice, this is not the guide for you... this intro to vim is though!

As a reminder, vim's command format is <number><command><text object or motion>

Posted in these interests:

23 guides

We all know you can delete a single character using the x key in normal mode. But that's no fun! We should start thinking in words.

Note: I'll be using the | character to signify the position of your cursor in normal mode.

This is so|me text.

Typing diw will result in the following (notice the extra space):

This is  text.

So what did we do? We told vim to d delete the i inner w word. We can also tell vim to delete "a word".

This is so|me text.

Typing daw will result in the following:

This is text.

Text objects always follow this pattern. a acts upon the surrounding white space and i does not.

I find this particularly handy with the c (change) command. The c command removes the text object and starts your cursor in insert mode.

Once you've gotten used to thinking in characters AND words, it's time to level up to sentences. A sentence is defined as ending at a ., ! or ? followed by either the end of a line, or by a space or tab.

Howchoo is great. I always learn |something new when I come here. I plan to tell all of my friends about this wonderful website.

Typing d2as results in the following:

Howchoo is great.|

We told vim to d delete 2 s sentences. So vim deleted the sentence our cursor was currently in plus the next one.

Here we can clearly see the difference between acting on text objects and using motions. To move to the next sentence, you can press ( or ) in normal mode. In my experience, this is great for moving around but when modifying text, it is less flexible given that you have to first get your cursor in the exact right position before your command.

Once you've mastered the previous objects, it's time to learn paragraphs. This will complete your text object knowledge. According to the docs, a paragraph begins after each empty line, and also at each of a set of paragraph macros, specified by the pairs of characters in the 'paragraphs' option. We can think of it as any block of text surrounded by empty lines.

Here's a cool paragraph about Howchoo. 
I'd really like to spend more time on this website.
Every time I'm here, I learn something amazing.

I'll maybe even make it my homepage.|
Who even uses homepages anymore?

Typing cip results in the following:

Here's a cool paragraph about Howchoo. 
I'd really like to spend more time on this website.
Every time I'm here, I learn something amazing.


With a cursor in insert mode, ready to type.

Once you've mastered these concepts, you can get into more interesting text objects and even use custom built ones. Here are some bonus text objects to experiment with:

  • Single quotes: a'
  • Double quotes: a"
  • Parentheses: a)
  • Brackets: a]
  • Braces: a}
  • HTML tags: at
  • Angle Brackets: a>
Calling all writers!

We're hiring. Write for Howchoo

Dayne's profile pictureDayne
Joined in 2015
Software engineer, co-founder of Howchoo, and renaissance man. Lifelong amateur woodworker, espresso mechanic, freestyle lyricist, drummer, artist, runner, coffee roaster, electrical engineer, gamer, inventor, churner, psychoanalyst, photographer, pizza chef, pit master, audiophile, guitarist, entrepreneur, dad, yogi, cyclist, and barista.
Share this guide!
Related to this guide:
Vim undo branches
You probably already know how to use vim's basic undo and redo features. For a quick refresher, read this short tutorial on how to use vim redo and undo.
Dayne's profile picture DayneView
In these interests: vim
3 Ways to Use Tabs in Vim
Vim tabs can be a great way to visualize which files you currently have open. Many vim users get bogged down by them and either end up working too hard to use them or give up altogether.
Dayne's profile picture DayneView
In these interests: vim
Vim redo and undo
Learn how to undo and redo changes using vim. If you are brand new to vim, learn more about the basics in this intro to vim guide.
Dayne's profile picture DayneView
In these interests: vim
People also read:
Vim is a powerful tool. I often paste raw data into Vim in order to format it in a specific way, and sometimes I need to clean up the file by removing many blank lines.
One of the strengths of Vim is that it's highly customizable. And while remapping keys is possible, we often want to create shortcuts without changing the default key bindings.
If you find yourself highlighting text in Vim with your mouse to copy and paste it elsewhere, stop. There's a better way, using the yank command, to copy text into your clipboard on macOS or Windows.
Basic vim commands
Because you've accidentally opened Vim and don't know how to exit
Vim is amazing. Well, Python is doing all the work, but I still like Vim. Learn how to format JSON in Vim like a pro with this short guide! tl;dr Paste your json, and run this in vim normal mode. :%!
Tabs are evil. This guide will show you how to convert tabs to spaces in Vim.
Need to change the case of characters to all caps or all lowercase? This is easily done using Vim. This guide will show you how to change the case of characters in Vim.
Trailing whitespace can be a real pain - especially when diffing changes between two versions of a file.
Vundle, Pathogen, Vim-plug, VAM, and the others can be great time savers but sometimes you just have to do something yourself so you can understand what exactly the time saving software is doing for you in the background. For myself, I got sick of not understanding what these were doing to my system. I realized I did not know the fundamentals of vim plugins so I set out to learn. This is a step by step guide to teach you how to install a basic vim plugin without the help of any fancy manager.
Posted in these interests:
Discuss this guide: