Gems of Vim: Tips & Tricks

Gems of Vim: Tips & Tricks

“A good programmable editor will grow with you for the rest of your career, multiplying your effectiveness as a developer.” - Avdi Grimm

Before Vim

Before I tried Vim, my code editor of choice was Sublime Text. Sublime is clean, fast, and has features that I still love. I was new to programming, and I needed an editor that worked well without a lot of effort.

Still, I liked the idea of Vim. With Vim, I wouldn’t need to leave the command line to use it, and Vim has extensive customization options and some crazy awesome features. Plus, I liked the idea of Vim because it looked cool.

When I set out to learn Vim, I quickly hit some tough learning barriers, as does any developer new to Vim.

Original Setbacks to Vim

As much as I tried to use Vim for all of my editing, I kept defaulting back to Sublime text. Most of the issues were common newbie problems:

No Mouse

I couldn’t use a mouse, and that made navigating hard.

Remedies: Life After Mice

  • Move to the top of a file: gg

  • Move to the bottom of a file: G

  • Move to a specific line: First type the colon key, :. Then type the line number* and press enter. To go to line 6, it will look like :6.

With these three commands, I could move around much easier. There is also j and k, and even multiple lined versions like 2j, but I prefer the colon command the most.

*To Display line numbers, type :set number. To get rid of the line numbers, press :set nonumber.

Highlighting Text

Without a mouse, I didn’t know how to highlight sections of text, and this was a big downer for me. Fortunately Vim has an awesome mode called Visual Mode. Visual mode is entered 3 ways:

Character-wise Visual Mode

Character-wise Visual Mode is entered by pressing v. From there, you navigate with hjkl to select what you’d like. You can copy your highlighted selection with y, delete it with d, or change it (erase it and enter insert mode) with c.

Line-wise Visual Mode

You enter Line-wise Visual Mode by pressing V. This highlights lines at a time, and you can select more by going up & down with jk. Changes are made the same way as with Character-wise Visual Mode.

The Almighty Block-wise Visual Mode

Block-wise Visual Mode was one of the first features of Vim that made me audibly say “WOW”. To enter Block-wise VM(or Column-wise as I think about it), press control & v at the same time, ctrl+v. Once you enter VM, you can select columns, up & down, left & right. What is really cool is that if you select your columns & press A, you can add to the end of each line all at once. First you see it making changes on the original line, but once you exit back into normal mode, the changes will take effect on each line.

Sublime’s Simultaneous Search & Edit

I loved Sublime Text’s Search & Edit, and that’s something Vim missed. There’s not a straight equivalent of it in Vim, but here are some commands that make me miss it less.

Search & Replace

Vim’s Search & Replace is a colon command, also called Ex Commands. To replace a specific word throughout your file you will start with :%s. The s stands for substitution, and the % says to search over the entire file. Next, section it with / and add the word you want to replace. It will look like :%s/word. Follow that with / and the replacement, making the command now :%s/word/replacement. Next, you’ll end it with /g. This stands for global, meaning that it makes changes globally or over the entire file. Without it, it will only replace the next word after your cursor. The full command looks like:

:%s/word/replacement/g

You can learn more about search and replace here.

The Process of Learning Vim

My switch-over to Vim was very gradual, a process of about a month. I went on the search for blogs & tutorials on the best ways to use Vim. Everyday, when I wanted to take a break at work, I’d open up a command line, type in vim nothingness, and write away in Insert Mode whatever came to my head. It was nonsense, but it helped me get used to the feeling of Vim, and it also made it easier to put my thoughts into writing (added bonus). After I was done, I’d exit without saving, :q!, and the writing would disappear into nothingness. After 3 weeks, I noticed myself using Vim more for my daily work, and after another 2 weeks, I felt much more comfortable with Vim and any other tool. Now when I jump into Sublime or Textmate, I catch myself using Vim commands.

Along the way, I found some great resources. If you’re new to Vim, check these out. They are very useful in getting down the basics:

  • Vim Adventures - An online game based on VIM’s keyboard shortcuts
  • Vim-Tutor - This usually ships with Vim and can be accessed in command line with vimtutor.

