help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

TIL about string-rectangle


From: Skip Montanaro
Subject: TIL about string-rectangle
Date: Wed, 30 Dec 2020 14:59:25 -0600

I needed to replace the first comma on each line of a CSV file so I
could more conveniently sort by the first field (date, arranged as
MM/DD/YYYY). After a bit of fussing around, I got it squared away with
query-replace-regexp, did my sort, then needed to revert the
replacement text back to comma. Unfortunately, there were a few other
instances of the replacement text elsewhere in the file which caused
problems. (If I'd had my thinking cap on I would have checked before
doing the original replace, but it seems I was wearing my dunce
cap...) Poking around for help from Emacs against a future need, I
discovered replace-rectangle, which turns out to be an alias for
string-rectangle (bound to C-x r t). It looks to be quite handy when
you need to replace text within a rectangle. It's sort of (but not
quite) like narrow-to-region + query-replace(-regexp)?. It doesn't
query for a string to replace. It simply replaces the rectangle's
contents with the given string on each line.

Kinda cool,

Skip



reply via email to

[Prev in Thread] Current Thread [Next in Thread]