Show
This content has been archived, and is no longer maintained by Indiana University. Information here may no longer be accurate, and links may no longer be available or reliable. You can go to a particular line or word in a file using vi in several ways:
Vim – short for Vi Improved – is a powerful open-source command-line text editor clone of the good old vi editor. It is highly configurable with an extensive manual and ships with a wealth of features, including syntax highlighting and color codes, comprehensive plugin support, and search and replace, to mention a few. Searching text or a string by manually scrolling up and down can be a daunting and time-consuming undertaking. Thankfully, the vim editor has a faster and convenient way of doing this. Main modes provided by VimVim provides three main modes: command mode, insert mode, and visual mode. By default, the Vim editor is first launched in command mode. In this mode, key-presses are inactive, and a user cannot insert text or modify the file. However, you can navigate up and down, left and right, using the following keys: k – Moves up one row. (Equivalent of Arrow up key ) j – Moves down one row. (Equivalent of Arrow down key ) l – Navigates one character to the right or moves forwards. (Equivalent of Arrow right key) h – Navigates one character to the left or moves backward. (Equivalent of Arrow down key ) You can also prefix the keys with a numeric letter to move up or down a certain number of rows or move forward and backward a certain number of characters. For example, 6k – moves up 6 rows 4j – Moves down 4 rows The insert modeThis mode allows you to type in text and make changes to a text file as you deem fit. The insert mode can be accessed from the command mode by pressing the following keys. The ‘i’ key ( insert ) allows you to insert a character at the cursor’s current position. The ‘a’ key ( append ) – This moves the cursor one character to the right and gets you to insert mode. The ‘o’ key – This creates a new line below the current row and switches to insert mode. The visual modeThe visual mode is usually used to highlight text, similar to clicking and dragging with a mouse. To start making a text selection, simply type ‘v’ then leverage the arrow keys to highlight text. Perform a basic search in VimTo search text, you need to be in command mode. If you are in insert mode, simply press the ‘ESC’ key. To perform a basic search of the string or text that you want, move to the beginning of the file and simply press the forward-slash ( / ) key. Then type the search string and press ENTER on the keyboard to start searching. The forward-slash ( / ) key performs a forward search. It searches for the string or pattern from the current cursor position right up to the end of the file. To search for the next pattern, simply press the letter n on the keyboard. To search backward, press the question mark symbol ( ? ), type the search string, and press ENTER on the keyboard. This searches the string from the current cursor position to the start of the file. NOTE:The search operation searches for a string or pattern and not the entire word. For instance, if you search for the string ‘form’, the search functionality will return results even when the string is present in more powerful or whole words such as ‘formal’ and ‘uniform.’ Search for a complete wordTo search for a complete word, begin by pressing the / or? Symbol. After that, type the \< symbol to mark the beginning of the word to be searched then /> to signify the end of the search word. Then finally, hit ENTER to commence the search. For instance, to perform a forward search of a pattern, run: Here, we are searching for the complete word – ssh – in the /etc/ssh/sshd_config configuration file. Ignore case sensitivityBy default, Vim is case sensitive, and so is the search pattern. To ignore case sensitivity, suffix the search pattern with the \c operand. For example, /path\c searches for any occurrence of the string ‘path’, whether upper or lowercase. Another way you can achieve this is to press the ESC key followed by a whole colon followed by the text set ignorecase or the short form, set ic. Next, hit the ENTER key. After that, type the ( / ) followed by the search pattern. In the example below, notice how we get the uppercase string of the pattern PATH. Search the history of searched stringsVim keeps a history of all the search items. To view the strings searched, simply type / or ? in command mode and press either the Arrow up or Arrow down keys to scroll through previously searched patterns. Wrapping upThat summarizes how you can search for strings, patterns, or complete words on the vim editor. The vi or vim is a console based text editor in Linux. It is probably the most useful of Linux applications, in that you can quickly view and edit the text files right at the command shell. When viewing the text files, one of the useful features is the ability to search for a word or some text in the file. The vi editor provides some powerful search and replace features. We will look some of the commonly used search feature that come in handy. search single word or stringYou can search for a single word or a character string by using the forward slash (/). A character string is one or more character that occur in succession and can be a single word or part of a word. It can contain letter, number, spaces, punctuations etc. When the file is open in the vi editor, change the editor to the normal or command mode by pressing Esc (or Escape) key. The command mode is the default mode. When in command mode type the forward slash (/) and followed by the string (or word) you want to search. You can see the string you typed at the bottom left side of the vi editor or the screen. Once you have typed in the string you want to search, hit enter and the vi editor will place the cursor at the first occurrence of that string in the editor and highlight the string. So, if you wanted to search for the string apple, then type /apple and hit enter. After finding the first occurrence of the string, you can type n to find the next occurrence of the same string in the editor. The editor will place the cursor at the next occurrence from the current cursor location. Similarly, you can find the previous occurrence by typing N (n in caps). search multiple strings or wordsSometimes you would want to search for multiple strings such that it matches either of them. Let’s say you want to search for the occurrence of either apples or oranges. You can separate the strings in the query string by using the pipe (|) character. When the file is open in command mode in the vi editor, type forward slash (/) and the type the strings separated by the | character. So in this case, you will type /apples\|oranges into the editor. You will need to escape the | character so that the editor identifies it as a separator and not use it as a literal character. search just the word or the whole wordAs you might have already noticed, the default search will match any part of the string which makes it difficult to search for whole words in the file. In order to match the beginning of a word you need to use the less than (<) character and to match the end of the word you will use the greater than (>) character. Of course, you will need to escape both < and > characters with the backslash. So, as always type forward slash (/) and then the backslash and < character, followed by the word you want to search. You can finish the query with another backslash and then the > character. So, to search for the word apple, you will type /\<apple\>. This will find all occurrences of apple but will not match apples. advanced search and special charactersThere are several special characters that you can use to further filter or narrow your search patterns. We will see some of the commonly used expressions.
You can use the above characters together as well. An example of using * and . with the bracket notation can be something like: /a[p]*le. that will match strings such as apples, apppples, ales etc. Another example would be /a[p-s]*le that will match strings like apple, aprole, appoorrle etc etc. forward/backward searchWe covered the way to search in a forward fashion in the previous sections. The use of forward slash (/) at the start of the search term denotes that the search is to be done in a forward fashion with in the file. You can always use n and N characters to find matched strings in a forward or backward fashion respectively. Alternatively, if you wish perform a backward search instead of forward search then you should use the ? (question mark) instead of the / (forward slash) when performing the search. So, the term ?apple will search backwards for the string apple in the file. The n and N now will now traverse in opposite directions compared to the forward search. case insensitive searchYou must have already noticed that search in the vi editor is case sensitive by default. So, if you want to do case insensitive searches you will need to turn it off. You can do that by using the command :set ic when in command mode. You can turn it back on using the command :set noic again when in the command mode. special charactersYou know by now that there are several characters that have special significance in the search term. Some of these characters are / (forward slash), ? (question), ^ (caret), $(dollar), . (dot), * (astericks), | (pipe) etc. You will need to treat these characters differently if you want use them in the search string. You can do so by escaping them by using the backward slash (\). So, to search for the term question? , you will use the term /question\? which will find all the strings that match question?. The backward slash before a character will treat that character as a literal character rather than a meta character. |