Summary of methods for replacing strings in Linux Vim

Source: Internet
Author: User
Document directory
  • 1. Basic replacement
  • 2. You can use # As the separator. The/in the middle will not be used as the separator.
  • 3. Delete ^ m from the file
  • 4. Other skills

Programs that use the Linux environment for development often compete with editors, such as vim or Emacs. Both of them are the development artifacts of programmers. No matter which one is used, it will help you get twice the result with half the effort. This article focuses on Vim's string replacement methods and techniques. These methods and techniques are just one of the many tips of Vim's use. Most of the techniques described in this article come from the Internet. The purpose is to help you find and communicate with netizens in a timely manner.

1. Basic replacement

: S/str1/str2/Replace the first str1 in the current row with str2

: S/str1/str2/g replace all str1 in the current row with str2

: N, $ S/str1/str2/Replace the first str1 from row n to row N as str2

: N, $ S/str1/str2/g replace all str1 from row n to row N as str2

(N is a number. If n is., it indicates starting from the current row to the last row)

: % S/str1/str2/(equivalent to: G/str1/S // str2/) Replace the first str1 of each row with str2

: % S/str1/str2/g (equivalent to: G/str1/S // str2/g) replace all str1 in each row with str2

2. You can use # As the separator. The/in the middle will not be used as the separator.

: S # str1/# str2/# Replace the first str1/in the current row with str2/

: % S +/str11/str12/+/str21/str22 + (use + to replace/):/str11/str12/replace it with/str21/str22/

3. Delete ^ m from the file

Problem description: For line breaks, in windows, the line breaks (0a0d) are used, and in Linux, the line breaks (0a) are used. In this way, when you copy files on Windows to Unix, there will always be a ^ m. Please write a shell or C program that is used in UNIX to filter Windows files (0d. There are two ways to do this:

(1) run the command: CAT oldfilename | tr-d "^ V ^ m"> newfilename

(2) run the following command: sed-E "S/^ V ^ m //" oldfilename> newfilename

Note that in methods 1 and 2, ^ V and ^ m indicate Ctrl + V and CTRL + M. You must enter the file manually instead of pasting it.

Process in VIM: first use Vim to open the file, then press ESC, and then enter the command:

: % S/^ V ^ m //
: % S/^ m $/g

If the above method is useless, the correct solution is:

(1) tr-d "\ r" <old_file_name> new_file_name or cat oldfilename | tr-d "\ r"> new_file_name

(2) tr-d "\ 015" <old_file_name> new_file_name or cat oldfilename | tr-d "\ 015"> new_file_name

Strings A> B

4. Other skills

Use the S command to replace strings. The specific usage includes:

: S/str1/str2/use string str2 to replace str1 that appears for the first time in the line

: S/str1/str2/g replace all the str1 strings in the row with str2

:., $ S/str1/str2/g replace string str1 from the current row to the end of the body with string str2

: 1, $ S/str1/str2/g replace str1 with string str2

: G/str1/S // str2/g functions are the same as above

From the above replacement command, we can see that G is placed at the end of the command, which means to replace each appearance of the search string; without g, it means to only Replace the first appearance of the search string; G is placed at the beginning of the command to replace all rows that contain search strings in the body.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.