3

Each line in my file has a character ; followed by information that I want to delete. What is the best way to do it?

Example:

I like phat beatsies; but Mark does not like them
I also like dogsies;  irrelavant information

should become

I like phat beatsies
I also like dogsies
6

You can use replace-regexp with the short regular expression ;.*, which, in this context, means "semi-colon and everything after it on the rest of the line".

In practice, put your cursor before the point at which you want to start removing the text (alternately, highlight the region you want to use). Do M-x replace-regexp. It will prompt you for the text to be replaced, so enter ;.*. It will then prompt you for the text to replace it with. You want to remove it, so just hit enter, which is effectively telling it to replace the text with nothing.

1

Aside from the straightforward solution proposed by Dan which works in this and other situations, you can use the below function which kills every line from a certain character (the function asks a character or a string as input and it does this action from the cursor until the end of buffer).

(defun kill-line-from-certain-character-or-string (str) (interactive "skill line from the character/string: ")
(save-excursion (while (re-search-forward str nil t)
        (backward-char)(kill-line)
)  ))

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.