100

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 7

172

Try d0. 0 denotes the beginning of the line.

4
  • 37
    If you are in insert mode you can use <C-U>. Commented Jan 9, 2013 at 18:08
  • 1
    not sure what happened but when I enter the d0, the line got deleted from my cursor location to start of the line. Commented Dec 14, 2017 at 6:43
  • 1
    @YouAreAwesome That is exactly what d0 does, and what OP was asking for.
    – sherrellbc
    Commented Jun 16, 2020 at 16:48
  • related: stackoverflow.com/a/61276398/3277592
    – mxt
    Commented Jun 17, 2023 at 19:02
74

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.

18

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

8

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.

To get the vim documentation simply type

:help i_CTRL-U

in the command line.

CTRL-U Delete all entered characters before the cursor in the current line. If there are no newly entered characters and 'backspace' is not empty, delete all characters before the cursor in the current line. If C-indenting is enabled the indent will be adjusted if the line becomes blank. See i_backspacing about joining lines. i_CTRL-I i_ i_Tab

1
  • Since you are in insert mode you have to add the prefix '_i' to your help command.
    – abu_bua
    Commented Mar 10 at 14:58
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.

0
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! :)

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.