vi to delete from the beginning of the line till the cursor
Asked Answered
S

7

97

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

Sememe answered 9/1, 2013 at 15:29 Comment(0)
W
168

Try d0. 0 denotes the beginning of the line.

Wehrle answered 9/1, 2013 at 15:31 Comment(4)
If you are in insert mode you can use <C-U>.Stinko
not sure what happened but when I enter the d0, the line got deleted from my cursor location to start of the line.Lunalunacy
@YouAreAwesome That is exactly what d0 does, and what OP was asking for.Misdoing
related: https://mcmap.net/q/218891/-deleting-to-beginning-of-line-d0-leaves-extra-spaceFarceuse
C
72

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.

Chihli answered 9/1, 2013 at 15:31 Comment(0)
P
17

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

Planking answered 28/6, 2018 at 7:4 Comment(0)
T
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.

Tap answered 12/1, 2015 at 16:12 Comment(0)
C
6

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

Clank answered 21/10, 2022 at 16:37 Comment(1)
Since you are in insert mode you have to add the prefix '_i' to your help command.Palaeontography
T
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! :)

Torto answered 20/1, 2022 at 15:32 Comment(0)
R
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.

Rehnberg answered 17/9, 2020 at 11:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.