You are not logged in.

#1 2013-09-27 22:01:56

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

vim: delete to last <character> on line

Is there a shortcut for this? Ie. say I have a line like

... /home/alphaniner/data/Downloads/isos/Linux/syslinux/custom/sysresccd-20130927-2009.md5

and I want to delete from my cursor to the last / on the line. I know I could count the slashes/etc. but that's just tiresome.


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#2 2013-09-27 22:35:58

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,546
Website

Re: vim: delete to last <character> on line

I don't know if there is a single key just for this, but these 5 would do it, you could make that into a binding for any character:

$F/d0

"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Online

#3 2013-09-27 22:44:12

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: vim: delete to last <character> on line

That deletes to the beginning of the line, not the cursor. You could set a mark first (maybe there's even some kind of automatic mark for this? I need to learn to use marks more):

ma$F/d`a

But you could also do (in this example)

d//sysr<CR>

This is usable as long as the part after the last / is (visible and) unique (and doesn't share a long prefix with a path element between it and the cursor).

Last edited by Raynman (2013-09-27 22:46:17)

Offline

#4 2013-09-27 22:51:49

Wibjarm
Member
Registered: 2012-05-04
Posts: 43

Re: vim: delete to last <character> on line

For what it's worth, you could also use visual mode for that:

v$F/d

which would work pretty similarly to Raynman's mark method, but without having to actually mess around with marks.

Offline

#5 2013-09-27 23:02:05

bohoomil
Banned
Registered: 2010-09-04
Posts: 2,377
Website

Re: vim: delete to last <character> on line

alphaniner wrote:

I want to delete from my cursor to the last / on the line.

D

:: Registered Linux User No. 223384

:: github
:: infinality-bundle+fonts: good looking fonts made easy

Offline

#6 2013-09-27 23:05:21

oliver
Member
Registered: 2007-12-12
Posts: 448

Re: vim: delete to last <character> on line

bohoomil wrote:
alphaniner wrote:

I want to delete from my cursor to the last / on the line.

D

I think he means specific character (in the example a / ), not the last character on the line

Offline

#7 2013-09-27 23:10:08

bohoomil
Banned
Registered: 2010-09-04
Posts: 2,377
Website

Re: vim: delete to last <character> on line

oliver wrote:

I think he means specific character (in the example a / ), not the last character on the line

Oh yes, you're right. My bad, sorry.


:: Registered Linux User No. 223384

:: github
:: infinality-bundle+fonts: good looking fonts made easy

Offline

#8 2013-09-27 23:52:29

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: vim: delete to last <character> on line

df/.......

It's vim golf, just with bad putting... smile


# edit: FWIW, I just take a wild guess at the number and refine once the damage is done. In th above case I initially guessed a 6, which just leaves `..`


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#9 2013-09-27 23:53:43

ngoonee
Forum Fellow
From: Between Thailand and Singapore
Registered: 2009-03-17
Posts: 7,362

Re: vim: delete to last <character> on line

If you know its the last character in the line, why not this?

vj?*-Enter-d

visual mode - go down a line - reverse search for character (put the character you want) and press enter - delete


Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.

Offline

#10 2013-09-28 00:01:08

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,546
Website

Re: vim: delete to last <character> on line

ngoonee, that's like v$F/d except requiring more keypresses and failing if there is a / near the beginning of the line below.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Online

#11 2013-09-28 02:54:55

ngoonee
Forum Fellow
From: Between Thailand and Singapore
Registered: 2009-03-17
Posts: 7,362

Re: vim: delete to last <character> on line

Thanks Trilby. I don't know what 'F' is in vim though, not bound to anything for me (I'm assuming its basically ?).


Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.

Offline

#12 2013-09-28 11:58:25

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: vim: delete to last <character> on line

'F' searches back for the character. 'Fi' moves the cursor to the previous occurrence of 'i'. 'fi' does the same, but forward.

Offline

#13 2013-09-28 12:24:33

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,546
Website

Re: vim: delete to last <character> on line

F and f only work within a line.  Is this possibly why you are not seeing any effect, ngoonee?  Go to the end of a line of text, for example, and hit F<space>.  "?" searches backward, but it waits for a <enter> to end a string of text, while F I would not call a search, but a cursor movement like h, l, w, or b.

Last edited by Trilby (2013-09-28 12:25:02)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Online

#14 2013-09-28 12:28:12

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: vim: delete to last <character> on line

Oh yes, it's a move, not a search, my bad.

Offline

#15 2013-09-28 13:01:52

ngoonee
Forum Fellow
From: Between Thailand and Singapore
Registered: 2009-03-17
Posts: 7,362

Re: vim: delete to last <character> on line

Trilby wrote:

F and f only work within a line.  Is this possibly why you are not seeing any effect, ngoonee?  Go to the end of a line of text, for example, and hit F<space>.  "?" searches backward, but it waits for a <enter> to end a string of text, while F I would not call a search, but a cursor movement like h, l, w, or b.

Years of using vim and I never knew this! Thanks Trilby (and karol) smile I've always used / and ? for searching.


Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.

Offline

#16 2013-09-28 14:49:43

spikkle
Member
Registered: 2013-09-19
Posts: 13

Re: vim: delete to last <character> on line

More terrible putting, you could do this while recording a macro and using a mark.

In NORMAL mode with your cursor where you want it to be on one line of your choice...

qqma$F/d`axq
qq  - Start recording into 'q'
ma  - Set a mark at the current position of the cursor named 'a'
$   - Go to the end of the line
F/  - Find, going backwards, the first occurrence of '/'
d`a - Delete from the current position to the position of the mark 'a'
x   - Delete the leftover slash character
q   - Stop recording

Now this is stored in a macro you can use again and again from normal mode by typing:

@q

Or more simply to repeat the last executed macro:

@@

Offline

#17 2013-09-28 17:56:32

stewie
Member
Registered: 2009-05-16
Posts: 20

Re: vim: delete to last <character> on line

This is the shortest I could come up with.

$T/d0
$ - end of line
T/ - go back till /
d0 - delete to beginning of line

You could turn this into a macro if you need to repeat this.


We wants it, we needs it. Must have the precious. Arch Linux.

Offline

#18 2013-09-28 18:26:18

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,546
Website

Re: vim: delete to last <character> on line

Stewie, that's basically the same as my first shot - that deletes from the beginning of the line, not from the current cursor position.  Wibjarm's seems to be the best answer (so far?).


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Online

#19 2013-09-28 19:23:20

stewie
Member
Registered: 2009-05-16
Posts: 20

Re: vim: delete to last <character> on line

Trilby wrote:

Stewie, that's basically the same as my first shot - that deletes from the beginning of the line, not from the current cursor position.  Wibjarm's seems to be the best answer (so far?).

Your answer will still leave a / . I'm assuming alphaniner wanted that removed. My answer is very similiar to Wibjarm's, just a different method. I hadn't seen anyone use the `T' command yet, and it seemed appropriate in this situation.


We wants it, we needs it. Must have the precious. Arch Linux.

Offline

#20 2013-09-30 13:27:19

alphaniner
Member
From: Ancapistan
Registered: 2010-07-12
Posts: 2,810

Re: vim: delete to last <character> on line

Wow, I wasn't expecting so much input, thanks everybody. smile

I'll test them out later when I have a chance.


But whether the Constitution really be one thing, or another, this much is certain - that it has either authorized such a government as we have had, or has been powerless to prevent it. In either case, it is unfit to exist.
-Lysander Spooner

Offline

#21 2013-09-30 14:27:26

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: vim: delete to last <character> on line

Can do it as a command, too:

:s/\%#.*\//

\%# is the cursor position.

Last edited by Procyon (2013-09-30 14:50:32)

Offline

Board footer

Powered by FluxBB