Home System Tutorial LINUX Search techniques for vim editor in Linux

Search techniques for vim editor in Linux

Feb 14, 2024 pm 10:42 PM
linux linux tutorial linux system video player linux command shell script embeddedlinux Getting started with linux linux learning

The vim editor is one of the most advanced text editors in Unix systems. It supports almost all file types and is also called a programmer's editor. In this tutorial, we will learn how to perform search operations in the vim editor to quickly find the text we need.

Search techniques for vim editor in Linux

But before that, please note that all examples, commands, and instructions mentioned in the article were tested under Ubuntu 14.04 and Vim 7.4.

Basic search operations in Vim

When you open a file in Vim and want to search for a specific word or template, the first step is to exit insert mode by pressing the Esc key (if you are in insert mode). Then type / followed by the word or search pattern you want to search for.

For example, if the word you want to search for is linux, the following figure shows the search command at the bottom of the Vim window: Search techniques for vim editor in Linux

After hitting the Enter key, you will see that Vim will place the cursor on the first line containing this word found starting from the cursor position in insert mode. If you have just opened a file and started searching immediately, the search will start from the first line of the file.

If you want to move to the next position containing the searched word, press the n key. When you have traversed all the locations of the searched templates, continue to press the n key. Vim will repeat the search operation, and the cursor will return to the position where the first search result appeared.

Search techniques for vim editor in Linux

When traversing the search results, if you want to return to the previous match, press N (i.e. shift n). At the same time, it is worth noting that at any time, you can enter ggn to jump to The first matching point, or GN to jump to the last one.

When you happen to be at the bottom of the file and want to search in reverse, use ? instead of / to start the search. The picture below is an example: Search techniques for vim editor in Linux

Customize your search

1. Highlight search results

Although it is simple to jump from the matching point of the searched word or pattern to another place through n or N, it would be more user-friendly if the matching point could be highlighted. For example, please see the attached screenshot:

Search techniques for vim editor in Linux

This can be achieved by setting the hlsearch variable, for example, executing the following command in normal/command line mode:

:set hlsearch
Copy after login

2. Make the search case-insensitive

Searching in Vim is case-sensitive by default. This means that if I search for linux, Linux will not match. However, if this is not how you want to search, you can make the search case-insensitive using the following command:

:set ignorecase
Copy after login

So when I set the ignorecase variable and then use the command mentioned earlier to search for linux, the location of linux will also be highlighted.

Search techniques for vim editor in Linux

3. Smart case search

Vim provides a feature where the editor will be case-sensitive only if the word/template to be searched for contains uppercase letters. To implement this functionality, you must first set ignorecase and then set the smartcase variable.

:set ignorecase
:set smartcase
Copy after login

For example, if a file contains both LINUX and linux, when the smart case search function is turned on, if you use /LINUX to search, only the word LINUX will be searched. On the other hand, if you search for linux, the search results will be matched regardless of case.

4. Progressive search

Just like Google, which displays different search results as you enter the query string (the string is constantly updated with each character you enter), Vim also provides progressive search. To use this feature, you must execute the following command before searching:

:set incsearch
Copy after login

Some cool tips for searching in Vim

There are a few other search-related tips you may find useful.

let's start! If you want to search for a word in a file but don't want to type it, just move your cursor under the word and press * (or shift 8). If you want to start a partial search (for example, search for both in and terminal), then you need to move the cursor under the word (in this case, in) and then press g* on the keyboard (press g once and then Keep pressing * ).

Note: If you want to search in reverse, press # or g#.

Here is a video, you can watch it:

Video Player

00:00

03:33

