Practical Vim Notes

ยท 8848 words ยท 42 minute read

My notes on Practical Vim. (Work in Progress)

1. The Vim Way ๐Ÿ”—

Vim is optimized for repetition. Its efficiency stems from the way it tracks our most recent actions.

Tip 1. Meet the Dot Command ๐Ÿ”—

The dot command lets us repeat the last change. It is the most powerful and versatile command in Vim.

The x command deletes the character under the cursor.

We can restore the file to its original state by pressing the u key a few times to undo the changes.

The dd command also performs a deletion, but this one acts on the current line as a whole.

The greater than uppercase G command increases the indentation from the current line until the end of the file.

Tip 2. Don’t Repeat Yourself ๐Ÿ”—

For such a common use case as appending a semicolon at the end of a series of lines, Vim provides a dedicated command that combines two steps into one.

Two for the Price of One ๐Ÿ”—

Compound Command uppercase C Equivalent in Longhand c dollar sign

Compound Command s Equivalent in Longhand c l

Compound Command uppercase S Equivalent in Longhand caret uppercase C

Compound Command uppercase I Equivalent in Longhand caret i

Compound Command uppercase A Equivalent in Longhand dollar a

Compound Command o Equivalent in Longhand uppercase A Enter

Compound Command uppercase O Equivalent in Longhand k o

Tip 3. Take One Step Back, Then Three Forward ๐Ÿ”—

We can pad a single character with two spaces (one in front, the other behind) by using an idiomatic Vim solution. At first it might look slightly odd, but the solution has the benefit of being repeatable, which allows us to complete the task effortlessly.

The s command compounds two steps into one: it deletes the character under the cursor and then enters Insert mode.

The f character command tells Vim to look ahead for the next occurrence of the specified character and then move the cursor directly to it if a match is found. So when we type f plus, our cursor goes straight to the next plus symbol.

The semicolon command will repeat the last search that the f command performed.

Tip 4. Act, Repeat, Reverse ๐Ÿ”—

When facing a repetitive task, we can achieve an optimal editing strategy by making both the motion and the change repeatable. Vim has a knack for this. It remembers our actions and keeps the most common ones within close reach so that we can easily replay them.

Repeatable Actions and How to Reverse Them ๐Ÿ”—


Make a change








Scan line for next character


f character or t character






Scan line for pervious character


uppercase F character or uppercase T character






Scan document for next match


slash pattern Enter key




uppercase N


Scan document for previous match


question mark pattern Enter key




uppercase N


Perform substitution


colon slash target slash replacement






Execute a sequence of changes


q x changes q


at sign x



at sign colon can be used to repeat any E X command.

We can repeat the last colon substitute command by pressing ampersand

Tip 5. Find and Replace by Hand ๐Ÿ”—

Vim has a colon substitute command for find-and-replace tasks, but with this alternative technique, we’ll change the first occurrence by hand and then find and replace every other match one by one.

We could search for the word “content” by pulling up the search prompt and spelling out the word in full.

Or we could simply place our cursor on the word and hit star key.

If you don’t see any highlighting, try running colon set h l s

Tip 6. Meet the Dot Formula ๐Ÿ”—

We’ve considered three simple editing tasks so far. Even though each problem was different, we found a solution using the dot command for each one.

The Ideal

One keystroke to Move, One Keystroke to Execute.

2. Normal Mode ๐Ÿ”—

Tip 7. Pause with Your Brush Off the Page ๐Ÿ”—

For those unused to Vim, Normal mode can seem like an odd default. But experienced Vim users have difficulty imagining it any other way. This tip uses an analogy to illustrate the Vim way.

Tip 8. Chunk Your Undos ๐Ÿ”—

In other text editors, invoking the undo command after typing a few words might revert the last typed word or character. However, in Vim we can control the granularity of the undo command.

In Vim, we can control the granularity of the undo command. From the moment we enter Insert mode until we return to Normal mode, everything we type (or delete) counts as a single change. So we can make the undo command operate on words, sentences, or paragraphs just by moderating our use of the Escape key.

So how often should you leave Insert mode? It’s a matter of preference, but I like to make each “undoable chunk” correspond to a thought.

Tip 9. Compose Repetable Changes ๐Ÿ”—

Vim is optimized for repetition. In order to exploit this, we have to be mindful of how we compose our changes.

Pressing d b deletes from the cursor’s starting position to the beginning of the word, but it leaves the final character intact. We can delete this regue character by pressing x.

The d a w command is easily remembered by the mnemonic delete a word.

Making effective use of the dot command often requires some forethought. If you notice that you have to make the same small changes in a handful of places, you can attempt to compose your changes in such a way that they can be repeated with the dot command.Recognizing those opportunities takes practice.

Tip 10. Use Counts to Do Simple Arithmetic ๐Ÿ”—

Most Normal mode commands can be executed with a count. We can exploit this feature to do simple arithmetic.

The control a and control x commands perform addition and subtraction on numbers. When run without a count they increment by one, but if we prefix a number, then we can add or substract by any whole number. For example, if we positioned our cursor on a 5 character, running 10 control a would modify it to read 15.

But what happens if the cursor is not positioned on a numeric digit?

If the cursor is not already positioned on a number, then the control a command will look ahead for a digit on the current line. If it finds one, it jumps straight to it. We can use this to our advantage.

Tip 11. Don’t Count If You Can Repeat ๐Ÿ”—

We can minimize the keystrokes required to perform certain tasks by providing a count, but that doesn’t mean that we should. Consider the pros and cons of counting versus repeating.

Tip 12. Combine and Conquer ๐Ÿ”—

Much of Vim’s power stems from the way that operators and motions can be combined.

Operator + Motion = Action ๐Ÿ”—

The d motion command can operate on a single character


d l,

a complete word


d a w

or an entire paragraph


d a p

Vim’s Operator Commands ๐Ÿ”—












Yank into register


g tilde


Swap case


g u


Make lowercase


g uppercase U


Make uppercase


greater than


Shift right


less than


Shift left


equal sign




exclamation point


Filger motion lines through an external program

The g tilde, g u, and g uppercase U commands are invoked by two keystrokes. In each case, we can consider the g to be a prefix that modifies the behavior of the subsequent keystroke.

3. Insert Mode ๐Ÿ”—

Tip 13. Make Corrections Instantly from Insert Mode ๐Ÿ”—

If we make a mistake while composing text in Insert mode, we can fix it immediately. There’s no need to change modes. Besides the backspace key, we can use a couple of other Insert mode commands to amke quick corrections.


Control h


Delete back one character


Control w


Delete back one word


Control u


Delete back to start of line

Tip 14. Get Back to Normal Mode ๐Ÿ”—

Insert mode is specialized for one task - entering text - whereas Normal mode is where we spend most of our time. So it’s important to be able to switch quickly between them.


Escape key


Switch to Normal mode


Control open square bracket


Switch to Normal mode


Control o


Switch to Insert Normal mode

Meet Insert Normal Mode ๐Ÿ”—

Insert Normal mode is a special version of Normal mode, which gives us one bullet.

Tip 15. Paste from a Register Without Leaving insert Mode ๐Ÿ”—

Vim’s yank and put operations are usually executed from Normal mode, but sometimes we might want to paste text into the document without leaving Insert mode.

Use Control r register for Character-wise Registers ๐Ÿ”—

The Control r register command is convenient for pasting a few words from Insert mode. If the register contains a lot of text, you might notice a slight delay before the screen updates.

Tip 16. Do Back-of-the-Envelope Calculations in Place ๐Ÿ”—

The expression register allows us to perform calculations and then insert the result directly into our document.

The expression register is addressed by the equal symbol. From Insert mode we can access it by typing Control r equal sign. This opens a prompt at the bottom of the screen where we can type the expression that we want to evaluate. When done, we hit Enter, and Vim inserts the result at our current position in the document.

Tip 17. Insert Unusual Characters by Character Code ๐Ÿ”—

Vim can insert any character by its numeric doe. This can be handy for entering symbols that are not found on the keyboard.

Tip 18. Insert Unusual Characters by Digraph ๐Ÿ”—

While Vim allows us to insert any character by its numeric code, these can be hard to remember and awkward to type. We can also insert unusual characters as digraphs: pairs of characters that are easy to remember.

Tip 19. Overwrite Existing Text with Replace Mode ๐Ÿ”—

Replace mode is identical to Insert mode, except that it overwrites existing text in the document.

From Normal mode, we can engage Replace mode with the uppercase R command.

Overwrite Tab Characters with Virtual Replace Mode ๐Ÿ”—

Vim has a second variant of Replace mode. Virtual Replace mode is triggered with g uppercase R and treats the tab character as though it consisted of spaces. In Virtual Replace mode, we overwrite characters of screen real estate rather than dealing with the actual characters that would eventually be saved in a file. This tends to produce fewer surprises, so I would recommend using Virtual Replace mode whenever possible.

Vim also provides a single-shot version of Replace mode and Virtual Replace mode. The r character and g r character commands allow us to overwrite a single character before switching straight back to Normal mode.

4. Visual Mode ๐Ÿ”—

Tip 20. Grok Visual Mode ๐Ÿ”—

Visual mode allows us to select a range of text and then operate upon it. However intuitive this might seem, Vim’s perspective on selecting text is different from other text editors.

Tip 21. Define a Visual Selection ๐Ÿ”—

Visual mode’s three submodes deal with different kinds of text.

Enalbling Visual Modes ๐Ÿ”—




Enable character-wise Visual mode


uppercase V


Enable line-wise Visual Mode


Control v


Enable block-wise Visual mode


g v


Reselect the last visual selection

Switching Between Visual Modes ๐Ÿ”—


Escape key or Control open square bracket


Switch to Normal mode


v or uppercase V or Control v


Switch to Normal mode




Switch to character-wise Visual mode


uppercase V


Switch to line-wise Visual mode


Control v


Switch to block-wise Visual mode




Go to other end of highlighted text

Toggling the Free End of a Selection ๐Ÿ”—

The range of a Visual mode selection is marked by two ends: one end is fixed and the other moves freely with our cursor. We can use the o key to toggle the free end. This is really handy if halfway through defining a selection we realize that we started in the wrong place. Rather than leaving Visual mode and starting afresh, we can just hit o and redefine the bounds of the selection.

Tip 22. Repeat Line-Wise Visual Commands ๐Ÿ”—

When we use the dot command to repeat a change made to a visual selection, it repeats the change on the same range of text.

Tip 23. Prefer Operators to Visual Commands Where Possible ๐Ÿ”—

Visual mode may be more intuitive than Vim’s Normal mode of operation, but it has a weakness: it doesn’t always play well with the dot command. We can route around this weakness by using Normal mode operators when appropriate.

We can select the inner contents of a tag by running v i t, which can be read as: visually select inside the tag.

Using a Normal Operator ๐Ÿ”—

The Visual mode uppercase U command has a Normal mode equivalent: g uppercase U motion.

If we want to set up the dot command so that it repeats something useful, then we’re better off staying out of Visual mode. As a general rule, we should prefer operator commands over their Visual mode equivalents when working through a repetitive set of changes.

Tip 24. Edit Tabular Data with Visual-Block Mode ๐Ÿ”—

We can work with rows of text in any editor, but manipulating columns of text requires a more specialized tool. Vim provides this capability in the from of its Visual-Black mode, which we’ll use to transform a plain-text table.

Tip 25. Change Columns of Text ๐Ÿ”—

We can use Visual-Block mode to insert text into several lines of text simultaneously.

Tip 26. Append After a Ragged Visual Block ๐Ÿ”—

Visual-Block mode is great for operating on rectangular chunks of code such as lines and columns, but it’s not confined to rectagular regions of text.


We have three lines with different lengths and we want to add a semicolon at the end of each line.

you can acomplish this by

Control v j j dollar

uppercase A then semicolon

Escape Key

5. Command-Line Mode ๐Ÿ”—

Tip 27. Meet Vim’s Command Line ๐Ÿ”—

Command-Line mode prompts us to enter an Ex command, a search pattern, or an expression.

When we press the colon key, Vim switches

E X commands that operate on the Text in a Buffer ๐Ÿ”—


colon range delete x


Delete specified lines into register x


colon range yank x


Yank specified lines into register x


colon line put x


Put the text from register x after the specified line


colon range copy address


Copy the specified lines to below the line specified by address


colon range move address


Move the specified lines to below the line specified by address


colon range join


Join the specified lines


colon range normal commands


Execute Normal mode commands on each specified line


colon range substitute slash pattern slash string slash flags


Replace occurences of pattern with string on each specified line


colon range global slash pattern slash c m d


Execute the E X command c m d on all specified lines where the pattern matches

We can use E X commands to read and write files

colon edit and colon write

To create tabs

colon tab new

To split windows

colon split

To interact with the argument list

colon prev or colon next

To interact with the buffer list

colon b prev or colon b next

See colon h e x dash c m d dash index for the full list

E X Commands Strike Far and Wide ๐Ÿ”—

As a general rule, we could say that E x commands are long range and have the capacity to modify many lines in a single move. Or to condense that even further: E x commands strike far and wide.

Tip 28. Execute a Command on One or More Consecutive Lines ๐Ÿ”—

Many E x commands can be given a range of lines to act upon. We can specify the start and end of a range with either a line number, a mark, or a pattern.




First line of the file




Last line of the file




Virtual line above first line of the file




Line where the cursor is placed


single quotation m


Line containing mark m


single quotation less than


Start of visual selection


single quotation greater than


End of visual selection




The entire file (shorthand for colon 1 comma dollar)

Tip 29. Duplicate of Move Lines Using colon t and colon m Commands ๐Ÿ”—

Duplicate LInes with the colon t Command ๐Ÿ”—

The colon copy command and its shorthand colon t lets us duplicate one or more lines from one part of the document to another, while the colon move command lets us place them somewhere else in the document


colon 6 copy period

Make a copy of line 6 and put it below the current line.

colon 6 t period

Copy line 6 to just below the current line

colon t 6

Copy the current line to just below line 6

colon t period

Duplicate the current line (similar to Normal mode y y p)

colon t dollar sign

Copy the current line to the end of the file

colon single quotation less than comma single quotation t 0

Copy the visually selected lines to the start of the file

Move Lines with the colon m Command ๐Ÿ”—

colon range move address

We can shorten it to a single letter colon m

Tip 30. Run Normal Mode Commands Across a Range ๐Ÿ”—

colon % normal uppercase A semicolon

The % symbol is used as a range representing the entire file.

colon % normal i slash slash

This single command could be used to comment out an entire JavaScript file

Tip 31. Repeat the Last Ex Command ๐Ÿ”—

at sign colon to repeat the command

After running at sign colon for the first time, we can subsequently repeat it with the at sign at sign command.

Tip 32. Tab-Complete Your Ex Commands ๐Ÿ”—

colon col control d

The control d command asks Vim to reveal a list of possible completions.

Tip 33. Insert the Current Word at the Command Prompt ๐Ÿ”—

control r control w

Tip 34. Recall Commands from History ๐Ÿ”—

Vim records the commands that we enter in Command-Line mode and provides two ways of recalling them: scrolling through past command-lines with the cursor keys or dialing up the command-line windows.

Press q colon to open the command-line window

The command-line window is like a regular Vim buffer, where each line contains an item from our history. With the k and j keys, we can move backward and forward through our history. When we press Enter key, the contents of the current line are executed as an E X command.

The beauty of the command-line window is that it allows us to change historical commands using the full modal editing power of Vim.

What if halfway through composing an E X command at the prompt, we realize that we need more editing power? In Command-Line mode, we can use the Control F mapping to switch to the command-line window, preserving a copy of the command that was typed at the prompt.

q slash

Open the command-line window with history of searches

q colon

Open the command-line window with history of E X commands

control f

Switch from Command-Line mode to the command-line window

Tip 35. Run Commands in the Shell ๐Ÿ”—

We can easily invoke external programs without leaving Vim. Best of all, we can send the contents of a buffer as standard input to a command or user the standard output from an external command to populate our buffer.

Executing Programs in the Shell ๐Ÿ”—

From Vim’s Command-Line mode, we can invoke external programs in the shell by prefixing them with a bang symbol.

What if we want to run serveral commands in the shell? In that case, we can use Vim’s colon shell command to start an interactive shell session.

Using the Contents of a Buffer for Standard Input or Output ๐Ÿ”—

colon read exclamation symbol command which puts the output from the command into our current buffer.

Tip 36. Run Multiple Ex Commands as a Batch ๐Ÿ”—

Instead of executing these commands one by one, we could put them all into a file and save it to disk.

