How can I delete a line in VI?
Here what I am doing right now:
- Open up the terminal alt + ctrl + t
vi a.txt
- 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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityHow can I delete a line in VI?
Here what I am doing right now:
vi a.txt
Pressing dd
will remove that line (actually it will cut it). So you can paste it via p
.
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.
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.
d5d
. I don't know why, but I prefer this. (Fun fact: NdMd
deletes N × M lines)
Apr 15, 2011 at 19:28
Esc dd
after going to the line you want to delete
dd
delete (cut) a line and stay in the COMMAND
mode.S
delete (cut) a line and go to the INSERT
mode.In visual mode,
shift+v, choose the lines, then d
ctrl+v, choose the columns, then d
If you want to delete all lines containing a pattern, use g/pattern/d
, for example:
:g/r[ea]gu.*r/d