[go: up one dir, main page]

DEV Community

Camilo Payan
Camilo Payan

Posted on • Originally published at camilopayan.com

Moving in Vim

Moving in Vim

Everyone who’s tried Vim learns about the wonky hjkl keys to move around a file. These usually make a new user feel one of the only two emotions we need: anger and confusion.

A scene from the sitcom The Good Place where a character says

What if I told you that there was a way to make moving around your code feel as smooth as a swan dive and as fast as thought, while making editing a cinch? That’s where Vim movements come in.

Vim movements are commands that move the cursor in some direction. The hjkl keys are all examples of movement commands. In their case, movements that go left, down, up, and right. Vim also has other motions built around text objects as well as the direction you want to go in. If you’d like to go to your next code block, instead of mashing the j key a few times, you can hit } to get to the next empty line, landing you right before your next block of code (you are spacing out your code for readability, right?). Swapping out several hits on the j button with a single } is both a time saver and a finger saver. It helps you get to where you need to be in the code as quickly as you can think.

You can learn about all the available Vim motions in the Vim help file motion.txt accessible by running :help motion.txt in command mode.

If we were only eliminating keystrokes, it would already be worth your time to learn. But Vim motions are composable. This means they can combine movements with Vim’s editing actions to make changes to your file.

Let's go back to our example about moving to a new code block. What if you wanted to delete everything up until your next code block? We already know that moving to the next blank line is }, so we can combine that with the delete command d to create d}. This will delete every line from where our cursor is up until the next blank line.

Now you aren’t only moving faster, you’re editing your file faster, too!

Motions and commands aren't only Vim and Neovim. Vim emulation layers like the IDEAVim plugin for JetBrains IDEs also have motions. The support for all motions might not always be there. My last experience with PHPStorm, for example, meant losing out on some motions. I missed those motions, and I went back to using Vim.

Vim motions and commands in normal mode are the language of Vim. When you learn to speak it, you’ll find that you can jump around files quicker than you ever thought possible.

Top comments (4)

Collapse
 
leojpod profile image
leojpod

after some good training getting around in vim, the time to put your skills to the test will come. Go there: vimgolf.com/ :D

Collapse
 
bgadrian profile image
Adrian B.G.

Funny that you tagged "productivity" in a Vim article

Collapse
 
camilo profile image
Camilo Payan

How so?

Collapse
 
leojpod profile image
leojpod

I've never been as productive as I am now with a good vim setup.