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

VimText EditorVi

Vim Problem Overview


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

Vim Solutions


Solution 1 - Vim

Try d0. 0 denotes the beginning of the line.

Solution 2 - Vim

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

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

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.

Solution 5 - Vim

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

Solution 6 - Vim

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionblue123View Question on Stackoverflow
Solution 1 - VimLars KotthoffView Answer on Stackoverflow
Solution 2 - VimMark WilkinsView Answer on Stackoverflow
Solution 3 - Vimmatson kepsonView Answer on Stackoverflow
Solution 4 - VimgregcaporasoView Answer on Stackoverflow
Solution 5 - VimEd GreenbergView Answer on Stackoverflow
Solution 6 - VimmrwonderfulnessView Answer on Stackoverflow