How can I remove all but the first n characters of a every line in the file buffer?
What I tried
Mark region
C-x r k #but this is cumbersome when the length of lines are very varied, and there are over 100 lines
How can I remove all but the first n characters of a every line in the file buffer?
What I tried
Mark region
C-x r k #but this is cumbersome when the length of lines are very varied, and there are over 100 lines
You can do a replace-regexp
like this:
^\(.\{15\}\).*$
\1
This will match lines with at least 15 characters, and replace with the 1st 15 (which are saved as a match group).
Did you mention that n=15?
(defun foo (beg end &optional n) (interactive "r\np") (let ((rgx (format "^\\(.\\{%d\\}\\).*$" n))) (replace-regexp rgx "\\1" nil beg end)))