vi to delete from the beginning of the line till the cursor

47,410

Solution 1

Try d0. 0 denotes the beginning of the line.

Solution 2

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.

Solution 3

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

Solution 4

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.

Share:
47,410
blue123
Author by

blue123

Updated on July 05, 2022

Comments

  • blue123
    blue123 almost 2 years

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

  • Daan Bakker
    Daan Bakker over 11 years
    If you are in insert mode you can use <C-U>.
  • Indrajeet Gour
    Indrajeet Gour over 6 years
    not sure what happened but when I enter the d0, the line got deleted from my cursor location to start of the line.
  • sherrellbc
    sherrellbc almost 4 years
    @YouAreAwesome That is exactly what d0 does, and what OP was asking for.