Home > Articles

Basic vi Skills

  • Print
  • + Share This
This chapter is from the book

Exam Prep Questions

  1. What is the default editor for Linux systems?

    1. emacs

    2. joe

    3. vi

    4. pico

    Answer C is correct because vi is in nearly every distribution of Linux that exists. Answer A is incorrect because, even though emacs is very well known, it's considered an add-on to Linux. Answer B is incorrect because joe is a specialized editor and not normally included in a distribution by default. Answer D is incorrect because pico is used only for document-style text editing, and is not the default editor.

  2. Which of the following saves and exits a file you are editing in vi? (Choose all that apply.)

    1. :x

    2. :q!

    3. Shift+ZZ

    4. zz

    5. :wq

    Answers A, C, and E are correct because they save and exit a given file in vi. Answer B is incorrect because it forces a nonsaving exit to a modified buffer or file. Answer D is incorrect because only a shifted pair of z characters saves and exits a file properly.

  3. After opening a file with vi, you want to navigate to the end of the file as quickly as possible. Which of the following, when performed in Command mode, accomplishes this?

    1. Ctrl+PgDn

    2. Shift+G

    3. Meta+End

    4. Shift+End

    Answer B is correct because it takes the cursor to the end of the file. Answer A is incorrect because Ctrl+PgDn does nothing in vi. Answer C is incorrect because it moves to the end of the current line. Answer D is incorrect because it does nothing in vi.

  4. On the line below, enter the vi command that would open the file mytestfile.txt and cause nonprinting line numbers to appear to the left of all file contents:

  5. __________________________________________________________________

    The answer is vi +"set number" mytestfile.txt.

    Alternative possible correct answers are

    vi mytestfile.txt +"set number"
    vi mytestfile.txt "+set number"
  6. While editing a file with vi, you press Esc to enter Command mode and then use the keystrokes 3+J+4+L. What is the result?

    1. Three columns left and four lines down

    2. Three columns right and four lines up

    3. Three lines up and four columns left

    4. Three lines down and four columns right

    Answer D is correct because the J key denotes the down arrow and the L character denotes the right arrow. Answers A, B, and C are incorrect because they don't use the correct keystrokes.

  7. Which of the following commands opens a file named file1.txt with nonprinting line numbers displayed in vi?

    1. vi /set number file1.txt

    2. vi +"set number" file1.txt

    3. vi < .exrc file1.txt

    4. vi "se nu" file1.txt

    Answer B is correct because it turns on the line numbering. Answer A is incorrect because it has a slash (/) instead of a plus symbol (+). Answer C is incorrect because it either produces a syntax error or just edits the .exrc file. Answer D is incorrect because it's missing the necessary plus symbol (+).

  8. You want to edit a file with vi and have the word string1 highlighted if found. Which of the following accomplishes this in one command?

    1. vi +/"string1" file

    2. vi /string1 file

    3. vi +/string1 file

    4. vi --find string1 file

    Answer C is correct because it searches for and highlights the string string1 properly. Answer A is incorrect because it has added quotation marks around the search string. Answer B is incorrect because it's missing the necessary plus symbol. Answer D is incorrect because there is no --find option for vi.

  9. While editing a file in vi, you need to display the permissions for the normal files in the current directory. Which command accomplishes this from Command mode?

    1. :x "ls –l"

    2. :e! ls -l

    3. Shift+L+S+-+L

    4. :! ls -l

    Answer D is correct because it starts a subshell and displays the normal files in the current directory. Answer A is incorrect because it exits the file and ignores the ls command. Answer B is incorrect because it attempts to edit all the files returned by the ls command and errors out. Answer C is incorrect because it just starts displaying the text on the screen in vi.

  10. You'll be editing the files in /project1 by executing vi from that location. You want a set of options to always be set for this project, and not other vi sessions. Which of the following files would you edit to best accomplish this?

    1. /project1/.exrc

    2. ~/.vimrc

    3. .virc

    4. /etc/editorrc

    Answer A is correct because it configures vi with the necessary options only if executed from the /project1 directory. Answer B is incorrect because the .vimrc file in question is located in the current user's home directory, not the project1 directory. Answer C is incorrect because the file named is a fake. Answer D is incorrect because no such file as /etc/editorrc exists.

  11. While in vi, you need to search and replace all instances of the string snark with FARKLE in the current file. Write the answer on the line below, including everything necessary to accomplish this from Command mode:

  12. __________________________________________________________________

    The answer is :%s/snark/FARKLE/g. There are no alternative correct answers.

  • + Share This
  • 🔖 Save To Your Account