2

I have the following:

e.printStackTrace();

with the block cursor over the semicolon, what is the easiest way to delete that and end up with the cursor where the first e is?

So dd for deleting the line is out, I guess ciw would only delete the (); part of the line, or I could press v to highlight go to the beginning of the line using ^ and then pressing d to delete the highlighted part, then into insert mode and then to tab in to the right indent.

It is quite a lot of keys which I don't particularly mind, but I am not sure whether I am thinking in a Vim like manner...

Any suggestions / eye openers / mind openers would be really useful and appreciated.

5

Probably the best thing you can do in this case is use cc/S and just retype the e.

Se

However as you look like you are using a language that uses ; as endings and .'s, then you probably just want to do something like cT. to make your changes.

In my opinion it is probably not worth optimizing your keystrokes to save typing one character of text on a line. Best to just clear the line and start over.

  • You are are optimizing keystrokes to save 1 character on the line. Really? - not sure what you mean...i just wanted to remove everything to the beginning and and put it in insert mode, if is more simply put. Sorry for any confusion. – user2405469 Jun 16 '15 at 15:54
  • It means, since you want to end up with the cursor on the e - it's easier to delete the whole line in one go and write the e again. Typing S has the threefold advantage of 1. deleting the whole line 2. indenting and 3. leaving you in insert mode to type the e again etc. – VanLaser Jun 16 '15 at 21:34
  • I would have assumed that the OP's data has some context that should not be touched, like in: pre-context; e.printStackTrace(); post-Context;. (Otherwise deleting the whole line with S would have been the obvious way. But it's very unlikely that in such code there wouldn't even be an indentation to preserve.) – Janis Jun 17 '15 at 4:27
  • In this case (which frankly I don't see in the original question), I found the solution proposed above, with cT. (which preserves the ';') the best one. – VanLaser Jun 17 '15 at 7:49
4

I'm not sure that is the optimal way, but I'd basically do: dB. That will keep the ;, so that depending on whether I want to instantly continue adding new text at the place of the replacement or just also remove the ; while staying in command mode I'd do one of: dBx or dBs. In visual mode that would be one of: vBd or vBs.

  • dB and dBs are way better solutions! I still have a while to go with getting comfortable with all of the bindings, so I find myself using a lot of keys do get it done, which is contradictory to using vim I guess... – user2405469 Jun 16 '15 at 14:29
2

dB deletes to the beginning of the word (whitespace-separated words, since it's a capital B), but that excludes the current character, so to remove the ; as well you'll need dBx, or dBs to go to insert mode immediately. Or you can use an inclusive motion, but again with a whitespace-sparated word: diW, or ciW to go to insert mode.

Alternatively, move back then delete: BdW or BcW. If there was additional stuff on the line after the semicolon, that would delete everything up to the end of the next sequence of whitespace.

If you consider the text to delete to be everything except the indentation, rather than just the current word, then you would need cv^ (c^ is transformed into a linewise command, so deletes the indendation as well; v makes it characterwise). Unfortunately ^ is exclusive, so the ; stays behind. But in this case moving then deleting is shorter anyway: ^D or ^C.

2

In this particular case I would do it with 2 keystrokes: ^C

  • ^ Allows you to go back to the first non white character (which is the "e" in your case).
  • C Triggers the change command from the cursor to the end of the line. It is the equivalent to c$.

This way you keep the indentation, and you only need one key to delete the content and switch to insert mode.

  • This is an awesome solution, I still have a lot to learn and get comfortable with ... arrrghh – user2405469 Jun 17 '15 at 9:01
  • @user2405469: ahah I think with vim you never stop learning ;) Note that on the same principle than C other commands operating on line exists like D which deletes the end of the line, V which selects all the line, Y which yanks all the line etc... – statox Jun 17 '15 at 9:07
0

with the block cursor over the semicolon, what is the easiest way to delete that and end up with the cursor where the first e is?

To answer this, precisely, dvF..

  • d delete (:help :delete)

  • v inclusively, since F is an exclusive character motion (:help o_v)

  • F find to the left (:help F)

  • . literal dot character

Summing up, this means delete inclusively to the left up to the dot.

If you have multiple dots, such as,

e.e.printStackTrace();

you can add a count to F, e.g.:

dv2F.

If you want to go into insert mode immediately after deletion, change the operator (:help operator) from d to c (change):

cvF.
0

There are many ways to do what you want.

If that text is the only content of the line you could do:

ddO<Esc>

or:

cc<Esc>

or:

S<Esc>

or:

0D

or:

^D

or:

BD

or probbaly other commands.

If that text is part of a longer line, you could do:

Fe;D

or maybe:

T D

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.