I recently asked:
How to add a prefix to every line?
So go from the code below:
I said Hello
I said There
I said I am some code
To code like:
Hello
There
I am some code
Now, how do I delete n number of characters from a line?
I recently asked:
How to add a prefix to every line?
So go from the code below:
I said Hello
I said There
I said I am some code
To code like:
Hello
There
I am some code
Now, how do I delete n number of characters from a line?
It's even easier! Again, use rectangle commands. Move to one corner of the rectangle you want to delete, press C-SPC
to set the mark. Move to the other corner of the rectangle and press C-x r d
(delete-rectangle
). For example, move to the first line you want to act on, press C-a
or home
to go to the beginning of the line, move down to the last line you want to act on, C-u number C-f
to move to the bottom right corner, and then C-x r d
to delete.
If the last line has fewer than the desired number of characters, it may be more convenient to use the bottom left and top right corners instead. Alternatively, you can insert junk characters (this is useful if both the first line and the last line are shorter).
You can use C-x r k
to kill the rectangle, which allows you to yank (C-x r y
) it later (move to the upper left corner of the place where you want to insert it).
You can also use C-x r t
(string-rectangle
) and enter an empty string.
Rectangle commands are based on column positions. If you want to delete n characters, rather than n columns (which makes a difference with multi-column characters such as tabs), you can use regular expression replacement: replace ^.\{42\}
by nothing to delete the first 42 characters of every line.
If you have cua-mode
enabled, then:
I prefer the cua-mode
way of working, compared to the Emacs standard rectangle where the equivalent would be:
The cua-mode
saves three key presses compared to standard Emacs rectangle.
Another possibility: I really like to use multiple cursors for tasks like this. Available in package.el
via MELPA, it's become an indispensable part of my emacs. I would simply select I said
(and possibly include the line break before it), use M-x mc/mark-all-like-this
, and be on my way. If you want more fine-grained control, you can use mc/mark-next-like-this
or mc/mark-next-lines
.
My keybindings:
(use-package multiple-cursors
:bind (("C->" . mc/mark-next-like-this)
("C-M->" . mc/mark-all-like-this-dwim)))
The obligatory Emacs Rocks! episode: http://emacsrocks.com/e13.html
For the sake of completeness: deleting the first 3 characters from each line (formally: from the point on, or in the region) can be done by regexp-replacing ^...
(or more generally: ^.\{3\}
) with an empty string.
Explanation: in regexen, ^
means "beginning of line", .
means "any character", and \{n\}
means "n repetitions of the thing before".
This has an additional benefit of being interactive (unless one presses !
, which makes query-regexp-replace
replace everything from now on without further questions).
And one more possibility:
F3C-aC-3C-dC-nF4
records a keyboard macro, saying basically "move to the beginning of line, delete three characters, and move to the next line". Then, you can replay it until the end of buffer by C-uF4, or C-xC-kr to apply it to the lines in region. This has one disadvantage: it will break when any line has less than 3 characters.
If it's only a line you can zap-to-char with M-z asuming that character is not repeated between the point and the one you want to delete up to. For some cases this is faster than creating a region.