3

I am looking for a simple few key command which would move till the last occurrence of a character in the line. Is there anything already existing?

The alternative is to jump to the next specified character and repeat the jump as many times as you need. It is not terrible; I am just curious whether there is there is something built into vim which could speed this up.

A use case could be to strip the absolute path only to the last directory.

3

Assuming that &virtualedit is all or onemore it's $lF/ (i.e. get one character past the end of the string, in case it ends in a searching character, and find char backward).

More robust variant (not to depend on &virtualedit) is ACtrl-\Ctrl-OT/Esc.

to jump to the next specified character and repeat the jump as many times as you need

[count]f{char}

Also to repeat by pressing semicolon (;)

6
  • Thanks! I like the first one. Could you explain how the second one works? <kbd>A</kbd> would put us into insert mode and I am not sure what <kbd>Ctrl-\</kbd> does. :h Ctrl-\ only gives me a command where <kbd>e</kbd> is followed. – Horror Vacui Apr 10 '20 at 15:03
  • 1
    @HorrorVacui To search help topic for insert mode you should type: :h i_ctrl-\_ctrl-o The key combination is primarily for scripts/mappings to make sure we get past end of line no matter what is the value of &virtualedit – Matt Apr 10 '20 at 15:40
  • thanks! Just leaving here my understanding of the second suggestion: it starts in normal mode, uses the insert mode and returns into the normal mode. It is a combination of commands which moves the cursor to the desired location, but it is not a "move" what one can use to define the scope of a command, but one can use marks to do that. – Horror Vacui Apr 12 '20 at 0:05
  • Is there any downsides of using the visual mode for tasks like that? – Horror Vacui Apr 12 '20 at 0:06
  • @HorrorVacui I can't get you. This is insert mode, not visual mode. – Matt Apr 12 '20 at 3:25
1

Perhaps i'm just a vim noob and don't quite understand the previous answer about &virtualedit. in any case, my solution is:

  1. Just to end of string with $
  2. Search backwards to find first instance of character with F (note: capital f)
  3. Type character you want to search for

So for example, to find the last occurance of / in a url:

https://vi.stackexchange.com/questions/24528/simple-keycombination-to-move-to-the-last-occurance-of-a-character-in-a-line

I would type $ F /

$ - moves my cursor to the end of the line regardless of where i'm starting from

F - starts to search backwards (works the same way as f but in the reverse direction)

/ - is the character we're searching backwards on

1
  • This is almost the same what I shown above, except it fails for corner cases, such as http://www.example.com/ – Matt Oct 20 '20 at 5:23

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.