The world’s leading publication for data science, AI, and ML professionals.

Supercharge your Vim Skills

Vi is a text editor that in the hands of a skilled user, can enable blazing fast edits closer to the speed of thought - much faster than…

Supercharge your Editing Speed

8 Vim tips to edit your files faster.

Vim is a text editor that in the hands of a skilled user, can enable blazing fast edits closer to the speed of thought – much faster than what’s usually achievable with a traditional text editor.

For everything we do on the computer, there is a time delay between our intention to perform an action and observing that action being completed. It simply takes time to edit text, and if this time delay is too long, it can hinder our focus. To combat this problem, learning some fancy Vim maneuvers can stamp our edits from brain-to-screen that much faster.

Let’s look at editing an example.

url = "https://www.google.com"

Say we wanted to change our URL to show localhost instead.

In a traditional text editor, we might do something like drag our cursor over the quote enclosed URL, and start typing the new URL. It takes a bit of time move our hand to the mouse, to precisely highlight the quote enclosed section (Fitt’s law tells us that this is a hard thing to do quickly since it’s such a precise movement), and to move our hand back to the keyboard to begin typing again.

In Vim, we could move our cursor anywhere within the quotes, type in ci" (which will delete everything within the enclosed quotes and enter insert mode), and immediately begin typing our new URL.

Notice that Vim saves us a good amount of time versus using the traditional text editor for this task. We waste a lot of time transitioning our hands between keyboard and mouse in the traditional text editor – Vim’s style of maintaining our hands on the keyboard shaves off some delay and hopefully let’s us print some more brain dump into the editor before we forget what we’re looking at again.

Now that you’ve seen a practical example of how Vim can speed up your editing, I’ll give you my most useful tips for squeezing out all of that productivity from Vim.


1. Get started learning the basics.

If you’re brand new to Vim or could get refamiliarized with the basics, you can run vimtutor on the command line to get started.


2. Exit insert mode better

You can exit insert mode with Ctrl-C. The escape key can do the same thing, but why do so much reaching?


3. Upgrade vimrc

The file ~/.vimrc storages configuration settings for every time we open Vim.

These are some sensible settings that most people would benefit from enabling.

" Enable mouse usage
set mouse=a
" Turn on line numbers
set number
" Enable syntax highlighting
syntax on

These are some shortcut settings that I personally use, but are useful examples for you to see how to make your own shortcuts to add in ~/.vimrc.

" Map Ctrl-C, Ctrl-C -> force quit
nmap <C-C><C-C> :q!
" Map w, w -> force write
nmap ww :w!
" Map Ctrl-W, Ctrl-W -> force write and quit
nmap <C-W><C-W> :wq!

Tweak these settings as you feel out what you like and push them up to a Github repo. Since Vim is ubiquitous and exists as an option in nearly any Linux server, you can clone your settings into any new machine you’re using and get started in that same familiar text editing environment.


3. Jump around

It can be tedious scrolling through a long file with only arrow keys. Learn these commands and you won’t have to spend so much time scrolling.

While in normal mode:

gg – jump to the top of the file

G – jump to the bottom of the file

<line-num>G – jump to , for example 7G jumps to line 7

From the command line:

vim <filename> +<line-num> – open the file and jump to


4. Yank (copy), delete (cut), and paste

Something to remember: Yank commands have the letter y involved, delete commands have the letter d involved, and paste commands have the letter d involved.

We’ll have to use visual mode (which we can enter using v or Shift-V in normal mode) to select the text we want to edit. With text highlighted in visual mode:

y – yank

d – delete

Then we’ll get put back in normal mode where we can type p to paste.

Some other useful normal mode commands:

yy – yank the current line

dd – delete and yank the current line

p – paste after cursor or line

P – paste before cursor or line


5. Undo and redo

Everyone makes mistakes.

In normal mode, undo edits with u and redo edits with Ctrl-R.


6. Search

To search for a string in the current file, type /<SEARCH_TERM> in normal mode. You can even use regex in <SEARCH_TERM>.

Hit enter and the cursor will jump to the first occurrence of the query.

To cycle through all occurrences, type n to go forward and N to go backward.


7. Compound commands

In the very beginning of the article, we demoed a compound command. Compound commands can be very powerful, and once you get them down, you will blaze through edits with incredible precision.

To understand compound commands in Vim, you need some context:

  • Actions are categorized by verbs like change and delete.
  • Actions can have modifiers like in and around.
  • Text is categorized by words, lines, sentences, and paragraphs.

This lends itself to some very cool and convenient compound commands – commands which are composable and easily repeatable.

For example, the compound command used in the beginning of the article was ci" ("change in quotes") which deleted everything in quotes and put us in insert mode.

c tells Vim our desired action – change – which will leave us in insert mode when the command is finished.

i" tells Vim our desired action modifier – in quotes – which will perform our desired action inside a pair of matching quotes, and leave the quotes intact.

It’s intuitive to figure out once you’ve tried a few examples so here are some to play around with.

While in normal mode:

cip ("change in paragraph")— delete the paragraph the cursor is on and enter insert mode

ciw – delete the word the cursor is on and enter insert mode

cw – delete the word on and after the cursor and enter insert mode


diw ("delete in word")— delete the word the cursor is on

dis – delete the sentence the cursor is on


ctX ("change to X")— delete up to the next occurrence of ‘X’ and enter insert mode

dtZ – delete up to the next occurrence of ‘Z’


da) ("delete around parentheses")— delete text within and including surrounding parentheses

ci] – delete text in surrounding brackets and enter insert mode


8. Repeat previous edit

All edits begin when you enter insert mode and end when you switch back to normal mode. Whether you manually initiate an edit, or a compound command does it for you, these discrete edits are repeatable.

Just type . from normal mode to repeat the last edit.


Conclusion

There is so much Vim can do to speed up our text editing. And there is so much more I haven’t mentioned here.

So get out there and start using Vim to edit your files! The only way is to practice and keep using Vim until the commands are second nature. Think about all the files you’ll edit in the future. Now think about all the time you’ll save editing those files in the future.

Let me know some of your favorite Vim tips and tricks in the comments! I look forward to hearing what you think.


Related Articles