USF Home
Administrators
Hardware Status





Search this site
 

Linux Tutorial: Chapter 4

5.0 Editing Files

Linux usually comes with many text editors, common ones include vi and emacs. These two editors in particular have separated Linux users into two groups that fight over who has the better editor. This tutorial will focus on vi, mainly because vi is found on most Linux installations by default and vi is considering one of the fastest editors around. We will show you most of the basic commands that you need to know to get started and feel comfortable.

5.1.0 Using vi

vi, (pronounced vee - eye), is a line by line text editor written in 1976 by Bill Joy. There are generally three types of modes that you are in while using the editor. One of the modes is called the command mode, in this mode you will enter different commands to the editor to do things such as navigating the lines of the file, searching for patterns, deleting lines and/or individual characters, etc. We will tell you some of the more command commands later on, generally you only really need to know about 15 or so commands to edit a file and do basic functions on it. Another mode that you will spend a lot of time in is the edit mode. In edit mode you edit the file that you are working on by simply entering text. You will always have to be in this mode to actually add to the file. And the other mode that is in vi is the line (or EX) mode. In this mode you will enter commands by first typing the colon (:) and then the command. This mode is pretty much considered just the basic command mode. That's pretty much all you need to know about the different modes of vi.

So let's start working in vi! You can start the editor with an empty buffer by simply entering vi into the command-line or start it with a file by entering 'vi filename'.

5.2.0 Using the command mode

Upon starting the editor you will be put into command mode. Let's see how we can use this mode.

Some commands that are used in vi can be entered by first entering a ':' into the editor. This will change the line of the bottom of the editor to a ':'. Most other commands that we will be using you can just enter then in the editor while in the default command mode. You can also get to command mode by hitting the ESC key several times. Lets look at the basic commands that we can use while in this mode.

5.2.1 Saving and quitting the editor

:wSave the file
:w! filenameSave the current buffer to filename
:qQuit the editor
:q!Quit the editor, abandoning all changes
:wqSave the current file and quit


5.2.2 Navigating the file

:nMove the the n-th line in the file
nwMove n words to the right
wMove to the beginning of the next word
eMove to the end of the next word
bMove to the beginning of the previous word
hMove the cursor one character to the left (May also be left arrow key)
lMove the cursor one character to the right (May also be the right arrow key)
jMove the cursor up one line (May also be the up arrow key)
kMove the cursor down one line (May also be the down arrow key)
0Move the cursor to the beginning of the line
$Move the cursor to the end of the line
GMove the cursor to the end of the file
%When the cursor is over a [,],(,),{, or } use this command to move the cursor to either the beginning or closing [],(), or {}.
Ctrl-fMove the cursor one page forward
Ctrl-bMove the cursor one page back
Ctrl-dMove the cursor half a page forward
Ctrl-uMove the cursor half a page back
Ctrl-mMove the cursor to the first character on the next line


5.2.3 Replacing/Deleting Text

ddDelete the current line
nddDeletes n lines
dwDelete the word under the cursor
ndwDelete n words
xDelete the character under the cursor
nxDeletes n characters
cwChange a word, deletes the word under the cursor and puts you in insert mode
ccChange a line, deletes the current line and puts you in insert mode
sChange a character under the cursor
uUndo the last change
JJoin the following line with the current one
UUndo all changes to current line
DDelete from the cursor to the end of the line
CChange the current line from the cursor - deletes everything after the cursor


5.2.4 Copying/Cutting/Pasting

yyYank(copy) the current line into a buffer
nyyYank n lines into a buffer
ynwYank n words into a buffer
ddDeletes the current line, also stores it into a buffer. ndd deletes and stores n lines
"ayyYanks the current line into a buffer called a, a could also be any other letter
n"ayyYanks n lines and puts them into a buffer called a. Again a could be any other letter
PPastes the buffer before the current line
pPastes the buffer after the current line
"aPPaste the buffer 'a' before the current line
"apPaste the buffer 'a' after the current line


5.2.5 Inserting Text

:r filenameAdd the contents of filename below the current line
iInsert text at current position of the cursor
IInsert text at the beginning of the current line
aInsert text at position next to the cursor
AInsert text at the end of the current line
oOpen a new line of text below the current line and change to insert mode
OOpen a new line of text above the current line and change to insert mode


5.2.6 Searching

/patternSearch the file downwards for pattern
?patternSearch the file upwards for pattern
nRepeat the last search
NRepeat the last search except in the reverse direction


5.2.7 Miscellaneous Commands

Ctrl-gProvides information on the current file such as filename, line number at current position, total lines in the file and the percentage of the current cursor position
Ctrl-[Cancel the current command
.Repeat the last command
>>Indent the text to the left
<<Indent the text to the right
Ctrl-lClear and redraw the screen

5.3.0 Other Editors

You should now have a grasp on the basic workings of vi and its commands. It will take some time to learn all the commands that are given here (which are not all the commands available to you), and feel comfortable working with the editor. But given some time you will see that the editor is very powerful and can do operations very quickly. Please understand that vi is not the only editor that is available for Linux. You may want to try pico, its very similar to the MS-DOS version of edit and can do mostly everything that vi can do, although it is not as fast.

Take Chapter Quiz


 



Copyright © 2008, Research Computing, USF, 4202 E. Fowler Avenue, Tampa, FL 33620