Topics:
Text editing tools and vi
Start and end a vi session
Move the cursor
Add, delete, and replace text
Search
Text Editors
Text Editing Utilities
There are a few common text editing utilities available for Linux
vi: (visual interpreter) the “original” and oldest text editor. It was developed by UC Berkeley student Bill Joy, when Unix was in its early stages. It is now often replaced by vim
vim: (vi improved) built on top of vi, with more bells and whistles. It was created by Bram Mooleanaar for the Amiga computer. This is the default text editor for Linux
pico: (pine composition) an editor built for the pine email tool. It was developed at the University of Washington and is most often used with the pine email tool.
emacs: (editor macros) this editor is as popular as vim. It was originally created by Richard Stallman (of Free Software Foundation fame)
During this notes, we will use vim, sometime pronounced as if it rhymes with ‘him’. vim is one of the most useful editor to learn for Linux, because every Linux distributions comes with vim as part of its package. Other editors are not guaranteed to be available without an extra download and install.
Using vim / vi
Since vi is a command line tool, there is no pull down menu for copy and paste, and no mouse to highlight and select. vi has to work with just the basic keys of letters, numbers, punctuation marks.
If every usable key is a certain text character, how can you tell vi that you want to select and delete a line?
The answer is in the 2 modes in vi: the command mode and the insert mode
Command mode: every key you type is interpreted as a command. Example: r for replace, q for quit
Insert mode: every key you type is interpreted as a text character that you add to the text file: r means r will appear in the text file, q means q will appear in the text file
It is very important that you keep track of which mode you are in, since r can mean 2 different things.
vi always starts in command mode.
At any time, to go to command mode, use the escape key.
From command mode, there are different commands to go to insert mode, as covered in the next slides.
vi commands are only used when you are in vi. They will not be interpreted correctly by the shell.
Note: The following lecture notes on vi is not a complete coverage of all vi commands. They are a set of most commonly used commands to help you get started. For more sophisticate use of vi, refer to the man page or the text book.
Command to Get Started
To run: vi filename
If filename is a new name, the new file will be created and then opened.
If filename is an existing name, the existing file will be opened
Empty lines (lines with nothing on it) appear with a ~ at the beginning of the line.
If the file is new, all lines will have ~
If the file is smaller than the size of the vi window, the last lines will have ~
If the file is larger than the size of the vi window, you see the first part of the file.
The cursor will typically be at the beginning of the file, or on the line where you last used vi with the file.
To open an existing file with the cursor at a particular line:
vi +n filename where n is the line number you specify
Commands to Get Out of vi
To get out of vi, you need to be in command mode so you can use the quit commands
To quit when you haven’t changed the file :q this is useful when you open the file for reading only
To quit but don’t save any change :q!
To quit and save any change :wq
The colon : is required in the quit commands. The q is short for quit, the w is short for write
If you’ve changed the file and use :q, vi will ask you to retype the command with the exclamation point :q!
Save Commands
You need to be in command mode to issue these save commands. You stay in command mode after these commands are run.
To save a file when quitting :wq
To save a file without quitting :w
It’s recommended that you do this every 5 or 10 minutes when you’ve been modifying a file. (A good rule of thumb no matter what editor you’re using)
To save a snap shot of the current file to a new file :w new_filename
Note that this does not save the current file in its own filename
The colon : is required. The w is for write
Commands to Move the Cursor
You need to be in command mode to issues these move commands. You will stay in command mode after the move.
The following commands are grouped in order of the distance of a move: from smallest distance to largest distance
Move by one character
Left: left arrow or h
Right: right arrow or l (lowercase L)
Up: up arrow or k
Down: down arrow or j
The j,k,h,l keys are not intuitive for the directions. They were created back at the time when a keyboard had no arrow keys, and these letters are the home keys (and therefore most easily reached) for a 10-finger typist.
Move by one word (a word is one or more character, surrounded by space)
Left: b (for back)
Right: w (for word)
Move within a line:
To the end: $ ($ often means end or last in Linux)
To the front: 0 (number 0)
Move one screen:
To next screen: control-f (f for forward)
To previous screen: control-b (b for backward)
Move to a line number: nG where n is the line number Numbering starts at 1, there is no space between n and G
G is for go
Move to last line: G
Move to first line: 1G or gg
Commands to Add Text
You need to be in command mode to run the add commands
The add commands put you in insert mode. At the bottom of the vi window is the word Insert to remind you that you’re in this mode
Once you’re in insert mode, any character you type is text that is added to the text file
When done, you need to use the escape key to get back to the command mode
Terminology: the cursor shows where your current position is in the text
The character at the cursor is the current character
The word that contains the cursor is the current word
The line that contains the cursor is the current line
Commands to add text. Use the one that is shortest to add text at the location you want
Add text after the cursor: a (for append)
Add text before the cursor: i (for insert)
Add text at the end of the line: A
Add text before the first word in the line: I (uppercase i) Note that this command does not add text at the very beginning of the line, if the beginning has space characters. This is useful for programmers who need to keep their code indented.
Add a new line of text above current line: O (uppercase o)
Add a new line of text below current line: o (lowercase o) (o for open)
Commands to Delete Text
You need to be in command mode to run these delete commands. After the commands run, you’re still in command mode.
Commands to delete text. Use the one that is shortest to delete text you want
Delete current character: x
Delete current word starting from the cursor: dw (for delete word)
To delete the entire word, make sure the cursor is at the beginning of the word
Delete current line, starting from the cursor: D (for delete)
Remove entire current line, no matter where the cursor is: dd
Commands to Replace Text
Replace means delete existing text and then add new text in its place
The replace command does both steps, saving you from having to delete in one step, then add text in a second step
You need to be in command mode to run the replace commands
Replace one character: r then new_character
The new_character can only be one character, then you’re automatically back to the command mode
Replace one word: cw (for change word)
This command deletes the current word, starting from the cursor, then puts you in insert mode so you can add the replacement text
Replace one line: cc
This command deletes the entire current line, no matter where the cursor is, then puts you in insert mode so you can add the replacement text
Commands to Search Text
You need to be in command mode to run the search commands. You stay in command mode after search runs.
The search commands search the file for an exact match of the text string. The text string can be a single character or multiple words. Spaces count as characters in the text string.
The search command will show you the first match
To search forward (from the cursor toward the end of file) for text_string: /text_string
To search backward (from the cursor toward the begin of file) for text_string: ?text_string
Since search will stop at the first match, you can ask for the next match:
In the same direction: n (for next)
In the opposite direction: N
If search reaches the end of the file, it will wrap around and continue the search starting at the beginning of the file. The same works for a backward search.
A text editor is a tool used for creating and modifying text files.
A text file contains basic characters that you can type in from the keyboard: letters, numbers, punctuation marks. A text file does not contain text formatting such as different font sizes, font types, colors, or special formats such as columns and bullets.
Text files are most often used with computers: software programs, scripts, log files, error files. On the Windows system, text files have the filename extension of .txt
Because text files are basic character files that are used with computer systems, a text editor has basic functionalities to modify these files, such as add, delete, change, cut, copy, paste. To do more visual formatting such as font color or table formatting, you would need to use a word processor rather than a text editor.