October 13, 2016

Navigating In Vim II | Your First Lesson in Vim

12:00 Posted by Durga swaroop Perla , , No comments

This is the fourth article in the series titled, "Your First Lesson In Vim". These articles are written with a goal of helping out new Vim users by teaching the awesomeness of the Vim editor and there by extending the Vim community. Vim though quite powerful, has a bad rep for being hard to learn and hard to get started with. So, even when someone is interested in learning about Vim, that infamous learning curve seem to be scaring them off. This series is going to put an end to all of that.

Vim Logo

In the last article Navigating in Vim I, we have seen a lot of Vim motions. Most of these fall under the category of word-motions (:help word-motions). We will learn some more motions in this article. And in case you still haven't tried Vim Adventures you should do it. It will help you a lot with getting the hang of Vim motions and getting around in vim.

Here are the list of Vim motions for this article.

Motion What it does?
0 Go to the STARTING of the CURRENT LINE
^ Go to the FIRST NBC* of the CURRENT LINE
- Go to the FIRST NBC* of the PREVIOUS LINE
+ Go to the FIRST NBC* of the NEXT LINE
$ Go to the END of the CURRENT LINE
g_ Go to the LAST NBC* of the CURRENT LINE
f{char} Find a character FORWARD in the current line (Usage: to go to first occurance of c, you type fc)
F{char} Find a character BACKWARD in the current line (Usage: to go to first occurance of c to the left of the cursor, you type Fc)
t{char} Like f but places the cursor before the character (Mnemonic : t - till)
T{char} Like T but places the cursor after the character
gg Move the cursor to the first line (compare this with H)
G Move the cursor to the last line (compare this with L)

* NBC - Non Blank Character

Line motions Vim picture

These motions let you move very fast between lines. You can go to any character you want on the current line with just 2 or at 3 keys, which is insanely fast compared to any other text editor. The last two motions (gg, G) are super useful and are certainly two of my most used commands.

Now we have one final set of motions to learn called Text Object motions (:help object-motions). Text objects is an important concept in Vim and we will cover that in depth in a future article. For now let's look at these motions.

Text Object Motions

Motion What it does?
( Go to the beginning of the PREVIOUS sentence
) Go to the beginning of the NEXT sentence
{ Go one paragraph BACKWARD
} Go one paragraph FORWARD

These four motions are very useful too. Especially if you're a programmer, the { and } will make navigating the code base a breeze.

And with that, we have covered all the basic Vim motions for you to get started. There is just one more important thing you need to know in conjunction with Motions. I haven't told you about this till now because I wanted you to get a full grasp of Vim motions before I explain this. Anyway, here it goes ..

Every Vim Motion takes a count before it

That's it. It might seem simple and it is simple, but its usefulness is just immeasurable.

Let's say you have to move eight lines down. To go eight lines down you don't have to frantically type jjjjjjjj. Just simply type *8j*. Similarly 4k to go four lines above, 6w to go to the sixth word from the cursor and so on. This is just such a useful feature that quite literally Sky is the limit for what you can do with this. Want to go to the second e after the cursor? Try 2fe and your cursor lands directly on e. Similarly to go to the ending of the 5th line below just do 5$ and B.A.M!

This opens up a whole new world of combinations for you to use and I hope you will make use of all of them. With these motions you can move to any place you want in the file with minimal number of keystrokes and your ultimate aim should be to accomplish everything with the minimum possible number of keystrokes. Be a Vim Ninja and conquer the world!

Vim ninja image

Well, That is all for this article folks. Will see you again in the next one. Until then, Keep practicing and Happy Vimming!

← Prev Next →




For more Vim stuff : vimtutorials.org

Follow FreBlogg on Facebook for more interesting articles.

Attributions:

Vim Logo - Vim Replacement Icon http://wolfrosch.com/works/goodies/vim (CC BY-NC-ND 3.0)

Vim Ninja image - https://goo.gl/QgTrsY (Originally from Practical Vim by Drew Neal)

0 comments:

Post a Comment

Please Enter your comment here......