VaNa
VaNa

Reputation: 367

vim delete till one character before the end of the line

I can press $ to go to the end of the line so if I want to delete everything till EOL I can do d$ (or D). What if I want to delete till EOL column - 1 character (or - n chars more broadly)?

Example

I have a row 1234.567890 where . represents the cursor and want 1234.0 (. represents cursor again).

Upvotes: 0

Views: 957

Answers (3)

sidyll
sidyll

Reputation: 59277

There is no built-in movement for that, but you can create your own easily with :omap. It will work with any operator.

:onoremap <silent> q :<C-U>normal! v$hh<CR>

Now dq will do what you want, as well as cq to change until the before-last char, vq, yq and so on.

This works by replacing q with a call to :normal to initiate a visual selection, up to the end of the line (v$) and back two characters (that's because $ selects until the newline itself). The <C-U> clears any possible range.

Upvotes: 1

Ingo Karkat
Ingo Karkat

Reputation: 172510

If there's no identical character in between (or the number of those is easily determined), the f and t commands are very useful, because they just involve two keystrokes (the command and the target character, possibly prepended by a [count]), and (unlike the more generic /...<CR>) they are limited to the current line. For your example, that would be dt0 then.

For more complex scenarios, before I stop and lengthily contemplate possible solutions, visual mode is a quick alternative that lets you iteratively fine-tune the area before applying the command. I think it's a great pragmatic addition to the original command set of vi. For your example, that would be v$hd.

Upvotes: 1

steffen
steffen

Reputation: 16938

How about

d/.$<CR>

This deletes until one character before EOL.

Upvotes: 3

Related Questions