How To Search In Vim / Vi - TecAdmin
Có thể bạn quan tâm
Vim is a popular text editor that is commonly used in the programming world. One of the most important features of Vim is its search functionality. Vim’s search capabilities allow you to quickly locate specific text in a file, making it an essential tool for developers and anyone who works with text files.
In this beginner’s guide, we will explore how to search in Vim and some useful tips to improve your productivity.
AdvertisementBasic Search
To search for a specific text in Vim, you need to be in the normal mode, which is the default mode in Vim. Then, press the forward slash (/) key followed by the text you want to search. For example, if you want to search for the word “example”, you can do:
- Press the ESC key to return to normal mode
- Type “/”
- Type the search pattern “/search_string”
- Press Enter or Return key to perform search
After pressing Enter, Vim will highlight the first occurrence of the word “example” in the file. You can use the n key to navigate to the next occurrence of the search term and the N key to navigate to the previous occurrence.
Search and Navigation Shortcuts
Vim has several search and navigation shortcuts that can help you find the text more quickly. For example:
- Press n (in small letter) command to move to the next instance of your search term
- Press N (in capital letter) command to move to the previous instance
Case Insensitive Search
Vim provides several options to customize your search. One of the most commonly used options is the i option, which makes the search case insensitive. For example, to search for the word “example” regardless of its case, you can use the following command:
/example/iThis will match “example,” “Example,” “EXAMPLE,” and any other variations of the word.
Search Globally in File
Another useful option is the g option, which searches globally in the file. By default, Vim only searches in the current line, but with the g option, it will search the entire file. For example, to search for the word “example” globally, you can use the following command:
/example/gYou can combine multiple options by placing them together. For example, to search for the word “example” globally and case-insensitively, you can use the following command:
/example/giRegular Expressions
Vim’s search functionality also supports regular expressions, which are a powerful way to search for patterns in text. Regular expressions are a bit more complex than basic search, but they are worth learning as they can save you a lot of time and effort.
To use regular expressions in Vim, you need to use the forward slash (/) followed by a regular expression enclosed in two slashes. For example, to search for all words that start with the letter “a,” you can use the following regular expression:
/\<a\w*This regular expression matches any word that starts with the letter “a” and is followed by any number of letters or digits.
Search and Replace
Vim also allows you to search and replace text in a file. To replace the first occurrence of a search term with a new text, you can use the following command:
:s/search_term/new_textFor example, to replace the first occurrence of the word “example” with the word “sample,” you can use the following command:
:s/example/sample [/bash] To replace all occurrences of a search term with a new text, you can use the following command: :%s/search_term/new_text/gFor example, to replace all occurrences of the word “example” with the word “sample,” you can use the following command:
:%s/example/sample/gSearch Whole World Only
You can also search for a string that matches the whole world only. Which means the search does not match the substring of the string. For example, if you search for the string "sample", it doesn't not strings like "samples", "samplings" etc.
To do this first type forward slash (/), then type your search pattern between the backward slashes (\) like:
/\sample\ [/bash]Search History vim Vim
Vim keeps track of your search history, making it easy to repeat previous searches. To access your search history, use the "/" command followed by the up or down arrow keys. This will cycle through your previous searches, allowing you to quickly find what you need.
Fuzzy Finder
Vim's fuzzy finder is a powerful tool that can help you quickly find files and navigate your project directory. To activate the fuzzy finder, use the ":FZF" command followed by your search term. Vim will then display a list of files and directories that match your search term, allowing you to quickly navigate to the file or directory you need.
Conclusion
In conclusion, Vim's search functionality is a powerful tool for finding and manipulating text in your code or text files. By mastering these tips and tricks, you can become more efficient and productive when working with Vim. So next time you need to find text in Vim, try out these techniques to streamline your workflow.
Từ khóa » Vi In Linux Search
-
Searching And Replacing With Vi
-
How To Search In Vim / Vi - Linuxize
-
How To Find A Word In Vim Or Vi Text Editor - NixCraft
-
How To Search To Find A Word In Vim Or Vi Text Editor - PhoenixNAP
-
Search In Vim Editor: Linux, For Words
-
How To Search In Vim/Vi? - LinuxTect
-
How To Search In Vim - Linux Hint
-
How To Search In VI Editor - Fedingo
-
How To Search And Replace In Vi - Qualitest
-
How To Search In VIM/VI Editor? [VI/VIM Search] - MonoVM
-
Linux Vi Searching - Javatpoint
-
Searching And Replacing In Vi
-
08 Vim Tutorial - How To Search In Vi Editor - YouTube
-
How To Search Using Vi Linux?