Linux+: Linux Shell 25 – vi Editor (Part 2)

J

Jarret W. Buse

Guest
Linux+: Linux Shell 25 – vi Editor (Part 2)

In the previous article (Part 1), I covered the options and insertion commands for the vi editor. In this article, I will cover search abilities within the editor.

To perform a search, use the following commands:

  • /string – searches forward in the file for the value in place of string
  • ?string - searches backwards in the file for the value in place of string
  • /\<string\> - searches for string for an exact match, not part of another word. For example, ^<p\> would find “p = 2” but not the word “parts”
  • n – used to find the next occurrence of the search string
  • N – searches for the previous occurrence of the search string
  • fx – moves the cursor to the next occurrence of the letter 'x' on the same line
  • #fx - moves the cursor to the # occurrence of the letter 'x' on the same line
  • ; - goes to the next occurrence in the line
  • Fx – moves the cursor to the previous occurrence of the letter 'x' on the same line
  • #Fx - moves the cursor to the previous # occurrence of the letter 'x' on the same line
  • tx – moves the cursor to character before the next occurrence of the letter 'x' on the same line
  • #tx - moves the cursor to character before the next # occurrence of the letter 'x' on the same line
  • Tx – moves the cursor to character before the previous occurrence of the letter 'x' on the same line
  • #Tx - moves the cursor to character before the previous # occurrence of the letter 'x' on the same line
To use search strings there can also include patterns within the search as follows:
  • . - a period is used to match a single character
  • ^ - beginning of the line
  • ^A – finds the beginning of the line which starts with an 'A' (the letter can be changed to other letters)
  • $ - matches the end of a line
  • [abc] – matches a word which contains any of the letters 'a', 'b' or 'c' (the letters can be changed to other letters, even adding or reducing the number)
  • \ - turns off the special meaning of the following character. For example, “\$” will match a dollar sign and not the end of a line
  • \# - matches a number 0-9
  • * - wildcard to represent 0 or more characters
  • + - wildcard to represent 1 or more characters
  • ? - wildcard to represent 0 or 1 characters
  • string1|string2 – match either string1 or string2
  • a.z – matches any word that starts with an 'a' followed by any character and the letter 'z' (the letter can be changed to other letters)
  • ^.$ - the carat (^) represents the beginning of the line, the period (.) is a single character, and the dollar sign ($) is the end of the line. The search string shows the search for a line which contains one character
There also exists commands to get information about the file as shown:

  • ctrl+g – lists file name, number of lines, current position of cursor in file
  • :set list – shows end of line (eol) and tabs
  • :set nolist - hides end of line (eol) and tabs
  • :args – shows the command-line arguments used when starting vi
Once a file has been edited, and possibly modified, there are important commands to close vi and perform certain tasks:

  • ZZ – saves changes and quits vi
  • :wq – saves changes and quits vi
  • :w – writes file to disk, but does not quit
  • :w! - saves the file overriding user permissions
  • :w filename – writes changes to a file named filename and does not quit
  • :q! - quits without saving and changes since last save
  • :qa – quits and closes all open files in vi
  • :e filename – opens a new vi session with filename. If the file does not exist, it is created, otherwise the file is edited
Both vi and vim use configuration files. The files are stored in a special place depending on which you use. The locations are as follows:

  • vi
    • ~/.exrc
  • vim
    • ~/.exrc
    • ~/.vimrc
    • ~/.gvimrc
    • ~/.vim/
Many of these commands can seem overwhelming to remember. Using vi or vim a bit will reinforce the commands and help you when taking the Linux+ Certification exam. On some exams, when a question is missed on a subject, you may likely get another question on the same subject. If enough of these are missed, you will not pass the exam.
 

Attachments

  • slide.jpg
    slide.jpg
    49.4 KB · Views: 27,240

Members online


Latest posts

Top