Beginner Vim user here.

I already learned about automating deleting with commands, which happens this way:

[number]d[j/k/h/l] [OR] d[number][j/k/h/l]

Where number stands for the amount of the action repeated.

  • Can I use it for simply moving the cursor? Like 5l - or 3w to jump words.

  • Is it good practice at all? Or can/should I find better ways of automatic cursor movement in Vim?

  • Can I use it for simply moving the cursor? Like 5l - or 3w to jump words. If you try it you'll see that 5l and 3w and Xany motion works :) Is it a good practice? Whatever floats your boat... But a good practice is to use the right motion for the right destination: if you want to go far, use /, ? , n or N, to the beginning or end of line: 0, ^ , $ just some words back and forth w, b, e, ge or even f, F, t, T, ;, , (I'm pretty sure there is a question here about that but I can't find it). automatic cursor movement what do you mean by automatic? – statox Dec 6 '16 at 10:00
  • 1
    Finally Finally :h motion.txt should be a good read for you, especially :h left-right-motions and :h up-down-motions – statox Dec 6 '16 at 10:00
  • @statox thank you very much! By "automatic" I meant exactly what you described. – Zoltán Schmidt Dec 6 '16 at 11:43
  • I didn't see 'in command mode' in the title of your question... Thus my comment might be really off topic. Are you talking about motions to edit a command after you typed : or are you talking about navigation in the buffer in normal mode? – statox Dec 6 '16 at 12:54
  • 1
    @ZoltánSchmidt Actually you were not :-) :h command-mode and :h Normal-mode are the same topic the mode simply has two names that's why I was confused – statox Dec 6 '16 at 20:05

To reword my comment:

Can I use it for simply moving the cursor? Like 5l - or 3w to jump words.

Yes. Every motion command can be prefixed by a count.

Is it good practice at all?

There can be exceptions/personal preferences mitigating what will follow but the general rule is to use the motion corresponding to the amplitude of the move you want to make.

Vim has an enormous amount of motion commands which allows you to travel in your buffer at different speeds: you can have motions to go from the beginning of the buffer to the end or from one character to the next one. I will not make an explicit list here because it's a lot of work and this work has already been done very well in :h motion.txt. Still here are some common example:

For up-down motions:

  • Search commands allow you to travel your buffer pretty fast: / and ? allow you to search for occurrences of a pattern and n and N repeat this search forward or backward

  • Absolute commands like gg, G, [count]G allows you to move to first line, last line or line [count] in your buffer. So does :[count]

  • For large up-down motions marks (like 'a or '') can also be used and % is useful to go to the matching character (like matching ) when cursor is on ()

  • Text objects can also be used for that: use { and } to switch to the beginning or end of a paragraph.

For motions within a line you also have a lot of options:

  • Go to beginning, first character or end of the line with 0, ^ and $

  • Target a character with f or t and backward with F and T. Repeat these motions with ; or ,

  • Move word wise with b, w, e, ge

  • Move from one character to another with h and l. *(Yeah I do recommend to use h and l. See my note at the end)

I think that it is important to make a note about h, j, k and l:

Some Vimmers will tell you to never use them because it's slow and repetitive and yada yada... The whole point of Vim is to use the available motions efficiently if you need to move 3 words forward yeah that's not a good idea to press 20 times on l when you could use 3w. But when you need a precise motion of two or three letter h and l are the way to go, just like j and k for moving two or three lines up or down.

That's why I believe that plugins like vim hardmode are stupid not the correct solution to a problem which might not even exist.

In conclusion read :h motion.txt, grok it, get used to the motions commands one by one. When you use a motion which feel inefficient, go read the page again and find the correct motion to use.

As I often did on this site I'd also recommend to read the amazing practical vim by Drew Neil which has a very good chapter on motions. And vimcasts probably also has a good reference about that (I'll let you find it ;-) )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.