We can use the colon source command to execute a script.

**colon arg do source batch period v i m

6. Manage Multiple Files ๐Ÿ”—

Tip 37. Track Open Files with the Buffer List ๐Ÿ”—

We can load multiple files during an editing session. Vim lets us manage them using the buffer list.

Most Vim commands operate on buffers, but a few operate on files, including the colon write, colon update, and colon save as commands.

The colon l s command gives us a listing of all the buffers that have been loaded into memory.

We can switch to the next buffer in the list by running the colon b next command

The % symbol indicates which of the buffer is visible in the current window, while the hash symbol represents the alternate file.

We can quickly toggle between the current and alternate files by pressing control caret

Use the Buffer List ๐Ÿ”—

We can traverse the buffer list using four commands.

colon b prev and colon b next to move backward and forward one at a time.

And colon b first and colon b last to jump to the start or end of the list.

The colon l s listing starts with a digit, which is assigned to each buffer automatically on creation. We can jump directly to a buffer by number, using the colon buffer uppercase N command. Alternatively, we can use the more intuitive form colon buffer buf name The buf name need only contain enough characters from the filepath to uniquely identify the buffer.

The colon buf do command allows us to execute an E X command in all of the buffers listed by colon l s

Deleting Buffers ๐Ÿ”—

If we want to delete a buffer, we can do so using the colond b delete command.

This can take one of these forms.

colon b delte N 1 N 2 N 3

colon N comma M b delete

If we wanted to delete buffers numbered 5 through 10 inclusive, we could do so by running colon 5 comma 10 b d.

Vim’s built-in controls for managing the buffer list lack flexibility.

Tip 38. Group Buffers into a Collection with the Argument List ๐Ÿ”—

The argument list is easily managed and can be useful for grouping together a collection of files for easy navigation. We can run as E X command on each item in the argument list using the colon arg do command.

Specify Files by Name ๐Ÿ”—

The simplest way of populating the argument list is by specifying filesnames one by one


Open file one and file two

colon a r g s file one file two

colon a r g s

Specify Files by Glob ๐Ÿ”—

The start symbol will match zero or more characters, but only in the scope of the specified directory.

The star star wildcard also matches zero or more characters, but it can recurse downward into directories below the specified directory.

We can combine these wildcards and use partial filenames or directories to form pattern, also known as globs, that match the set of files we’re interested in.


colon a r g s star period star

Files Matching the Expansion

all the files in the current directory


colon a r g s star star slash star period j s

Files Matching the Expansion

all the JavaScript files in the current directory and child directories


colon a r g s star star slash star period star

Files Matching the Expansion

All files in the current directory and sub directories

Just as we can use more than one filename in the a r g list, we can also supply more than one glob. If we wanted to build an argument list containing all j s and c s s files but not other file types, we could use these globs.

colon a r g s star star slash star period j s space star star slash star period c s s

Tip 39. Manage Hidden Files ๐Ÿ”—

When a buffer has been modified, Vim gives its special treatment to ensure that we don’t accidentally quit the editor without saving our changes.

Vim raises an error message, reporting that the current buffer contains unsaved changes.

colon b next exclamation point

The bang symbol forces Vim to switch buffers, even if our current buffer has unsaved changes.

Handle Hidden Buffers on Quit ๐Ÿ”—

Vim loads the first hidden buffer with modifications into the current window so that we can decide what to do with it.

If we want to keep the changes, we can run colon write to save the buffer to a file.

Or if we want to discard the changes, we can instead run colon edit bang which rereads the file from disk, overwriting the contents of the buffer.

If we want to quit Vim wihtout reviewing our unsaved changes, we can issue the colon q all bang command.

Or if we want to write all modified buffers without reviewing them one by one, we can use the colon w all command.

Tip 40. Divide Your Workspace into Split Windows ๐Ÿ”—

Vim allows us to view multiple buffers side by side by dividing our workspace into split windows.

Creating Split Windows ๐Ÿ”—

When Vim starts up, it contains a single window. We can divide this window horizontally with the Control w then s command, which creates two windows of equal height.

Or we can use the Control w then v command to split the window vertically, producing two windows of equal width.


colon split or colon s p then file name


Split the current window horizontally, loading file into the new window


colon v split or colon v s p then file name


Split the current window vertically, loading file into the new window.

Changing the Focus Between Windows ๐Ÿ”—


Control w then w


Cycle between open windows


Control w then h


Focus the window to the left


Control w then j


Focus the windows below


Control w then k


Focus the windows above


Control w then l


Focus the window to the right

Closing Windows ๐Ÿ”—

E X Command

colon close or just colon c l o


Close the active window

E X Command

**colon only or just colon o n


Keep only the active window, closing all others

Normal Command to close the active window

Control w then c

Normal Command to keep only the active window, closing all others

Control w then o

Resizing and Rearranging Windows ๐Ÿ”—

To Equalize width and height of all windows

Control w then equal sign

To Maximize height of the active window

Control w then underscore

To Maximize width of the active windows

Control w then pipe

To Set active window height to N rows

N Control w then underscore

To Set active window width to N columns

N Control w then pipe

Tip 41. Organize Your Window Layouts with Tab Pages ๐Ÿ”—

Vim’s tabbed interface is different from that of many other text editors. We can use tab pages to organize split windows into a collection of workspaces.

How to Use Tabs ๐Ÿ”—

Vim’s tab pages can be used to partition work into different workspaces.

The colon l c d path command lets us set the working directory locally for the current window.

Opening and Closing Tabs ๐Ÿ”—

We can open a new tab page with the colon tab edit filename command.

Alternatively, if the current tab page contains more than one window, we can use the Control w then uppercase T command, which moves the current window into a new tab page.