下一个,只要你想要,你可以获得所有被搜索单词或模式匹配处所在的行和行号的一个列表。这可以在你开始搜索后通过按 [I 来实现。如下图是一个列表结果如何在 Vim 窗口底部被分组和显示的例子:

Search techniques for vim editor in Linux

接下来,你可能已经得知,Vim 默认是环形搜索的,意味着在到达文件结尾处(或者被搜索单词的最后一处匹配)时,如果继续按 “搜索下一个” 会将光标再次带回第一处匹配处。如果你希望禁止环形搜索,可以使用如下命令:

:set nowrapscan
Copy after login

再次开启环形搜索,使用如下命令即可:

:set wrapscan
Copy after login

最后,假设你想要对文件中已经存在的单词做一点小小的修改,然后对修改后的单词执行搜索操作,一种方法是输入 / 与要搜索的单词。但是如果这个单词又长又复杂,那么可能需要一点时间来输入它。

一个简单的办法是将光标移到你想要略微修改的单词下,按/ 之后再按Ctrl +r 最后按 Ctrl块 + w 。这个在光标下的单词不仅仅会被拷贝,也会被复制到/ 后,允许你对它进行修改并且继续进行搜索操作。

如果想要获得更多小技巧(包括如何使用鼠标来使在 Vim 中的操作变得简单),请前往 Vim 官方文档。

在本教程中,我们介绍了如何在vim编辑器中进行搜索和替换,包括使用斜杠和点方法,使用冒号和s命令,以及使用全局命令。我们还学习了如何设置大小写敏感或不敏感,以及如何搜索整个单词。vim编辑器提供了强大而灵活的搜索功能,可以帮助我们提高编辑效率。

The above is the detailed content of Search techniques for vim editor in Linux. 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 start apache How to start apache Apr 13, 2025 pm 01:06 PM

The steps to start Apache are as follows: Install Apache (command: sudo apt-get install apache2 or download it from the official website) Start Apache (Linux: sudo systemctl start apache2; Windows: Right-click the "Apache2.4" service and select "Start") Check whether it has been started (Linux: sudo systemctl status apache2; Windows: Check the status of the "Apache2.4" service in the service manager) Enable boot automatically (optional, Linux: sudo systemctl

What to do if the apache80 port is occupied What to do if the apache80 port is occupied Apr 13, 2025 pm 01:24 PM

When the Apache 80 port is occupied, the solution is as follows: find out the process that occupies the port and close it. Check the firewall settings to make sure Apache is not blocked. If the above method does not work, please reconfigure Apache to use a different port. Restart the Apache service.

How to monitor Nginx SSL performance on Debian How to monitor Nginx SSL performance on Debian Apr 12, 2025 pm 10:18 PM

This article describes how to effectively monitor the SSL performance of Nginx servers on Debian systems. We will use NginxExporter to export Nginx status data to Prometheus and then visually display it through Grafana. Step 1: Configuring Nginx First, we need to enable the stub_status module in the Nginx configuration file to obtain the status information of Nginx. Add the following snippet in your Nginx configuration file (usually located in /etc/nginx/nginx.conf or its include file): location/nginx_status{stub_status

How to set up a recycling bin in Debian system How to set up a recycling bin in Debian system Apr 12, 2025 pm 10:51 PM

This article introduces two methods of configuring a recycling bin in a Debian system: a graphical interface and a command line. Method 1: Use the Nautilus graphical interface to open the file manager: Find and start the Nautilus file manager (usually called "File") in the desktop or application menu. Find the Recycle Bin: Look for the Recycle Bin folder in the left navigation bar. If it is not found, try clicking "Other Location" or "Computer" to search. Configure Recycle Bin properties: Right-click "Recycle Bin" and select "Properties". In the Properties window, you can adjust the following settings: Maximum Size: Limit the disk space available in the Recycle Bin. Retention time: Set the preservation before the file is automatically deleted in the recycling bin

How to restart the apache server How to restart the apache server Apr 13, 2025 pm 01:12 PM

To restart the Apache server, follow these steps: Linux/macOS: Run sudo systemctl restart apache2. Windows: Run net stop Apache2.4 and then net start Apache2.4. Run netstat -a | findstr 80 to check the server status.

How to optimize the performance of debian readdir How to optimize the performance of debian readdir Apr 13, 2025 am 08:48 AM

In Debian systems, readdir system calls are used to read directory contents. If its performance is not good, try the following optimization strategy: Simplify the number of directory files: Split large directories into multiple small directories as much as possible, reducing the number of items processed per readdir call. Enable directory content caching: build a cache mechanism, update the cache regularly or when directory content changes, and reduce frequent calls to readdir. Memory caches (such as Memcached or Redis) or local caches (such as files or databases) can be considered. Adopt efficient data structure: If you implement directory traversal by yourself, select more efficient data structures (such as hash tables instead of linear search) to store and access directory information

How to solve the problem that apache cannot be started How to solve the problem that apache cannot be started Apr 13, 2025 pm 01:21 PM

Apache cannot start because the following reasons may be: Configuration file syntax error. Conflict with other application ports. Permissions issue. Out of memory. Process deadlock. Daemon failure. SELinux permissions issues. Firewall problem. Software conflict.

How debian readdir integrates with other tools How debian readdir integrates with other tools Apr 13, 2025 am 09:42 AM

The readdir function in the Debian system is a system call used to read directory contents and is often used in C programming. This article will explain how to integrate readdir with other tools to enhance its functionality. Method 1: Combining C language program and pipeline First, write a C program to call the readdir function and output the result: #include#include#include#includeintmain(intargc,char*argv[]){DIR*dir;structdirent*entry;if(argc!=2){

See all articles