TransWikia.com

In Vim How to jump to last, second last or some n'th character from the end in current line

Vi and Vim Asked by CodeTalker on August 31, 2021

Suppose I have a string like

takeThisAsAFunction(someParameter, someNestedFn(furtherNesting(foo, oneMoreNesting())), someText);
      ^                                                                              !  

Lets’s say I have cursor at location ^, and I want to go to second last ), pointed by location !, how can I jump directly to that.
Currently I do

$2F)

Is there any better way?

One Answer

The "problem" with 2F( is the 2: you need to count, which is easy in some case, but can be troublesome most of the time.

I see several brainless ways of doing what you intend:

  • %: %h%l%h%l...

the percent sign jumps to the symbol that matches the one under the cursor (e.g. closing/opening parenthesis), or the first closing symbol after your cursor. Use l and h to go a level inner and get closer to your search

  • /: /function_name<cr>%

As pointed by DBK's comment, same idea as above, if you know the name of the function

  • f: f);;;...

jump to the next ), then use ; to repeat. , goes backwards.

  • /: /(,

Using a unique sequence in search is a fast way to jump anywhere in a file

  • B: $Bj

B jumps to the end of the previous WORD (i.e., sequence of characters delimited by white spaces), that fits your example perfectly.

  • W: WWWj

Same as above, but jumping forwards by WORD instead of backwards.

Final thoughts:

Always try to get as close as you can and avoid using h and l, but don't force yourself not to use them. h and l are perfectly OK when you're right next to the character you're aiming at.

Correct answer by Biggybi on August 31, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP