Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Replay search Mode by vi in linux

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to achieve the replay search mode in vi in linux, which is very detailed and has certain reference value. Friends who are interested must read it all!

Replay the search mode

When searching for patterns to replace in a file, you can have vi save any patterns you want to match in a buffer; then, when you perform a replacement, you can replay them with buffer reference numbers. The way to do this is to put the pattern between\ (and\), which instructs vi to put the pattern in a numbered buffer (1 to 9). When performing a replacement, you can reference these buffers with buffer reference numbers\ 1 through\ 9.

For example, suppose you want to search for lines starting with the word Martin in the file shown in listing 7 and add the prefix Mr and suffix Wicks to each match, enter command mode, enter the vi command:% s / ^\ (Martin\) / Mr\ 1 Wicks/g, and then press enter.

Listing 7. Replay the search pattern (previous) Martin is an IT consultant. Martin likessnowboarding and mountain biking. Martin hasworked on UNIX systems for over 15 years. Martin alsoworked for many years before that on mainframes.Martin lives in London.~~~~:%s/ ^\ (Martin\) / Mr\ 1 Wicks/g

Let's break down this command and explain it:

:% s-instructs vi to perform a replacement.

/-pattern delimiter.

^\ (Martin\)-look for the line that begins with the string Martin and save the string in buffer 1.

/-pattern delimiter.

Mr\ 1 Wicks-replace the found string with the string Mr, plus the contents of buffer 1, plus the string Wicks.

/-pattern delimiter.

G-Global modification (that is, modify all matching places).

Buffer references can be used in both search and replace strings.

The result of the modification is shown in listing 8.

Listing 8. Replay the search mode (after that) Mr Martin Wicks is an IT consultant. Martin likessnowboarding and mountain biking. Martin hasworked on UNIX systems for over 15 years. Martin alsoworked for many years before that on mainframes.Mr Martin Wicks lives in London.~~~~:%s/ ^\ (Martin\) / Mr\ 1 Wicks/g is all the contents of the article "how to replay the search pattern in vi in linux". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report