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.
You can use my JumpToLastOccurrence plugin. It provides ,f
/ ,F
/ ,t
/ ,T
commands that do just that.
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.
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.
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".
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:
There are no spaces in that word
E Go to the end (since there are no spaces - also works if not the last thing on the line)
T/ Find the last /
(stop just after it, so it will be deleted, as well)
dB Delete to the beginning of the word
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With