LPIC-1 103.8 Perform basic file operations using vi
For the LPIC-1 or CompTIA Linux+ certification you will be expected to know the basics of editing files in vi or vim. If you know the basics then you will be able to learn more in your own time. The objective detail for the current exam 400-101 (LPIC-1) and LX0-103(CompTIA) are listed below.
Description: Candidates should be able to edit text files using vi. This objective includes vi navigation, basic vi modes, inserting, editing, deleting, copying and finding text.
- Navigate a document using vi
- Use basic vi modes
- Insert, edit, delete, copy and find text
Terms and Utilities:
vi /, ?
i, o, a
c, d, p, y, dd, yy
ZZ, :w!, :q!, :e!
The text editors vi or vim (Vi Improved) are very common in Unix, Linux and even OSX now in the MAC world. It may not at first be the most friendly as you have to become familiar with the hot keys to carry out tasks as there is no menu. I believe though that this makes the system quicker than menu driven editors like nano.
The three modes of Vi
Command Mode This mode will accept command, usually entered as a single letter. Such as i to insert or a to append text. Both these letters will take you from the command mode to inset mode. Press the ESC key to return to command mode once you have finished editing.
Ex Mode The ex mode will allow you to save files, quit vi, read in files save as and so on. To enter ex mode, first make sure we are in the command mode by using the ESC key, then from the command mode we can type the colon : . We are now in ex mode and we can then enter wq to write and quit.
Insert Mode is , as you can guess, are editing mode were the keys that we type appear in the document. The ESC key will allow us to return to the command mode.
Save and Quit
To exit the document we normally need to save and then quit. The following command entered in ex mode will help with these operations, for clarity the colon is included to show we are in ex mode.
- save :w
- force save :w!
- quit :q
- quit and discard changes :q!
- save and quit :wq
- save and quit : x
- save as w: filename
- revert to last saved version of the current document :e!
It is also possible to save and quit directly from the command mode using ZZ
From command mode we use the following commands to navigate our open document from command mode:
- Go to the end of the current line $
- Go to the start of the current line 0
- Go to line number x xG ie to go to line 1 then use 1G
- Go to the end of the document G
- Move one word forward w
- Move one word backwards b
- (hjkl) can often be replace with the navigational arrows on the keyboard
- One character left h
- One character right l
- Down one line j
- Up one line k
From the command mode we can use the following commands to control deleting of text from the current document:
- To delete the character at the cursor x
- To delete the character to the left of the cursor X
- To delete to the end of the line D or d$
- To delete to the start of the line d0
- To delete to the next letter s dts
- To delete the whole line use dd
- Similarly to d you may use c to clear which deletes text and takes you to Editing mode, to clear to the end of line c$
- To delete 3 lines, the current line and the two lines following) 3dd
- To delete the rest of the word form the current cursor position dw
- To delete three words from the current cursor position 3dw
Cut and Paste
- To copy the current word from the cursor yw
- To copy three words from the current cursor position 3yw
- Use p to paste after the cursor and P before
- Similarly yy or Y to copy the current line 3yy to copy 3 lines from the current line
- p pastes below P pastes above
From the command mode we can insert text in the following ways
- Insert at the current cursor position i
- Insert after the current cursor position a
- Insert at the start of the line (great for commenting a line out) I
- Insert at the end of the line A
- Insert a new line below the current line o
- Insert a new line above the current line O
Let the video step you through the process and please practice, practice , practice. It is worthwhile! Once you have seen this video do continue on this page where we can show some search and replace tricks to block indent text in VI
So you been busy writing a few scripts with you new found VI skills and you have notices in your haste you have omitted to use indents to help identify code blocks. If you want to add then in with VI it is not difficult.
- turn on line numbering if it is not already on :set number
- let’s say lines 5 to 22 should be indented :5,22s/^/ /
- Here we set a range of line 5 to 22- :5,22
- we search – s/
- for lines that begin with – ^/
- and replace with two spaces – /
- all together s/^/ /
- The carat or ^ represents the start of the line and we are inserting extra spaces at the start of the line
- Turn numbering off when you are finished :set nonumber
The video will demo this for you so please take a look