简体   繁体   中英

How to replace spaces with A[ in emacs

I have an input file like this

    line 1
    line 2
    line 3

There are four spaces before all the lines I want to replace.

I want the end result to be:

A[line 1
A[line 2
A[line 3

(Its funny, so editor doesn't want to display line by line)

I tried Mx replace-regex ^[]+ -> A\\[ , but get error invalid regex "Unmatched [ or [^"

I tried Mx replace-regex ^[]+ -> A[ , but get same error.

Replacing the [ is a problem. How to fix this?

Try

M-x query-replace-regex RET ^    RET A[ RET
                             ^^^
                          notice the 4 spaces here

In your examples you are missing a space inside the character class: ^[ ]+

Prettier would probably be: ^[[:space:]]+ for every kind of white-space.

Rectangles give you another way to deal with these issues. Cua-mode provides an even nicer system. To use it, first add the following to your .emacs:

(setq cua-enable-cua-keys nil)
(cua-mode)

Then, with point at the * in the following:

*   line 1
    line 2
    line 3

Hit C-enter , and use the arrow keys (or Cn and Cf ) to move point to:

line 1
    line 2
   *line 3

The blanks will all be highlighted. Type A[ to insert the characters. Then hit Cw to delete the spaces.

M-x query-replace-regex RET ^ + RET A[ RET

因为它是一种可替换的字符,所以不需要方括号或类

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM