Home System Tutorial LINUX How to use markers for text selection in the Vim editor

How to use markers for text selection in the Vim editor

Jan 02, 2024 pm 10:37 PM
vim Select text

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:

How to use markers for text selection in the Vim editor

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.

How to use markers for text selection in the Vim editor

Now, I can use the arrow key "Down" to select the entire paragraph.

How to use markers for text selection in the Vim editor

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:

How to use markers for text selection in the Vim editor

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:

How to use markers for text selection in the Vim editor

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:

How to use markers for text selection in the Vim editor

Then press the "right" arrow key to select this column.

How to use markers for text selection in the Vim editor

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to add the Escape key to your iPad keyboard How to add the Escape key to your iPad keyboard Apr 16, 2023 pm 01:43 PM

How to Remap CapsLock to ESC on iPad Keyboard Are you ready to make CapsLock the ESC key on your iPad? Here's all you need to do: Open the Settings app on your iPad Go to "General" then go to "Keyboard" Go to "Hardware Keyboard" Select "Modifier Keys" Select "CapsLockKey" and select "Escape" as Modifier Keys Now you're ready to try out the new hardware ESC key on your iPad by pressing CapsLock. Go to any application that uses the Escape key and you can test it immediately, such as vi/vim. Now you can use a physical keyboard from

How to use Vim to delete even or odd lines in Linux How to use Vim to delete even or odd lines in Linux May 22, 2023 pm 05:07 PM

The method of deleting even-numbered lines is as follows: :g/^/+1d The :gbobal command is used above. The gbobal command format is as follows: :[range]global/{pattern}/{command}global command is actually divided into two steps: first Scan all the lines within the range specified by [range] and mark the lines matching {pattern}; then execute the {command} command on the marked lines in sequence. If the marked lines are marked during the command operation on the previous matching lines, If you delete, move or merge, the mark will automatically disappear without executing the {command} command on the line. {command} can be an ex command or separated by |

A brief analysis of 3 methods to install Vim on Alpine Linux A brief analysis of 3 methods to install Vim on Alpine Linux Mar 24, 2023 pm 02:28 PM

In PHP development, using Vim is very common. However, you may encounter some problems installing Vim in Alpine Linux. This article will share how to install Vim on Alpine Linux.

Linux Tips: Cancel automatic indentation when pasting in vim Linux Tips: Cancel automatic indentation when pasting in vim Mar 07, 2024 am 08:30 AM

Preface: vim is a powerful text editing tool, which is very popular on Linux. Recently, I encountered a strange problem when using vim on another server: when I copied and pasted a locally written script into a blank file on the server, automatic indentation occurred. To use a simple example, the script I wrote locally is as follows: aaabbbcccddd. When I copy the above content and paste it into a blank file on the server, what I get is: aabbbcccddd. Obviously, this is what vim does automatically for us. Format indentation. However, this automatic is a bit unintelligent. Record the solution here. Solution: Set the .vimrc configuration file in our home directory, new

How to save and exit vim How to save and exit vim Aug 01, 2023 am 10:47 AM

How to save and exit vim: 1. Use shortcut keys to save and exit; 2. Use a separate command to save and exit; 3. Use automatic commands to save and exit; 4. Use shortcut keys to save the file.

Learn all aspects of Vim split-screen operation in 5 minutes Learn all aspects of Vim split-screen operation in 5 minutes Aug 03, 2023 pm 03:20 PM

Vim's split-screen function is implemented by splitting windows, which is a great tool for improving work efficiency. Whether we want to display two files at the same time, or display two different locations of a file at the same time, or compare two files side by side, etc., these can be achieved through split screen, which is very convenient for code comparison and copy and paste.

How to make VIM support Nginx .conf file syntax highlighting function How to make VIM support Nginx .conf file syntax highlighting function May 15, 2023 pm 12:10 PM

Manually modify the syntax file for downloading the nginx configuration file: nginx.vimwgethttp://www.vim.org/scripts/download_script.php?src_id=14376-onginx.vimCopy the file to the /usr/share/vim/vim74/syntax directory (It can also be a single-user directory ~/.vim/syntax/). Modify vim/usr/share/vim/vim74/filetype.vim to add aubufread,bufnewfile/etc/nginx/*,/usr/local/n

Detailed introduction to the encryption and decryption methods of Vim text in CentOS Detailed introduction to the encryption and decryption methods of Vim text in CentOS Dec 31, 2023 pm 02:49 PM

CentOS uses vim/vi to encrypt and decrypt files 1. Use vim/vi to encrypt: Advantages: After encryption, if you don’t know the password, you cannot see the plain text, including root users; Disadvantages: It is obvious that others know the encryption , it is easy for others to destroy encrypted files, including content destruction and deletion; I believe everyone is familiar with the vi editor. There is a command in vi to encrypt files. For example: 1) First, in the root master Create an experimental file text.txt under the directory /root/: [root@www~]#vim/vitext.txt2) Enter the editing mode, press ESC after entering the content, and then enter: X (note the capital X), Enter; 3)

See all articles