What finally sold me on Vim was a book called Practical Vim. This book was the turning point for me, and I highly recommend reading it because it will completely change your mindset on how to use Vim. I am only 60 pages into this 300 page book, and I already feel like a Practical Vim whiz.

Features That Sold Me To Vim

Dot Command

Practical Vim starts off with one very powerful command: the Dot Command, or simply .. Dot allows you to repeat the previous command. Here’s a good example. Say I have the sentence, “When I eat, I eat like a pig.” Obviously that’s not true for me, but it may be true for you, and I can’t have that rumor spreading around.

So, I take the sentence and hover over the first “I” and rewrite it to “you” with the command cwyou, or Change-Word and then “you”. I escape to normal mode, move over to the next “I”, and all I have to do is press .. Voila, the change is made and rumors are spread. It’s very powerful. What’s even more powerful is that it helped me understand the point of the different modes. I started to enter and exit Insert Mode more often; I was learning how to make modular changes.

The Different Modes

Normal Mode

Normal mode is home base in my eyes. It’s the mode you begin with, and the mode you go back to any time you hit escape. When I am in Normal Mode, I am taking the next steps to enter another mode to make changes. It’s where the . command lives. It’s also the mode that makes navigating easy.

If you’re navigating in Insert Mode, consider getting used to escaping out of it back into normal mode, and it’ll be much easier.

Visual Mode

We discussed Visual Mode earlier, but it’s worth mentioning again because with Vim, it’s good to think in modes. I’m navigating? Normal Mode. I’m making changes to a selection? Visual Mode. I quickly want to move whole lines or move to another line far away? Ex mode, as we’ll see below.

Replace Mode

Replace mode can be very useful at times. It’s entered into from Normal Mode in two ways:

  1. With r to replace one character and return to normal mode.
  2. With R to stay in Replace Mode until you escape.

Replace mode deletes the highlighted character and replaces it with the character that you type.

Ex Commands

Ex commands(colon commands) have the power of making changes anywhere on the file despite the location of your cursor. One I’ve already mentioned is moving to a specific line. Here are some other common ex commands.

Copy Command

You can copy lines to another location easily with :t, which comes from “copy To”. Say you want to move line 6 to below line 18, the command would be :6t18. The line to be copied is stated before the ‘t’, and the location to move it is stated after. You can copy several lines to a location at once. Say we want to move lines 6 through 11 to below 18, then you would use the command :6,11t18 where 6 is the beginning line and 11 is the end line.

Relative Location

Say you want to move the lines to the line right below the cursor. To do so, replace the location number with . So the command above would instead be :6,11t..

Move Command

The Move command moves whole lines to a new location and works exactly like the copy command, only replacing t with m.

The O command

The O command is nifty and very useful with two variations:

  1. o will create a new line below and put you into insert mode on that line.
  2. O will create a new line above and put you into insert mode on that line.

Final Thoughts - Keep It Fun

If you’re new to programming, I’d consider holding off learning Vim until you’re comfortable with a language. Starting off with Vim can add significant frustration to what may already be a frustrating time of getting down programming concepts. I started with Textmate, which I loved, and moved on to Sublime Text which I loved even more. I recommend either, and you can decide which one you favor more.

Don’t give in to the pressure of the hardcore Vim or Emacs enthusiasts; it’s all about what works for you. Enjoy the process of learning Vim because it should be fun. If your experience is full of frustration rather than “WOW” moments, give it a break and use what works. You can give it another try later. In fact, the quote from Avdi at the beginning is from a blog called “Let’s stop telling programming newbies to learn Vim (or Emacs)”. He hits on points from another great blog by Yehuda Katz called “Everyone Who Tried to Convince Me to use Vim was Wrong”.

Enjoy the learning experience and happy coding!

Follow Up - New Vim Blog

I’ve learned a lot more about Vim and I have more to share in a new blog: More Gems of Vim - Tabs, Mappings, and More Tips. Check it out!

alex

About Alex Kitchens

I am a principal software engineer at Stitch Fix working remotely from Amarillo, TX. Talk to me about anything Rails, Ruby, Rust, or coffee.