Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Vim

You're reading from   Mastering Vim Build a software development environment with Vim and Neovim

Arrow left icon
Product type Paperback
Published in Nov 2018
Publisher Packt
ISBN-13 9781789341096
Length 330 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Ruslan Osipov Ruslan Osipov
Author Profile Icon Ruslan Osipov
Ruslan Osipov
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Getting Started FREE CHAPTER 2. Advanced Editing and Navigation 3. Follow the Leader - Plugin Management 4. Understanding the Text 5. Build, Test, and Execute 6. Refactoring Code with Regex and Macros 7. Making Vim Your Own 8. Transcending the Mundane with Vimscript 9. Neovim 10. Where to Go from Here 11. Other Books You May Enjoy

Moving around: talk to your editor

Vim allows you to navigate content a lot more efficiently than most conventional editors. Let's start with the basics.

You can move your cursor around character by character by using arrow keys or letters h, j, k, and l. This is the least efficient and the most precise way to move:

Key Alternative key Action
h Left arrow Move cursor left
j Down arrow Move cursor down
k Up arrow Move cursor up
l Right arrow Move cursor right

The following diagram is a visual representation which might be a little easier on the eyes:

Vi (Vim's predecessor) was created on an old ADM-3A terminal, which didn't have arrow keys. Keys h, j, k, and l were used as arrows.




Image by Chris Jacobs, Wikipedia (CC BY-SA 3.0)

Try it! There's a lot of value to getting used to hjkl for movement: your hands stay on the home row of your keyboard. This way you don't have to move your hands and it helps you stay in the flow. Furthermore, many applications treat hjkl as arrow keys—you'd be surprised how many tools respond to these.

Now you might be inclined to hit directional keys multiple times to get to a desired position, but there's a better way! You can prefix every command by a number, which would repeat the command that number of times. For example, hitting 5j will move the cursor five lines down, while hitting 14l will move the cursor 14 characters to the left. This works with most commands you encounter in this book.

Calculating the exact number of characters you would like to move is pretty hard (and nobody wants to do it), so there's a way to move by words. Use w to move to the beginning of the next word, and use e to get to the end of the closest word. To move backwards to the beginning of the word, hit b.

You can also capitalize these letters to treat everything but a white space as a word! This allows you to differentiate between the kind of things you'd like to traverse.

Vim has two kinds of word objects: referred to as lowercase "word" and uppercase "WORD". In Vim world, word is a sequence of letters, digits, and underscores separated by white space. WORD is a sequence of any non-blank characters separated by white space.

Let's take the following line of code from our example:

Notice the cursor position, it's hovering over the first character of add_animal.

Hitting w will move the cursor to beginning of the word add_animal, while hitting W will take you to the beginning of animal. Capitalized W, E, and B will treat any characters bundled together and separated by a space as their own words. This can be seen in the following table:

Key Action
w Move forward by word
e Move forward until the end of the word
W Move forward by WORD
E Move forward until the end of the WORD
b Move backwards to the beginning of the word
B Move backwards to the beginning of the WORD

The following screenshot shows more examples of how each command behaves:

Key Initial cursor position Resulting cursor position
w
e
b
W
E
B

Combine the movements shown with the directional movements you learned earlier to move in fewer keystrokes!

It's also really useful to move in paragraphs. Everything separated by at least two new lines is considered a paragraph, which also means each code block is a paragraph, as can be seen in the following example:

The functions add_animal and main are two separate paragraphs. Use a closing curly brace } to move forward, and an opening curly brace { to move backwards as detailed in the following table:

Command Action
{ Move back by one paragraph
} Move forward by one paragraph

Don't forget to combine these two with numbers if you need to move by more than one paragraph.

There are more ways to move around, but these are the most important basics. We'll be covering more complex ways to navigate in chapter 2, Advanced Editing and Navigation.

You have been reading a chapter from
Mastering Vim
Published in: Nov 2018
Publisher: Packt
ISBN-13: 9781789341096
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime