88

How can we use vim to delete characters from the beginning of the line till the cursor. Say, we have a string "hello world" while the cursor is on "w". How can we delete from "h" till "w".

Jonas Stein
  • 6,826
  • 7
  • 40
  • 72
blue123
  • 2,937
  • 7
  • 27
  • 29

7 Answers7

154

Try d0. 0 denotes the beginning of the line.

Lars Kotthoff
  • 107,425
  • 16
  • 204
  • 204
67

I believe that the following should work (d^):

d^

This assumes that you only want to delete to the h even if there is white space in front of it. It will leave the white space.

Mark Wilkins
  • 40,729
  • 5
  • 57
  • 110
15

TLDR: The easiest way is to do the following

  • use the navigate mode
  • set the cursor at whatever line you want
    • press dgg - this will remove everything from cursor to the beginning
    • press dG - this will remove all the lines from cursor till the end of the doc.

But why?

gg - goes to the begin of the document
G - navigates at its very end
d - delete mode

I was also looking for some combinations with the notation of d and line number, and when you are editing huge files like GB in size I found it bit annoying. Like to remove first 3 lines you need to do this :0,d3, but when the docu is over 1mln lines its tough for my eyes to read this crap...

mati kepa
  • 2,543
  • 19
  • 24
6

The other suggestions didn't work for me, but I used visual mode to achieve this.

I moved the cursor to the position I wanted to delete through, and hit v to enter visual mode. Then I hit ^ to select back to the beginning of the current line, and then d to delete all of the highlighted text.

PoolloverNathan
  • 148
  • 2
  • 11
gregcaporaso
  • 444
  • 4
  • 11
4

If you're in insert mode and you want to delete to the start of the line (and stay in insert mode), you can use CTRL+u

This matches the bash meaning of CTRL+u as shown here.

I don't know if this is undocumented or just a quirk of my setup (neovim on Ubuntu) but doing :help CTRL-U shows the help for the keystroke out of insert mode. Maybe someone can help me find the help which points to the usage I'm describing here.

LondonRob
  • 73,083
  • 37
  • 144
  • 201
1

Well, if your cursor is on "w", you'll be deleting backwards...

You can use vim's "till" t, but moving backwards it requires an uppercase T. The same works for "find" f moving backwards it's F.

So, in your case, you can delete back "till" the quote symbol: dT"

Or, if to prefer targeting/finding the "h": dFh

Try jumping around first without the delete to get a feel for it, then you can just layer in the action as the prefix.

Happy Vimming! :)

mrwonderfulness
  • 361
  • 2
  • 7
0

not sure what happened but when I enter the d0, the line got deleted from my cursor location to start of the line. – YouAreAwesome Dec 14 '17 at 6:43

Confirming that this worked. It seems to be the simplest method. (You can't upvote a comment, so I'm adding it as an answer in it's own right.)

Example: In .ssh/authorized_keys, I had:

no-port-forwarding,no-agent-forwarding,no-X11-forwarding,command="echo 'Please login as the user "centos" rather than the user "root".';echo;sleep 10" ssh-rsa AAAAB3NzaC1yc2...

This is the way Amazon AWS keeps you from logging in as root.

I put the cursor on the s in ssh-rsa and hit 'd0' and got a perfect delete to beginning of line.

Ed Greenberg
  • 209
  • 3
  • 12