If the active tab page contains only a single window, the colon close command will close the window and the tab page with it.

Or we can use the colon tab close command, which closes the current tab page no matter how many windows it contains.

Finally, if we want to close all tab pages except for the current one, we can use the colon tab only command.

Switching Between Tabs ๐Ÿ”—

To Switch to tab page number N

You can colon tab n page number N in E X Command or N g t in Normal Command

To Switch to the next tab page,

You can colon tab n in E X Command or g t in Normal Command

To Switch to the previous tab page,

You can colon tab p in E X Command or g uppercase T in Normal Command

Rearranging Tabs ๐Ÿ”—

We can use the tab move N E X command to rearrange tab pages.

When N is 0, the current tab page is moved to the beginning

and if omit N, the current tab page is moved to the end.

7. Open Files and Save Them to Disk ๐Ÿ”—

Tip 42. Open a File by Its Filepath Using colon edit ๐Ÿ”—

The colon edit command allows us to open files from within Vim, either by specifying an absolute or a relative filepath.

Open a File Relative to the Active File Directory ๐Ÿ”—

The % symbol is a shorthand for the filepath of the active buffer.

Pressing the tab key expands the filepath, revealing the absolute path of the active buffer.

The colon h modifier removes the filesname while preserving the rest of the path.

Entering colon edit % colon h tab expands to the full path of the current file’s directory.

Tip 43. Open a File by Its Filename Using colon find ๐Ÿ”—

The colon find command allows us to open a file by its name without having to provide a fully qualified path. To exploit this feature, we first have to configure the ‘path’ setting.

Configure the ‘path’ ๐Ÿ”—

The ‘path’ option allows us to specify a set of directories inside of which Vim will search when the colon find command is invoked.

For example

If we want to make it easier to look up files in the app slash controllers and app slash view directories,

we can add these to our path simply by running this

colon set path plus sign equal sign app slash star star

The star star wildcard matches all subdirectories beneath the app directory

Use colon find to look up Files by Name ๐Ÿ”—

Now that we’ve configured our path, we can open files in the directories we specified by providing just their filename.

We can use the tab key to autocomplete filenames.

Tip 44. Explore the File System with n e t r w ๐Ÿ”—

In addition to letting us view and edit the contents of a file, Vim also lets us view the contents of a directory. The net r w plugin, included in the Vim distribution, allows us to explore the file system.

Preparation ๐Ÿ”—

This plugin comes as standard with the Vim distribution, so we don’t have to install anything, but we do need to make sure that Vim is configured to load plugins. These lines of configuration are the minimum requirement for your v i m r c file

set no compatible

file type plugin on

Opening the File Explorer ๐Ÿ”—

To open file explorer for current working directory

colon e .

To open file explorer for the directory of the active buffer

colon uppercase E

In addition to colon explore, net r w also provides colon uppercase S explore and colon uppercase V explore commands, which open the file explorer in a horizontal split window or vertical window, respectively.

Working with Split Windows ๐Ÿ”—

Think of each window as a playing card. One side of the card shows the contents of a file, and the other side shows the file explorer. When we run the colon Explore command, the card for the active window flips over to show the side with the file explorer.

After choosing the file we want to edit, we press Enter key and the card flips over again, this time showing the contents of the file that we just selected.

After summoning the file explorer view, if we decide that we want to switch back to the buffer we were already editing, we can do using the control tilde command.

Tip 45. Save Files to Nonexistent Directories ๐Ÿ”—

Vim is happy to let us edit a buffer whose path includes directories that don’t exist. It’s only when we attempt to write the buffer to a file that Vim objects.

Use the external m m d i r program to create a directory

colon exclamation point m k d i r space dash p space % colon h

colon write

Tip 46. Save a File as the Super User ๐Ÿ”—

Running Vim as the super user isn’t normal, but sometimes we have to save changes to a file that requires sudo permission.

We can do so without restarting Vim by delegating the task to a shell process and running that with sudo.

colon w space exclamation point su do space tee space % space greater than space slash dev slash null

The % symbol has special meaning on Vim’s command line

It expands to represent the path of the current buffer.

8. Navigate Inside Files with Motions ๐Ÿ”—

Tip 47. Keep Your Fingers on the Home Row ๐Ÿ”—

Vim provides much quicker ways of moving around.

You’re wasting keystrokes if you press the h key more than two times in a row.

When it comes to moving horizontally, you can get around quicker using word-wise or character search motions.

Tip 48. Distinguish Between Real Lines and Display Lines ๐Ÿ”—

Avoid frustration by learning the difference between real lines and display lines.

Vim lets us operate on both.

Understanding the difference between real and display lines is important because Vim provides motions for interacting with both kinds.

The j and k commands move down and up by real lines.

Whereas the g j and g k commands move down and up by display lines.

To move cursor down one real line


To move cursor down on display line

g j

To move cursor up one real line


To move cursor up one display line

g k

To move cursor to first character of real line


To move cursor to first character of display line

g 0

To move cursor to first nonblank character of real line


To move cursor to first nonblank character of display line

g tilde

To move cursor to end of real line

dollar sign

To move cursor to end of display line

g dollar sign

Tip 49. Move Word-Wise ๐Ÿ”—

Vim has two speeds for moving backward and forward word-wise. Both allow for a more rapid traveral than moving by one column at a time.

To move cursor forward to start of next word


To move cursor backward to start of current or previous word


To move cursor forward to end of current or next word


To move cursor backward to end of previous word

g e

Append at the end of the current word

e a

Append at the end of the previous word

g e a

Know Your Words from Your WORDS ๐Ÿ”—

