I need to truncate the lines in a file to 24 characters, to feed to a small dot-matrix printer. How can I do this? I use doom emacs.

2 Answers 2


From the shell:

cut -c1-24 FILE

In Emacs, interactively:

M-x replace-regexp RET ^\(.\{24\}\).+ RET \1 RET


phils has answered your question, but i wonder if you didn't mean that you wanted to limit the line lengths of a given file so that they'd all print within the constraint of your printer, rather than cutting your lines off.

if that is so, you need to set the fill-column to a value small enough, and then fill (wrap) your text, which limits your line lengths to the fill-column's value:

use set-fill-column (C-x f in vanilla emacs) to set a value for it.

then fill (wrap) your text with e.g. fill-region or fill-paragraph. to wrap a whole file you can just select whole buffer with mark-whole-buffer (C-x h in vanilla emacs) then run fill-region.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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