Introduction to Linux: Class 2 Notes

Text Editors

vi

vi tutorial

'vi testvi'

Starts in command mode

Press 'i' to enter insert mode

Type. Watch letters appear. Ooh. Type at least 4 lines (lines are separated by enter)

Use arrow keys to navigate (not all vi's allow this)

Backspace and Delete will work (not all vi's allow this)

Press 'Escape' to return to command mode

Use 'h' to go left, 'l' to go right, 'j' to go down, 'k' to go up (original way of navigating)

To go to the beginning of a line, press '^' (this comes from regular expressions)

To go to the end of a line, press '$' (also from regular expressions)

Move the cursor over a letter and press 'x' to erase it

Press 'u' to undo

Press 'dd' to erase an entire line

'.' repeats the last command: press it now to delete another line

Commands can be performed multiple times by prefixing them with a number.

Press '40i', type some letters, press Enter, and press escape.

The letters will be repeated 40 times.

Go to a line, and press '3dd'. 3 lines will be deleted.

Sometimes, you need to quickly navigate a large file.

Press 'gg' to go to the top of a file.

Press 'G' to go to the end of a file.

To go to a specific line, press '#G'. So to go to line 38, type '38G'. Very useful for debugging programs.

These commands in isolation simply move you. But you can combine them. For instance, 'dgg' will erase from the current line to the top of the file.

Not all commands are simply letters typed in command mode. Some commands are performed by prefixing with a colon (:)

To save a file, we 'write' it. To save this file, type ':w'. At the bottom of the screen, it will tell you that you have made a new file. If you run ':w' again, it will simply tell you that it wrote the file.

You can also change editor options this way.

Enter the following string of commands: 'ggdG'. What did we just do?

Enter the command: ':set tabstop?'. This tells you the current width of a tab. Now press 'i' and enter some text separated by a tab.

Press 'Escape', and enter ':set tabstop=15'. Notice that the tab is now much larger. You have changed how the editor represents a tabstop (note that the file doesn't actually have a larger space in it).

A common question is how to copy and paste. When you delete lines, you actually cut them: you are copying the lines and can paste them elsewhere.

To paste, you press 'p'. It will paste the copied lines below the current line.

What if you don't want to cut? In that case, you 'yank' the text. The command to yank is 'y'. You use it just like delete. 'yy' copies the current line, 'ygg' copies from the top of the file to the current line, and 'ggyG' would copy the entire file. You paste the same way you do with delete.

The things I have shown so far are cool, but I talked before about this being very useful for developers. Allow me to prove this. Let us quit from this file. The command to quit is ':q'.

ERROR. vi is telling us that we haven't saved the file with the most recent changes. We have two options at this point: we can save the file and quit, or we can quit without saving.

To save and quit, you simply combine the write and quit commands: ':wq'. This is a very common letter combination: vi users tend to forget that other programs use Ctrl-S, and will often accidentally press :wq at the bottoms of their files.

To quit without savings, you can override the error. To do this, you add a '!' to the end of the quit command, so you use ':q!'. This will quit without saving.

Anyway, let me now show you a few of vi's developer-friendly features. Let us create a source code file:

'vi test.c'

vi knows that '.c' is an extension used on C source code, and has automatically put us into C syntax highlighting. If you start typing C code, you will notice that words will be highlighted in order to tell you what's going on. Also note that lines are automatically tabbed correctly, as I type.

Let's type the following C program:

#include <stdio.h>

int main()
{
	printf("This is my main function!\n");

	return 0;
}

This is also a perfect time to demonstrate some of vi's searching features. Let us suppose that I wanted to find my main function in a large program. I could search for it by entering from command mode: /main

This will jump to the first match of main. To go to the next match, I simply press 'n'. Also notice that each match is highlighted. To go backwards in my search, I just press 'N' instead of 'n'.

The final thing to observe is vi's help system. You can find help for any option by typing ':help <option>'. For instance, for help on that tabstop command we ran earlier, we can type ':help tabstop'. To find out more information on vi's help system, you can type ':help'.

This is a simple overview of vi: it has a number of very complex and useful features that we have not covered.

To get another simple walkthrough, you can run the 'vimtutor' command from a terminal, and you will be brought into a tutorial for vi.