Vi/Vim is a legend in text editing, and it still has a place in the Arsenal of advanced programmers. Every Linux distribution includes Vim by default, and you can install Vim even if you are not a Linux system user.
Why are we still talking about this old software? Because it’s awesome! It includes lightweight keyboard shortcuts for almost everything, built-in expression search, and a powerful and stable plugin ecosystem. It even has other modern text editor features, such as nifty syntax highlighting. You’ll fall in love with this text editor before you know it.
Back on topic, today we are going to learn about text search and replace in Vim to help you get the most out of it. If you want to learn more about Vim operation, you can reply “information” in the background of the public account “Liangxu Linux”, there are e-books and videos I organized.
Demo file creation
In order to better explain the following, we need to prepare a demo file first. Here, I generated a text file packagelist.txt containing all the installed packages on your system as a demonstration file.
cat PackageList.txtCopy the code
Now, we open the text file in Vim. After that, we will demonstrate the following operations in this file.
vim PackageList.txtCopy the code
Vim search function
In Windows, the search function of many software can use CTRL +F to bring up the search box and then complete the subsequent search. But in Vim, we need to use the following command structure to implement the search function:
? <search_string>Copy the code
For example, if we want to search for python in packagelist. TXT, we can do this:
? pythonCopy the code
By default, the search will locate a match closest to the current cursor. If we want to go to the next/previous search result, please use the following button:
- N — Finds the next match
- N — Finds the last match
Note: do not forget to press the above buttons before using themEnter
The key. Otherwise, the search term will be changed!
That’s just the basic functionality of Vim search, and if you think that’s all it does, you’re dead in the water. Next, let’s try a more powerful search feature.
First give the search format:
/\<search_string>/>Copy the code
Here, \< indicates the beginning of the string, /> indicates the end of the string, and the middle is the string expression to search for.
As in the previous example, use n or n to navigate the search results.
The string expression in the middle supports not only direct input of keywords, but also the use of wildcards. For example, if we want to search for strings that begin with python-, we can use Python *. Where * means match any character.
/\<python*/>Copy the code
After the search is complete, Vim defaults to placing the cursor at the beginning character of the matching keyword.
Don’t like the default cursor position? Never mind, Vim allows us to change the default character position for cursor positioning.
/python/eCopy the code
Here, e is used to position the cursor at the end of the matching keyword.
Not only that, you can use the following command mode to position the cursor below or above the search results.
/<search_string>/+2
or
/<search_string>/-3Copy the code
You can also offset the cursor from the beginning/end of the search match. For example, the following command positions the cursor at three characters next to the beginning.
/<search_string>/s+3Copy the code
/<search_string>/b+3Copy the code
Here, s represents the beginning and +3 represents the third character from the beginning. B also stands for begin, and its function is similar to s. As can be seen from the screenshot, Vim will automatically translate B into S.
If you are interested in offsetting from the end of the search match, use e (end).
/<search_string/e-2Copy the code
Here is another great way to perform searches on text files.
:%s/<search_pattern>//gnCopy the code
What exactly does this order do? We’ll keep it in suspense, so read on. If you want to learn more about Vim operation, you can reply “information” in the background of the public account “Liangxu Linux”, there are e-books and videos I organized.
Vim string substitution
Find and replace is a common and necessary set of features for any text editor. Here’s a look at string substitution in Vim.
Vim uses the following command structure to implement the replacement functionality.
:<range> s/<search_string>/<replace_string>/<modifier>Copy the code
- Range – Defines the range within which the find and replace function is performed, with two different values
- % – Executes for the entire file
- < start _line > < end_line > – Performs an operation on a specific set of lines
- Search_string – The string to replace
- Replace_string – A new string to replace an old string
- Modifier – Determines the replacement action that has several different values
- G – Global replacement
- Gc – Require confirmation before each replacement
- Gn – Ignores the replace feature and highlights the search results.
For example, replace all Python strings with Python3 strings in the packagelist. TXT file.
:%s/python/Python3/gCopy the code
Simple, right? Let’s look at the following command.
:%s/python/Python3/gcCopy the code
As you can see, it asks you at the bottom if you want to do a replacement. At this point, you can choose some actions. So what do these guys stand for? Let’s look at them one by one.
- Y – Allows changes to be performed.
- N – Disables changes.
- A – Replace all of them.
- Q – Quit the mission.
- L – JUST replace this case. Then, quit.
- ^E (Ctrl + E) – Scroll up the screen
- ^Y (Ctrl + Y) – Scroll down the screen
Next, let’s try the scope field. In fact, we already used the % range (for the entire file range) in the previous example.
If we don’t want to find and replace the entire file, if we only want to do it on a few specific lines, what do we do? We can use a command format similar to the following:
: 200250 s/python/Python3 / gCopy the code
After doing this, Vim replaces all Python starting at line 200 and ending at line 250 with Python3.
This function is not limited to one word, but can also complete the substitution of multiple keywords. The command format is as follows:
:%s/<search_term_1>\|<search_term_2>\|<search_term_3>/<replace_string>/gCopy the code
For example, let’s want to change all python and py to Python3 in the text.
:%s/python\|py/Python3/gCopy the code
Case sensitive
All of the substitutions mentioned earlier in this article are case sensitive. For example, Python, Python, Python, etc are all different.
If you want to ignore case, you can add a \c to the end of the search string as follows:
/<search_term>\c
:%s/<search_term>\c/<replace_term>/gCopy the code
However, this setting is temporary and will not work when Vim is turned off. If you want to do this once and for all, you can also set case insensitive directly from the VIMRC file.
set ignorecaseCopy the code
There’s another interesting way to do this: smart capitalization.
set smartcaseCopy the code
Ignorecase forces Vim to ignorecase. However, smartCase will make Vim case-sensitive again if only capital letters are in the search term.
Note: If you want to enablesmartcase
First you need to set it upignorecase
。
conclusion
Vim can help you do a lot of things, practice with patience, I believe you will grow to love this ancient artifact.
All you see is true love. Why don’t you like it before you leave? Your “sanlian” is the biggest motivation for liang Xu’s continuous creation!
- Pay attention to the original public number “good Xu Linux”, the first time to get the latest Linux dry goods!
- Public number background reply [information] [interview] [resume] to get selected first-line big factory interview, self-improvement, resume and other information.
- Follow my blog: lxLinux.net