A word consists of a sequence of letters, digits, and underscores, or as a sequence of other nonblank characters separated with whitespace.

The difinition of a all uppercase WORD is simpler; it consists of a sequence of nonblank chracters separated with whitespace.

Use all uppercase WORD-wise motions if you want to move faster.

And use word-wise motions if you want a more fine-grained traversal.

Tip 50. Find by Character ๐Ÿ”—

Vim’s character search commands allow us to move quickly within a line, and they work beautifully in Operator-Pending mode.

The f char command is one of the quickest methods of moving ar

Tip 51. Search to Navigate ๐Ÿ”—

To forward to the next occurrence of char

f char

To backward to the previous occurrence of char

uppercase F char

To forward to the character before the next occurrence of char

t char

To backward to the character after the previous occurrence of char

uppercase T char

To repeat the last character-search command


To reverse the last character-search command


Think LIke a Scrabble Player ๐Ÿ”—

The character search commands can be highly economical with keystrokes, but their efficiency varies depending on our choice of target.

As any Scrabble player can tell you, some letters appear more frequently than others.

If we can make a habit of choosing less common characters for use with the f char command, then we’ll be more likely to strike our target with a single move.

Tip 51. Search to Navigate ๐Ÿ”—

The search command allows us to rapidly cover distances both large and small with very few keystrokes.

Operate with a Search Motion ๐Ÿ”—

We’re not limited to using the search command in Normal mode. We can use it from Visual and Operator-Pending modes just as well to do real work.

Tip 52. Trace Your Selection with Precision Text Objects ๐Ÿ”—

Text objects allow us to interact with parentheses, quotes, XML tags, and other common patterns that appear in text.

Vim’s text objects consist of two characters, the first of which is always either i or a.

In genral, we can say that the text objects prefixed with i select inside the delimiters, whereas those that are prefixed with a select everything including the delimiters.

As a mnemonic, think of i as inside and a as all.

To select a pair of parentheses

a parentheses

To select a pair of curly braces

a closing curly braces

To select a pair of brackets

a closing bracket

To select a pair of angle brackets

a closing angle bracket

To select a pair of single quotes

a single quote

To select a pair of double quotes

a double quote

To select a pair of backticks

a backtick

To select a pair of tags

a t

To select inside of parentheses

i closing parentheses

To select inside curly braces

i closing curly brace

To select inside of brackets

i closing bracket

To select inside of angle bracket

i closing bracket

To select inside of single quotes

i single quote

To select inside double quotes

i double quote

To select inside backticks

i backtick

To select inside tags

i t

Visual mode makes for a nice introduction to text objects because it’s easy to see what’s happening.

But text objects reveal their true power when we use them in Operator-Pending mode.

Text objects are not motions themselves: we can’t use them to navigate around the document.

But we can use text objects in Visual mode and in Operator-Pending mode.

Remember shi: whenever you see motion as part of the syntax for a command, you can also use a text object.

Common examples include

d motion

c motion

y motion

We can read the c i double quote command as “chage inside the double quotes”

The c i t command can be read as “change inside the tag.”

Tip 53. Delete Around, or Change Inside ๐Ÿ”—

Text objects usually come in pairs: one that acts inside the object and another that acts around the object.

Vim’s text objects fall into two categories

Those that interact with pairs of delimiters,

such as i closing parentheses

and those that interact with chunks of text, such as words, sentences, and paragraphs.

i w selects word

i uppecase W selects uppercase WORD

i s selects sentence

i p selects paragraph

a w selects word plus spaces

a uppercase W selects uppercase WORD plus spaces

a s selects sentence plus spaces

a p selects paragraph plus blank lines

Suppose that we want to delete the world “excellent” from the following sentence.

We can do it using the d a w command.

If we used d i w instead, then we’d end up with two adjacent spaces, which is probably not what we want.

Now let’s suppose that we want to change the word to something else.

The c i w command deletes the word without trimming any whitespaces and then puts us into Insert mode.

That’s just what we want.

As a general rule, we could say that the d motion command tends to work well with a w, a s, and a p, whereas the c motion command works better with i w and similar.

Tip 54. Mark Your Place and Snap Back to It ๐Ÿ”—

Vim’s marks allow us to jump quickly to locations of interest within a document.

We can set marks manually, but Vim also keeps track of certain points of interest for us automatically.

The m then all uppercase letter and lowercase letters command marks the current cursor location with the designated letter.

Lowercase marks are local to each individual buffer, whereas uppercase marks are globally accessible.

Vim does nothing to indicate that a mark has been set, but if you’ve done it right, then you should be able to jump directly to your mark with only two keystrokes from anywhere in the file.

Vim provides two Normal mode commands for jumping to a mark.

single quotation mark moves to the line where a mark was set, positioning the cursor on the first non-whitespace character.

backtick mark command moves the cursor to the exact position where a mark was set, restoring the line and the column at once.

The m m and backtick m commands make a handy pair.

Automatic Marks ๐Ÿ”—

We can set up to twenty-six lowercase marks per buffer.

Many of the tasks that would have required a mark in v i can be done in VIM using Visual mode.

The marks that Vim sets for us automatically can be really handy.

Tip 55. Jump Between Matching Parentheses ๐Ÿ”—

Vim provides a motion that lets us move between opening and closing pairs of parentheses.

The % command lets us jump between opening and closing sets of parentheses.

Jump Between Matching Keywords ๐Ÿ”—

Vim ships with a plugin called matchit, which enhances the functionality of the % command.

9. Navigate Between Files with Jump ๐Ÿ”—

As we learned in the previous chapter, motions allow us to move around within a file.

Jumps are similar, except that they can also move us between different files.

