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:

I am some code

Now, how do I delete n number of characters from a line?

  • IMHO it is the same as your previous question. – A_P Feb 18 '19 at 22:04

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.

  • 3
    instead of trying to remember all the rectangle commands, you can also use C-x spc, instead of C-spc, which turns your region into a rectangle and everything just works. – Malabarba Oct 27 '14 at 15:43
  • 1
    @Malabarba Only since Emacs 24 though. (Writing this on a machine with Emacs 23. They're still very much around.) – Gilles 'SO- stop being evil' Oct 27 '14 at 22:56
  • 2
    My condolences... :-( – Malabarba Oct 27 '14 at 23:07
  • This just worked for me on GNU Emacs 23.4.1 (x86_64-pc-linux-gnu, GTK+ Version 2.24.21) of 2013-10-25 on allspice, modified by Debian – davidrmcharles Jan 7 '16 at 18:48

If you have cua-mode enabled, then:

  • C-a to move to beginning of line,
  • C-RET to start rectangle,
  • Then move with arrow keys or any emacs move command like C-f
  • DEL to delete selection

I prefer the cua-mode way of working, compared to the Emacs standard rectangle where the equivalent would be:

  • C-a to move to beginning of line,
  • C-SPC to start rectangle,
  • Then move with arrow keys or any emacs move command like C-f
  • C-x r d to delete selection

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).

  • 1
    Why the downvotes? – mbork Nov 12 '14 at 10:18

And one more possibility:


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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