Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?
share|improve this question
4  
I suggest you read washington.edu/computing/unix/vi.html it gives you a basic usage overview. Here's a cheatsheet: tuxfiles.org/linuxhelp/vimcheat.html It's always a good idea to take a look at pages like that before asking trivial questions... –  polemon Apr 15 '11 at 12:41
add comment

5 Answers 5

up vote 18 down vote accepted

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

share|improve this answer
8  
You have to be quick on the draw to get +10 for dd –  Eric Wilson Apr 15 '11 at 16:59
add comment

As others said, it's dd if you're in normal mode (pres 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.

share|improve this answer
4  
+1 for vimtutor. That's what people asking this question really need. –  phunehehe Apr 15 '11 at 14:15
    
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. –  Shawn J. Goff Apr 15 '11 at 16:36
    
I fired vimtutor at command line, and it opened up a vim, with a temp. file. Have I done any mistake? –  I-M-JM Apr 16 '11 at 4:21
    
@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. –  Shawn J. Goff Apr 16 '11 at 8:57
add comment

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.

share|improve this answer
3  
Or d5d. I don't know why, but I prefer this. (Fun fact: NdMd deletes N × M lines) –  Random832 Apr 15 '11 at 19:28
add comment

Make sure you are now in the "Normal" mode, and then press dd. The command ndd will delete n lines from the line which the cursor is on.

share|improve this answer
3  
There is already an upvoted and accepted answer with this suggestion and another with details on your additional suggestion. In the future if you don't have anything new to add, throw your weight behind somebody elses answer. This is a collaborative community where we build on each other not fight for attention. –  Caleb Apr 15 '11 at 13:04
add comment

Esc dd after going to the line you want to delete

share|improve this answer
1  
The escape is not necessary, if you can go to the line you want to delete. –  Anthon Jan 14 at 6:15
    
Escape is needed if you use the arrow keys in insert mode to go to the right line. –  Alexander May 4 at 20:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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