OPS145 Lab 5 Newversion: Difference between revisions

From Littlesvr Wiki
Jump to navigation Jump to search
(Replaced content with "Category:OPS145 The latest version is here: OPS145_Lab_5")
Tags: Replaced Visual edit
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[Category:OPS145]]
[[Category:OPS145]]
 
The latest version is here: [[OPS145_Lab_5]]
= !!! 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:<syntaxhighlight lang="bash">
vi ~/Downloads/SampleFiles/1984.txt
</syntaxhighlight>
 
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:<syntaxhighlight lang="bash">
vi ~/Downloads/SampleFiles/1985.txt
</syntaxhighlight>
* 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!'''
{{Admon/important|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:<syntaxhighlight lang="bash">
vi /home/yourusername/Downloads/SampleFiles/1984.txt
</syntaxhighlight>
* Move your cursor to the fourth line using the arrow keys.
* Press '''i''' - this will switch vi into '''insert mode'''. Depending on the version of vi: it may or may not tell you at the bottom that it's now in insert mode.
* Type in '''This is a new sentence!''' - this will insert that text at the cursor position.
* Press '''Escape'''. This will take vi into '''command mode'''.
* Save your changes using the ''':w''' command.
* Quit vi using the ''':q''' command.
* Use '''less''' to read 1984.txt and confirm that your changes have been saved.
 
== Append ==
The '''i''' command will switch vi into insert mode, and will keep the cursor where it is. Sometimes you need to insert some text at the end of a line, and because of your configuration the arrow keys won't work in insert mode. Try it:
 
* Open 1984.txt again using vi
* Move your cursor to the end of the line you typed in (over the exclamation mark), and switch to insert mode using '''i'''.
 
Note that the cursor is over the exclamation mark, but you will need to add text after it.
 
* Try to move the cursor to the right using the arrow key. If your configuration is the same as my defaults: vi will do something stupid.
* Quit vi without saving your changes.
 
There's another command which will switch vi into insert mode, but unlike '''i''': it will also move the cursor one character forward. That command is '''a''' (for append). Try it:
 
* Open 1984.txt again using vi
* Move your cursor to the end of the line you typed in (over the exclamation mark), and switch to insert mode using '''a'''.
* Type in " '''This is a second sentence. And a third.'''" You should end up with this:
 
[[File:ViInsertedTwoSentences.png|center]]
 
* Save your changes, exit vi, and confirm that your changes were saved correctly.
 
== Delete one character ==
Sometimes the delete and backspace keys work properly, but half the time they dont. A relatively easy way to delete some text is to use the '''x''' command, in command mode.
 
*Open 1984.txt again using vi
* Move your cursor to the space between the second and third sentence you added.
* Still in command mode: press '''x'''. This will delete the space.
* Press '''x''' several more times to delete the rest of the third sentence.
 
If you make a mistake (e.g. delete too much): quit vi without saving, and try again.
 
You should end up with this:
[[File:ViThirdSentenceDeleted.png|center]]
 
* Save your changes, exit vi, and confirm that your changes were saved correctly.
 
 
 
= 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 =

Latest revision as of 23:40, 10 February 2024

The latest version is here: OPS145_Lab_5