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".
7 Answers
Try d0
. 0
denotes the beginning of the line.

- 107,425
- 16
- 204
- 204
-
34If you are in insert mode you can use `
`. – Daan Bakker Jan 09 '13 at 18:08 -
1not sure what happened but when I enter the d0, the line got deleted from my cursor location to start of the line. – Indrajeet Gour Dec 14 '17 at 06:43
-
1@YouAreAwesome That is _exactly_ what `d0` does, and what OP was asking for. – sherrellbc Jun 16 '20 at 16:48
-
related: https://stackoverflow.com/a/61276398/3277592 – Mxt Jun 17 '23 at 19:02
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.

- 40,729
- 5
- 57
- 110
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...

- 2,543
- 19
- 24
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.

- 148
- 2
- 11

- 444
- 4
- 11
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.

- 73,083
- 37
- 144
- 201
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! :)

- 361
- 2
- 7
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.

- 209
- 3
- 12