OPS145 Lab 5 Newversion

From Littlesvr Wiki
Jump to navigation Jump to search


!!! THIS PAGE IS NOT READY YET !!!

One of the most common tasks a system administrator will do is to modify some configuration. Almost all services on Linux servers are configured using plain text configuration files.

So far in the course you've been using a graphical text editor. But since Linux servers don't have graphical user interfaces (GUIs): you have to learn to edit text files on the command line.

There are several common applications for command line text file editing. We're going to learn vi. Even though vi is a frustratingly annoying application to use: it's the one available on any Linux system.

vi

As you go through your Linux courses: try to think of vi as a necessary evil. We're going to learn the absolute basics necessary for you to be able to accomplish any task a systems administrator would need it for.

Modes

One of the confusing things about vi is that it can be in one of two modes at any time: command mode, and insert mode.

In insert mode it functions more or less the way you'd expect a text editor to function, except depending on your setup: the arrow keys, delete key, and backspace key might do weird things. Otherwise: when you press a letter key: that gets inserted into the text.

In command mode the keypresses on your keyboard are interpreted as instructions/commands to vi, and different keys are used for different commands.

For example: in insert mode pressing x on your keyboard will put the letter x into the text where the cursor is, but in command mode pressing x on your keyboard will delete the character underneath the cursor.

Often times this creates such confusion (especially if you don't look at the screen when you're typing) that you have to abandon what you've been trying to do and start over. A good portion of this lab is spent on recovery from mistakes.

Quit without saving

  • Open one of the text files you donwloaded in lab2 in vi:
    vi ~/Downloads/SampleFiles/1984.txt
    

Similarly to the less command: you can move around using the arrows, home/end, and PgUp/PgDown keys. Unlike the less command: there is a cursor. But if you try to type in some text: depending on which keys you use either nothing will happen or something weird will happen.

  • Move the cursor to an empty like, and type in hello. Whatever happend in vi is probably not what you wanted.
  • Press ctrl+s. This will not save your document. Try ctrl+w, and ctrl+q. Neither will quit out of vi.
  • Try the backspace and delete keys. They may or may not work, depending on various complicated factors.

At this point you probably made some unintended changes to your document, and you don't know how to undo them. The best thing to do when you're in this situation is to quit vi without saving:

  • Press the Escape key, then colon (:), then q, then exclamation mark (!), then Enter. From now on in the instructions I'll use a shorthand notation like this: Esc+:q!

This will quit vi without saving any changes you made to the document.

  • You will also want to do this if you open vi with no filename, or the wrong filename. Try it:
    vi ~/Downloads/SampleFiles/1985.txt
    
  • You could put in some text, and save it: but it would be saved in the wrong file. So quit without saving using Esc+:q!
Important.png
Never use ctrl+z
If you've taken this course with someone else: you may have learned about ctrl+z. You are not qualified to understand what that does, and using it will cause you more trouble than good. Just forget that that key combination exists!

Switching modes

  • Open 1984.txt again, use an absolute path for practice:
    vi /home/yourusername/Downloads/SampleFiles/1984.txt
    
  • Move your cursor to an empty line using the arrow keys.


Also:

  • run+quit vi. no :w filename
  • command mode, insert mode
  • commands:
    • i
    • a
    • u
    • x
    • /
    • w
    • wq
    • q!

First half of the course: review