Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim - Delete til last occurrence of character in line

Tags:

vim

I'm trying to figure out how to dt or df the last occurrence of a character in a string.

For example, let's say I have the following line:

foo not.relevant.text.bar

If I f df. I expectedly get foo relevant.text.bar but I would like to get foo bar. Using f 3df. is not an option as I don't know how many of that character will be in the string. Additionally, I may want to get foo .bar (f 3dt.), or if the line ends with a dot, I may want to get foo .. I want to always find the last one regardless of how many there are.

Is this possible without a regex? I suppose I could use a regex but I was hoping there was a simple vim command that I'm missing. I find myself trying to do something like this often.

like image 212
Rob Avatar asked Mar 06 '13 13:03

Rob


4 Answers

You can use my JumpToLastOccurrence plugin. It provides ,f / ,F / ,t / ,T commands that do just that.

like image 104
Ingo Karkat Avatar answered Oct 23 '22 12:10

Ingo Karkat


one way without using regex, without counting "dot" (could be other letters)... see if others have better way..

foo[I]not.relevant.text.bar ([I] is cursor)

you could try:

lmm$T.d`m

or in this format, may look better?

lmm$T.d`m

this will do the job. you could create a mapping if you use that often.

EDIT

I add a GIF animation to show it works. :)

note

I typed @= in normal mode after moving my cursor to the right starting point (by f(space)), to display the keys I pressed in command line.

enter image description here

like image 44
Kent Avatar answered Oct 23 '22 11:10

Kent


I would use f df...

It is not necessarily shorter to type, but I find it easier to use "repeat last command" than counting in advance the number of word/sentence I want to delete.

Then you can adjust the number of . you type to adjust the length of the string you want to delete.

like image 4
Xavier T. Avatar answered Oct 23 '22 11:10

Xavier T.


For your example: ET.dB

foo not.relevant.text.bar

And it works, as long as the cursor is anywhere within the text following "foo".

Strip Path from Path+Filename: ET/dB

I use it for stripping a pathname of all but the trailing filename.

Strip the path from /some/long/path/filename.ext leaving only the filename.

Just as long as:

  • The cursor is anywhere within the bold word
  • There are no spaces in that word

    1. E Go to the end (since there are no spaces - also works if not the last thing on the line)

    2. T/ Find the last / (stop just after it, so it will be deleted, as well)

    3. dB Delete to the beginning of the word

like image 1
Brent Faust Avatar answered Oct 23 '22 13:10

Brent Faust