A text or source code editor based on a graphical interface, providing some functions such as text selection. What I'm trying to say is that probably most people don't think this is a feature. But this is not the case with command line-based editors like Vim. When you use Vim only with the keyboard, you need to learn specific commands to select the text you want. In this tutorial, we will discuss in detail the functionality of text selection and the flags functionality in Vim.
Before this, it should be noted that the examples, commands and instructions mentioned in this tutorial were all tested in the Ubuntu 16.04 environment. Vim version is 7.4.
Vim’s text selection function
We assume that you already have basic knowledge of the Vim editor (if not, you can read: https://www.jb51.net/os/Ubuntu/547230.html). You should know that the d command can cut/delete a line of content. If you want to cut 3 lines, repeat the command 3 times. But what if 15 lines need to be cut? Is repeating the d command 15 times a practical solution?
Obviously not. The best approach in this case is to select the lines you want to cut/delete and then run the d command. for example:
Suppose I want to cut/delete the first paragraph of the INTRODUCTION section in the screenshot below:
Then what I do is: put the cursor at the beginning of the first line, (make sure you exit the Insert mode) and press the V (ie Shift v) command. At this time, Vim will open view mode and select the first line.
Now, I can use the arrow key "Down" to select the entire paragraph.
This is what we want, right! Now just press the d key to cut/delete the selected paragraph. Of course, you can do anything to the selected text except cut/delete.
This brings us to another important question: what should we do when we don’t need to delete the entire row? In other words, the solution we just discussed is only suitable for situations where you want to operate on the entire row. So what if we only want to delete the first three sentences of the paragraph?
In fact, there is a corresponding command - just use lowercase v instead of uppercase V. In the example below, I use v to select the first three sentences of the paragraph:
Sometimes, the data you need to process consists of separate columns, and your need may be to select a specific column. Consider the following screenshot:
Suppose we only need to select the second column of text, which is the name of the country. In this case, you can place the cursor on the first letter of the column and press Ctrl v once. Then, press the "Down" arrow key to select the first letter of each country's name:
Then press the "right" arrow key to select this column.
Tips:If you selected a text block before and now want to reselect that text block, just press gv in command mode.
Use flags
Sometimes, when you are working on a large file (such as a source code file or a shell script), you may want to switch to a specific location and then return to the same line. This isn't a problem if the two rows are not far apart, or if you don't do this kind of thing very often.
However, if you need to frequently switch between the current position and some further rows, then the best way is to use flags. You only need to mark the current position, and then you can return to the current position from any position in the file through the mark name.
In Vim, we use the m command followed by a letter to mark a line (the letter represents the flag name, lowercase a - z can be used). For example ma. You can then use the command 'a (including the single quote on the left) to go back to the line marked with a.
Tips:You can use "single quote" ' to jump to the first character of the flag line, or use "backtick" ` to jump to a specific column of the flag line.
Vim’s flag function has many other uses. For example, you can mark a line first, then move the cursor to other lines and run the following command:
d'[logo name]
to delete everything between the current position and the flag line.
In the official Vim documentation, there is an important content:
Each file has some flags defined by lowercase letters (a-z). In addition, there are global flags defined by uppercase letters (A-Z) that define a location in a specific file. For example, you might be editing ten files at the same time. Each file can have the flag a, but only one file can have the flag a.
We've discussed the basic use of lowercase letters for Vim logos, and how convenient they are. The following excerpt explains it clearly enough:
Due to various limitations, the uppercase letter logo may not be as useful as the lowercase letter logo at first glance, but it can be used as a quick file bookmark. For example, open a .vimrc file, press mV, and exit. Next time you want to edit the .vimrc file, press 'V to open it.
Finally, we use the delmarks command to delete marks. For example:
:delmarks a
This command will remove a flag from the file. Of course, you can also delete the line where the flag is located, so that the flag will be automatically deleted.
Summarize
When you start using Vim as your preferred editor, features like the ones mentioned in this tutorial will be very useful tools and can save a lot of time. You have to admit, the text selection and marking features described here require almost no learning, just a little practice.
The above is the detailed content of How to use markers for text selection in the Vim editor. For more information, please follow other related articles on the PHP Chinese website!