delete line in vi

461,752

Solution 1

Pressing dd will remove that line (actually it will cut it). So you can paste it via p.

Solution 2

As others said, it's dd if you're in normal mode (press ESC) to ensure you are in normal mode, but by default, you will be. You may have found that vi doesn't act as you would expect an editor to act. It's pretty unique, and those unique features make it extremely powerful and fast to work with. If you want to learn more, you can run vimtutor at the command line, and you'll get a quick (maybe an hour worth of reading/experimenting) tutorial on the basics of how to use it.

Now, you can use an editor with a more familiar interface. Most systems have nano. You can type nano a.txt to open your file in nano. You can even go a step further - there isn't any reason you have to use a terminal-based editor. Since you mentioned using (Ctrl+Alt++T) and this is the default shortcut in Ubuntu and some other Gnome distributions, so I'd be surprised if you didn't have gedit installed. At the command line, try gedit a.txt and you'll get a graphical editor.

Solution 3

To delete the line under the cursor, use dd.

The delete command accepts all the normal positional modifiers, so if you are on the beginning of the line below the one you want to delete, you could just dk to go into delete mode and move up one line, deleting everything the cursor passed.

You can also specify repetition, so 5dd will delete the line under the cursor five times, effectively deleting the current and following four lines.

Solution 4

Esc dd after going to the line you want to delete

Solution 5

  • dd delete (cut) a line and stay in the COMMAND mode.
  • S delete (cut) a line and go to the INSERT mode.
Share:
461,752

Related videos on Youtube

I-M-JM
Author by

I-M-JM

Updated on September 18, 2022

Comments

  • I-M-JM
    I-M-JM over 1 year

    How can I delete a line in VI?

    Here what I am doing right now:

    1. Open up the terminal alt + ctrl + t
    2. vi a.txt
    3. I move my cursor to the line which I wan to delete, then what key-combination is should use to delete line in vi editor?
  • Shawn J. Goff
    Shawn J. Goff about 13 years
    A few people need it. If you edit text files regularly, it's worth the time to learn vim. If you don't, and you're just trying to follow some instructions to get something working, then it's probably not worth it unless you just interested in trying different software and learning new things.
  • Random832
    Random832 about 13 years
    Or d5d. I don't know why, but I prefer this. (Fun fact: NdMd deletes N × M lines)
  • I-M-JM
    I-M-JM about 13 years
    I fired vimtutor at command line, and it opened up a vim, with a temp. file. Have I done any mistake?
  • Shawn J. Goff
    Shawn J. Goff about 13 years
    @i-m-jm I don't think you've made a mistake, but your system might have an unusual configuration. It should open a document that begins "Welcome to the VIM Tutor". If you're interested in working through that, you should post another question.
  • Anthon
    Anthon over 10 years
    The escape is not necessary, if you can go to the line you want to delete.
  • Alexander
    Alexander about 10 years
    Escape is needed if you use the arrow keys in insert mode to go to the right line.