There are so many different text editors in Linux to choose from. Vi and emacs are probably two of the well known editors. There are probably several hundreds more..but as you are here you probably use emacs. Although emacs is not technically just a text editor, it does have text editing capabilities and we will consider it to be one for the purposes of this post. We will look at how the line deletion capabilities of the vi editor can be duplicated in emacs.
In the true emacs style, I will show you the command that does the function and followed by the keyboard shortcut that can be used if there is one……
Delete the line from the cursor position
This is useful if you want to delete the line starting with the current cursor position or point on the line. I find this to be the most used in my case, as usually I am already at the position that I need to delete the text from.
M-x kill-visual-line # kill the current line from point to end of line
The default shortcut that is mapped to the kill-visual-line command is <C-k>.
Delete the current line
Sometimes you would want to kill (or delete) the entire line. If you are at start of the line, it is easy. You can use the earlier command that we used and it will delete the rest of the line which happens to be the entire line. But if the cursor is positioned at another position in the line, then this will not work.
One option is to move the cursor to the start of the line and then use the C-k shortcut. The shortcut to move the cursor to start of the line is C-a, so you could do something like this:
M-x beginning-of-visual-line # move to the beginning
M-x kill-visual-line # kill the line
But lets say you want to delete the current line without moving the cursor or point. Then you could use the command
The default shortcut bound to this command is <C-S-backspace>. I usually find the earlier option easier but this will work just as well.
Delete multiple lines or a region
If you want to delete multiple lines then you can use the standard prefix command that emacs uses to repeat either of the previous two options.
C-u 7 C-S-backspace # This will delete 7 lines
M-7 C-S-backspace # This is same as the previous, just whichever you are used to.
C-a C-u 7 C-k # Again this is equivalent to the above two
Also, you can mark the region that you want to delete and then use backspace or del or kill (C-w) to delete the text. This is equivalent to deleting a region but will work the as deleting multiple lines since you have selected the lines that you want to delete.
Delete lines by repeating the command
Another option is to use the repeat last command feature in emacs. You can perform a command then repeat that command as many times as you want with a single keystroke.
C-S-backspace # Delete current line
C-x z # Repeats the last command
z # repeat as often as necessary to repeat the delete command
Use packages to define the region as the current line
The package whole-line-or-region in a minor mode that auto-selects the current line as the region when there is none selected. Install the package whole-line-or-region and then run (whole-line-or-region-global-mode).
C-w will now kill the whole line if there is no active region.
Delete current sentence
Emacs do differentiate between a line and a sentence. So you can delete the current sentence instead of just the line. The shortcuts and commands are similar to the ones that is used for the lines.
M-x kill-sentence # this will delete from the point to the end of the sentence.
The shortcut for the command is <M-k>.
In order to kill or delete the whole sentence, you can use the same technique as moving to the beginning of the sentence and then killing the sentence.
M-x backward-sentence # Moves backwards to the start of the sentence
The shortcut for this is <M-a>, so you do the following to delete a sentence:
Delete all lines that match a pattern
Now let’s say you want to delete all lines that match a pattern. I will mention a couple of ways to do that, and they are both interactive.
M-x delete-matching-lines # an alias for the flush-lines
The delete applies to the part of the buffer after the point or cursor. So if you want to delete from the entire buffer, you can either move to the top of the buffer or select the entire buffer.
Delete all lines that do not match a pattern
This is essentially the opposite of the earlier use case of deleting the lines that match the pattern. And we have commands for that.
M-x delete-non-matching-lines # an alias for the keep-lines
Just as with the earlier commands, these works on the part of the buffer after the point.
Delete all blank lines
Again, this is subset of the earlier use case of deleting the lines that match the pattern. Here the pattern happens to be empty. You can use flush-lines command to do that.
M-x flush-lines # use the pattern ^$ to delete empty lines
If you want also delete lines that contain only spaces then you should use a regular expression that account for spaces.
M-x flush-lines # use the pattern ^[[:space]]*$ to delete empty and lines with spaces
There is another command delete-blank-lines that lets you manage blank lines in a more intuitive way. If you use the command on a blank line, it will collapse to just one empty line by deleting all the surrounding blank lines. If there is no surrounding blank line, then it will delete the current one. If the current line is not an empty line, then it will delete any blank lines that immediately follow the current line.
This command is bound to <C-x> <C-o>