Tip 56. Traverse the Jump List ๐Ÿ”—

Vim records our location before and after making a jump and provides a couple of commands for retracing our steps.

In web browser, we’re used to using the back button to return to pages that we visited earlier.

Vim provides a similar feature by way of the jump list:

the control o command is like the back button while the complementary control i command is like the forward button.

Tip 57. Traverse the Change List ๐Ÿ”—

Vim records the location of our cursor after each change we make to a document. Traversing this change list is simple and can be the quickest way to get where we want to go.

Vim maintains a list of the modifications we make to each buffer during the course of an editing session.

It’s called the change list.

Using the g semi colon and g comma commands, we can traverse backward and forward through the change list.

colon changes

Marks for the Last Change ๐Ÿ”—

Vim automatically creates a couple of marks that complement the change list.

The backtick period mark always references the position of the last change.

While the backtick tilde mark tracks the position of the cursor the last time that Insert mode was stopped.

In most scenarios, jumping to the backtick period mark has the same effect as the g semicolon command.

Tip 58. Jump to the Filename Under the Cursor ๐Ÿ”—

Vim treats filenames in our document as a kind of hyperlink. When configured properly, we can use the g f command to go to the filename under the cursor.

Tip 59. Snap Between Files Using Global Marks ๐Ÿ”—

A global mark is a kind of bookmark that allows us to jump between files. Marks can be especially useful for snapping back to a file after exploring a codebase.

Set a Global Mark Before Going Code Diving ๐Ÿ”—

Global marks can be especially useful when we need to browse through a set of files and then quickly snap back to where we started.

Global marks are only useful if we have the forethought to set them up correctly in advance.

Try to get into a habit of setting a global mark before using any commands that interact with the quickfix list, such as colon grep, colon vimgrep and colon make.

The same goes for the commands that interact with the buffer and argument list, such as colon args arglist and colon arg do.

10. Copy and Paste ๐Ÿ”—

Tip 60. Delete, Yank, and Put with Vim’s Unnamed Register ๐Ÿ”—

Vim’s delete, yank, and put commands are designed to make common tasks easy by default.

Normally when we discuss cut, copy, and paste, we talk about putting text on a clipboard.

In Vim’s terminology, we don’t deal with a clipboard but instead with registers.

x p commands can be considered as “Transpose the next two chracters”

Transposing Lines ๐Ÿ”—

The d d p sequence could be considered to stand for “Transpose the order of this line and its successor”

Duplicating Lines ๐Ÿ”—

y y p

Tip 61. Grok Vim’s Registers ๐Ÿ”—

Rather than using a single clipboard for all cut, copy, and paste operations, Vim provides multiple registers.

When we use the delete, yank, and put commands, we can specify which register we want to interact with.

Addressing a Register ๐Ÿ”—

To yank the current word into register a,

double quotation a y i w

Tot cut the current line into register b,

double quotation b d d

To paste the word from register a

double quotation a p

To paste the line from register b

double quotation b p

To cut the current lin into register c in E X command

colon put c

To paste it below the current line

colon put c

The Unnamed Register ๐Ÿ”—

If we don’t specify which register we want to interact with, the Vim will use the unnamed register, which is addressed by the double quotation symbol.

To address this register explicitly, we have to use two double quote marks.

The Yank Register ๐Ÿ”—

When we use the y motion command, the specified text is copied not only into the unnamed register, but also into the yank register, which is addressed by the 0 symbol.

The Named Registers ๐Ÿ”—

Vim has one named register for each letter of the alphabet.

That means that we can cut

double quote a d motion


double quote a y motion

or paste

double quote a p

up to twenty-six pieces of text

The Black Hole Register ๐Ÿ”—

The black hole register is a place from which nothing returns.

If we run the command, doulbe quote underscore d motion, then Vim deletes the specified text without saving a copy of it.

The System Clipboard and Selection Registers ๐Ÿ”—

If we use the cut or copy command to capture text in an external application, then we can paste it inside Vim using quote plus p command or Control r then plus from the Insert mode.

Conversely, if we prefix Vim’s yank or delete commands with colon quote plus, the specified text will be captured in the system clipboard.

The X11 windowing system has a second kind of clipboard called the primary.

This represents the most recently selected text, and we can use the middle mouse button to paste from it.

Vim’s quotestar register maps to the primary clipboard and is addressed by the star symbol.

In Windows and Mac OS X, there is no primary clipboard, so we can use the quote plus and quote star registers interchangeably.

Tip 62. Replace a Visual Selection with a Register ๐Ÿ”—

When used from Visual mode, Vim’s put command has some unusual qualities.

When we use Vim’s p command in Visual mode, it does both:

it gets the contents of the unnamed register, and it sets the contents of the unnamed register.

The m character command sets a mark, and the backtick character command jumps to the mark.

Tip 63. Paste from a Register ๐Ÿ”—

The Normal mode put command can behave differently, depending on the nature of the text that is being inserted.

It can be helpful to adopt different strategies, depending on whether we’re pasting a line-wise or a character-wise region of text.

Pasting Chracter-wise Regions ๐Ÿ”—

Pasting Line-Wise Regions ๐Ÿ”—

Tip 64. Interact with the System Clipboard ๐Ÿ”—

Besides Vim’s built-in put commands, we can sometimes use the system paste command. However, using this can occasionally produce unexpected results when running Vim inside a terminal.

11. Macros ๐Ÿ”—

Tip 65. Record and Execute a Macro ๐Ÿ”—

Macros allow us to record a sequence of changes and then play them back. This tip shows how.

Tip 66. Normalize, Strike, Abort ๐Ÿ”—

