简体   繁体   中英

Search and replace in a range of line and column

I want to apply a search and replace regular expression pattern that work only in a given range of line and column on a text file like this :

AAABBBFFFFBBBAAABBB
AAABBBFFFFBBBAAABBB
GGGBBBFFFFBHHAAABBB

For example i want to replace BBB with YYY in line range 1 to 2 and from column 4 to 6, then obtaining this output :

AAAYYYFFFFBBBAAABBB
AAAYYYFFFFBBBAAABBB
GGGBBBFFFFBHHAAABBB

Is there a way to do it with Vim ?

:1,2 s/\%3cBBB/YYY/

\\%3c表示第三列(请参阅:help /\\%c或更全局的:help pattern

If this is always the first one you want to replace, simply don't specify /g

:1,2s/BBB/YYY/

would work fine.

Alternatively, if you need to exactly specify which column you want replaced, you can use the \\%Nv syntax, where N is the virtual column (column as it looks, so tabs are multiple columns, use c instead of v for actual columns)

Replacing the second set of B's on lines 1 and 2 could be done with:

:1,2s/\%11vBBB/YYY/

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