1. Intro
It's been almost 30 years since the Visual Improved editor (VIM in short) was born and it's still one of the most popular text editors on Unix type operating systems. While it has the most simple and clean interface it's proven quite difficult for first time users to find their way around it's usage.
Legend has it a dev once said: "I've been using vim for 2 years now, mostly because I cannot figure out how to exit it!"
In this article we walk through the ways how to search certain words, or strings in any text file.
2. Open VIM
As always, we use simple examples to go through the process. In my experience that makes it easier to understand the logic behind the described procedures. First, we open the ssh server configuration file that is most likely already available even on very basic linux, bsd or mac installations.
[root@localhost ~]# vim /etc/ssh/sshd_config
3. How to Search
Always use these commands in normal mode. When you open the editor it automatically starts up in normal mode, which is designed for command execution. You can type, delete text in insert mode. To get into that mode simply push the "i" key on your keyboard. To switch back to normal mode, push the ESC key.
As a rule of thumb, if unsure, always puch the ESC key so you make sure you are in the right mode!
# Find the string 'fox' forward from the actual position: /fox
# Find the string 'fox' backward from the actual position: ?fox
# Highlight results :set hlsearch # To disable it :set nohlsearch
# Case insensitive search, that will include 'fox' and also 'Fox': :set ignorecase
# Search for the exact word: fox. That means 'SuperFox' is excluded, as the 'fox' string is not a separate word /\<fox\>
# To get the search history use the following keys. You can navigate in the list with the arrow keys (or j, k), and use ENTER to do repeat the search! q/
# Tip: search for the word at the actual cursor position: Forward search: * Backward search #
4. TIPS
VI vs VIM
Vi is the old 'VIsual' editor, while VIM stands for it's successor, the 'Visual IMproved' app.
On Debian/Ubuntu based system the vi command opens the same full-fledged VIM application, so vi and vim are interchangeable there. However, be aware that on RHEL/CentOS machines the vi command is mapped to the old, dumb version of the command, while vim opens the full application up that we need.
Generally speaking, most of the linux distributions contain both (if you haven't already, install the improved version on your box)
#On Debian/Ubuntu: apt-get install vim # On RHEL./CentOS: yum install vim
Comments