Executing a macro can sometimes produce unexpected results, but we can achieve better consistency if we follow a handful of best practices.

Tip 67. Play Back with a Count ๐Ÿ”—

The Dot Formula can be an efficient editing strategy for a small number of repeats, but it can’t be executed with a count. Overcome this limitation by recording a cheap one-off macro and playing it back with a count.

Tip 68. Repeat a Change on Contiguous Lines ๐Ÿ”—

We can make light work out of repeating the same set of changes on a range of lines by recording a macro and then playing it back on each line.

Tip 69. Append Commands to a Macro ๐Ÿ”—

Sometimes we miss a vital step when we record a macro. There’s no need to re-record the whole thing from scratch.

Instead, we can tack extra commands onto the end of an existing macro.

Tip 70. Act Upon a Collection of Files ๐Ÿ”—

So far, we’ve stuck to tasks that were repeated in the same file, but we can play back a macro across a collection of files.

Tip 71. Evaluate an Iterator to Number Items in a List ๐Ÿ”—

Being able to insert a value that changes for each execution of a macro can be useful.

Tip 72. Edit the Contents of a Macros ๐Ÿ”—

12. Matching Patterns and Literals ๐Ÿ”—

Tip 73. Tune the Case Sensitivity of Search Patterns ๐Ÿ”—

Tip 74. User the backslash v Pattern Switch for Regex Searches ๐Ÿ”—

Tip 75. Use the backslash uppercase V Literal Switch for Verbatim Searches ๐Ÿ”—

Tip 76. Use Parentheses to Capture Submatches ๐Ÿ”—

Tip 77. Stake the Boundaries of a Word ๐Ÿ”—

Tip 78. Stake the Boundaries of a Match ๐Ÿ”—

Tip 79. Escape Problem Characters ๐Ÿ”—

13. Search ๐Ÿ”—

Tip 80. Meet the Search Command ๐Ÿ”—

Tip 81. Highlight Search Matches ๐Ÿ”—

Tip 82. Preview the First Match Before Execution ๐Ÿ”—

Tip 83. Offset the Cursor to the End of a Search Match ๐Ÿ”—

Tip 84. Operate on a Complete Search Match ๐Ÿ”—

Tip 85. Create Complex Patterns by Iterating upon Search History ๐Ÿ”—

Tip 86. Count the Matches for the Current Pattern ๐Ÿ”—

Tip 87. Search for the Current Visual Selection ๐Ÿ”—

14. Substitution ๐Ÿ”—

Tip 88. Meet the Substitute Command ๐Ÿ”—

Tip 89. Find and Replace Every Match in a File ๐Ÿ”—

Tip 90. Eyeball Each Match in a File ๐Ÿ”—

Tip 91. Reuse the Last Search Pattern ๐Ÿ”—

Tip 92. Replace with the Contents of a Register ๐Ÿ”—

Tip 93. Repeat the Previous Substitute Command ๐Ÿ”—

Tip 94. Rearrange CSV Fields Using Submatches ๐Ÿ”—

Tip 95. Perform Arithmetic on the Replacement ๐Ÿ”—

Tip 96. Swap Two or More Words ๐Ÿ”—

Tip 97. Find and Replace Across Multiple Files ๐Ÿ”—

15. Global Commands ๐Ÿ”—

Tip 98. Meet the Global Command ๐Ÿ”—

Tip 99. Delete Lines Containing a Pattern ๐Ÿ”—

Tip 100. Collect TODO Items in a Register ๐Ÿ”—

Tip 101. Alphabetize the Properties of Each Rule in a CSS File ๐Ÿ”—

16. Index and Navigate Source Code with ctags ๐Ÿ”—

Tip 102. Meet ctags ๐Ÿ”—

Tip 103. Configure Vim to work with ctags ๐Ÿ”—

Tip 104. Navigate Keyword Definitions with Vim’s Tag Navigation Commands ๐Ÿ”—

17. Compile Code and Navigate Errors with the Quickfix List ๐Ÿ”—

Tip 105. Compile Code Without Leaving Vim ๐Ÿ”—

Tip 106. Browse the Quickfix List ๐Ÿ”—

Tip 107. Recall Results from a Previous Quickfix List ๐Ÿ”—

Tip 108. Customize the External Compiler ๐Ÿ”—

18. Search Project-Wide with grep, vimgrep, and Others ๐Ÿ”—

Tip 109. Call grep Without Leaving Vim ๐Ÿ”—

Tip 110. Customize the grep Program ๐Ÿ”—

Tip 111. Grep with Vim’s Internal Search Engine ๐Ÿ”—

19. Dial X for Autocompletion ๐Ÿ”—

Tip 112. Meet Vim’s Keyword Autocompletion ๐Ÿ”—

Tip 113. Work with the Autocomplete Pop-Up Menu ๐Ÿ”—

Tip 114. Understand the Source of Keywords ๐Ÿ”—

Tip 115. Autocomplete Words from the Dictionary ๐Ÿ”—

Tip 116. Autocomplete Entire Lines ๐Ÿ”—

Tip 117. Autocomplete Sequences of Words ๐Ÿ”—

Tip 118. Autocomplete Filenames ๐Ÿ”—

Tip 119. Autocomplete with Context Awareness ๐Ÿ”—

20. Find and Fix Typos with Vim’s Spell Checker ๐Ÿ”—

Tip 120. Spell Check Your Work ๐Ÿ”—

Tip 121. Use Alternate Spelling Dictionaries ๐Ÿ”—

Tip 122. Add Words to the Spell File ๐Ÿ”—

Tip 123. Fix Spelling Errors from Insert Mode ๐Ÿ”—