[ back to User's Guide Table of Contents ]

How To Create or Edit a File

   

Three editors, Emacs,  JOVE  , and vi   can be used to create files on the EECS Instructional machines. You can also create short files using the UNIX cat command.

vi:
vi is the standard editor on UNIX-based systems.

Emacs:
GNU Emacs  is becoming the most popular editor. Emacs is a public-domain editor that is available for a variety of systems and is therefore popular with users who work on more than one system. Features available in Emacs help make debugging code with gdb (the GNU debugger) easier to manage.

JOVE:
JOVE stands for "Jonathon's Own Version of Emacs". It is a modified version of Emacs that contains most of the important functionality, but not all the features, of Emacs.

cat:
To create a file using the UNIX cat command,  first enter cat > filename, then enter the lines of text, pressing Return after each line; end by pressing d. If you make mistakes entering the lines, you'll probably need to use an editor to correct them.

Points to remember when editing files:

Find the control keys:
Before entering an editor, be sure that you know where to find the the special control keys, such as Ctrl, Meta and Escape. 

Enter commands in the correct case:
Remember that UNIX is case sensitive.

Save your file frequently:
When you save your edited file, the original version is gone. (In fact, Emacs makes a copy of the original, but JOVE and vi don't.)

Until you are familiar with UNIX and the editor you are using, you may wish to make an extra copy of your file before you start to edit it. For example, if you want to edit a filed named mywork, you could do:

% cp mywork myworkBackUp

before doing any editing.

Editors usually create temporary files from which recovery may be possible in case of a system crash (or in case you inadvertently close the window in which you were doing the editing). However, it isn't wise to depend on recovering a file in this way. So save your file frequently during your editing session. Also, save different versions of files -- don't just save it with the same name over and over again, because if you accidentally delete huge portions of the file and wipe out your only copy, you are stuck. Consider naming files mywork.1, mywork.2, etc. Delete old copies when you are sure you have a good one.

You may need to set the terminal type so that the editor will display to your screen correctly and recognize your control characters. See Setting Your Terminal Type.

If you cannot save your file in your account for any reason (like the disk quota system won't let you), you can save your files temporarily in /tmp. /tmp is a temporary directory that all users can write into. Your files may only stay here a short time and are not guaranteed to remain there, but you can usually store files there while we resolve your disk storage problem.

     

Using the Emacs or JOVE Editors

GNU Emacs is a complicated editor with a large number of sophisticated features. The purpose of this section is to provide you with enough information to create and edit files.

JOVE is a simpler modified version of the Emacs editor. Many of the commands are similiar, but there are differences in some of the command syntax and not all Emacs commands are available in JOVE. We have tried to clarify these differences when relevent to the commands listed in this chapter.

Emacs Windows

When you use Emacs on a workstation, it creates a window for you to work in. (JOVE just runs in whatever window you start it in.) If you don't like the size or position of the window, you can change it by resizing the window.

The window is divided into two parts by a black bar containing status information. The large area above the bar is the buffer-editing area; the small area, usually a single line below the bar is the echo area or minibuffer where Emacs displays messages and prompts for responses (see Emacs/JOVE Buffers).

Temporary Files

 

If you exit from Emacs normally and save your file, you will find that Emacs has also saved your original file; the filename for this old version is formed by appending a tilde (~ ) to the original filename.

Every 300 characters, Emacs saves the changes in an auto-save  file. The auto-save filename is formed by adding a pound sign (#) at the beginning and end of the original filename. If you exit from Emacs abnormally, you will see this file. You can recover  most of your edits from this file in case of a crash or in case you inadvertently close the Emacs window without exiting from Emacs. It isn't wise to count on this auto-save file, though; it's better to get in the habit of saving your file every five or ten minutes. You can do this without exiting from Emacs.

Since these Emacs temporary files are created automatically, you need to check your account periodically to be sure you don't accumulate temporary files and exceed your disk quota.

JOVE's behavior with temporary files is different. JOVE temporary files have names like jreca13016 and jovea13016 and get deleted when you exit JOVE normally. If you don't exit JOVE normally (due to a system crash, modem hang, etc.), you will need to delete these files manually.

Invoking Emacs or JOVE

You can invoke Emacs or JOVE by entering one of the UNIX commands emacs, emacs filename, jove, or jove filename. On workstations, it is more useful to invoke Emacs with the emacs & command because then you can move back and forth between your UNIX window and your Emacs window. (You cannot do "jove &" to get another window. If you want a separate JOVE window, you must open up a new window and run jove in the new window.) This allows you to enter UNIX commands without exiting from Emacs--just move the mouse cursor to the window you want to use.

In either case, be sure to save your file and exit the editor before you logoff.

Entering Text and Commands

In Emacs and JOVE, you are either typing text or entering commands. Commands always start with a prefix character, either the Control key CTRL  or a meta key META.  Press and hold down this prefix character while typing the character that completes the command.

On workstation keyboards, the Compose Character key  is the Meta key. On other keyboards, it might be Edit  or Alt.  If you can't find your Meta key or your keyboard doesn't have one, use Esc.  If you do use Esc, release it before typing the character that completes the command.

If you type a closing parenthesis, square bracket, or curly brace in the text you are entering, the cursor will momentarily move to the corresponding opening parenthesis, bracket or brace. This helps you see whether you are matching them correctly. Note that this is a mode in Emacs which can be turned off if you don't wish to use it. In JOVE, this mode is automatically on if the file you're editing has a ".c", ".l", or ".scm" extension, i.e., "homework.scm".

Common Emacs and JOVE Commands

   

Table 4.1 lists commonly-used Emacs and JOVE commands.  

When a command requires a response of some kind, for example a filename or a string, you will be prompted for it in the echo area at the bottom of the screen. Enter your response and press Return. When you need to enter a filename, the prompt will include the current path. If the file is in this path, just enter the filename. If it isn't, back up over the path and enter the correct path and filename.

If you change your mind in the middle of a command and wish to cancel that command, g will cancel the command you're in the middle of and return you to regular text insertion. (Sometimes you need to type g more than once for this to work.)  

Table 4.1: Emacs/JOVE Commands

Topic  	Function                             	Command 
-------	--------------------------------------	-----------------------------------
Invoke	Invoke emacs	                 	% emacs & 
		or           			% emacs filename
		or                 		% emacs} 
	Invoke jove                         	% jove 
		or           			% jove filename
-------	--------------------------------------	-----------------------------------
Exit  	Exit from Emacs/JOVE                  	CTRL-x CTRL-c
      	(You will be asked about saving files   	
      	except the Scratch buffer.)          	
-------	--------------------------------------	-----------------------------------
Files	Get file, open new buffer for it     	CTRL-x CTRL-f filename
  	Get file, include in current buffer 
			   	    (Emacs)  	CTRL-x i 
  	Get file, include in current buffer 
				     (JOVE)  	CTRL-x CTRL-i 
      	Save file     				CTRL-x CTRL-s 
		or	                  	CTRL-x s 
      	Write to a different file           	CTRL-x CTRL-w filename
-------	--------------------------------------	-----------------------------------
Cursor	Move cursor in any direction         	use the arrow keys 
      	Move forward one character	    	CTRL-f 
      	Move forward one word at a time     	META-f 
      	Move backward one character         	CTRL-b 
      	Move backward one word at a time	META-b 
      	Move down to the next line 		CTRL-n
      	Move up to the previous line          	CTRL-p 
      	Move to the beginning of the line    	CTRL-a 
      	Move to the beginning of the sentence	META-a
      	Move to the end of the line		CTRL-e 
	Move to the end of the sentence		META-e
      	Move down one screenful			CTRL-v
      	Move up one screenful			META-v
      	Move to the beginning of the buffer  	META-<
      	Move to the end of the buffer        	META-> 
	Move to the mouse cursor position 
				(Emacs only) 	click left mouse button 
-------	--------------------------------------	-----------------------------------
Delete	Delete character before cursor		[DELETE]
      	Delete character at cursor           	CTRL-d
      	Delete from cursor to end of word    	META-d
      		(with special characters)
      	Delete from cursor to end of line    	CTRL-k
      		with trailing return    	CTRL-k CTRL-k 
	Delete the current sentence		META-k
-------	--------------------------------------	-----------------------------------
Trouble	Undo the last change (Emacs only)   	CTRL-x u 
      	Undo the last several changes        	CTRL-x u ... CTRL-x u
      	Cancel the current command           	CTRL-g
      	Repaint the screen                   	CTRL-L 
-------	--------------------------------------	-----------------------------------
Find	Find next occurrence of string       	CTRL-s string (See Trouble in Emacs or JOVE)
   		 and the next   		CTRL-s        (See Trouble in Emacs or JOVE)
     	Find previous occurrence of string   	CTRL-r string
      		 and the one before that 	CTRL-r 
      	Terminate search in Emacs            	[ESC] 
      	Terminate search in JOVE		[RETURN]
-------	--------------------------------------	-----------------------------------
Modify	Change string1 to string2 from cursor	
      		to end of buffer in Emacs	META-%
	 Change string1 to string2 from cursor	
      		to end of buffer in JOVE	META-q 
   		Respond to prompts with 	string1 [RETURN] string2 [RETURN]!
-------	--------------------------------------	-----------------------------------
Move  	Move/copy/delete block of text.      	
	 First locate beginning of block and	move cursor to start of block 
      		 Set a mark with 	      	CTRL-Space 
     		 or	                    	META-x set mark 
	 	 or    	                	CTRL-@
 	Then locate end of block and either	move cursor to end of block 
      		 Move block to kill ring,	CTRL-w 
      		 or Copy it to kill ring.	META-w 
      	Finally, for move or copy, locate new	
	 	 position for text block,	move cursor to new position 
      		 Yank block from kill ring. 	CTRL-y
	 Replace the text just yanked with	META-y
	 the item before it in the kill ring. 
-------	--------------------------------------	-----------------------------------

Buffer	Display list of buffers              	CTRL-x CTRL-b
      	Switch buffers, entering name        	CTRL-x b buffer name [RETURN]
 	Move cursor to other window          	CTRL-x o ("oh") 
	Return to one window mode            	CTRL-x 1 ("one") 
-------	--------------------------------------	-----------------------------------

Help  	Invoke Emacs help to read tutorial   	CTRL-h t
      	Invoke Emacs help                    	CTRL-h 
	Invoke JOVE help (from UNIX)	  	teachjove
-------	--------------------------------------	-----------------------------------
Recover	After invoking Emacs,                	
	ask to recover auto-save file        	META-x recover file
	                                      	filename [RETURN]
  	When Emacs file is recovered, save it.	CTRL-x CTRL-s 
	Recover JOVE auto-save file (from UNIX)	jove -r
      	Cancel the current command           	CNTL-g
-------	--------------------------------------	-----------------------------------

Table 4.1: Emacs/JOVE Commands
   

Emacs/JOVE Buffers

When you use Emacs or JOVE to create or modify a file, a temporary workspace called a buffer is set up for you. Your file is edited in the buffer and the original file is not modified until you ask to save the changes.

You can edit more than one buffer at a time if you wish. Some commands also create a new buffer--Emacs/JOVE will either divide your window into two parts, for example, when you ask for help, or will hide your editing buffer, replacing it with the new area. To get back to your main buffer, you need to use one of the buffer/window commands.

If you get into trouble with buffers and can't figure out what to do, enter x c to exit from the editor. You will be asked if you want to save the file you are editing (unless you are editing it in the Scratch buffer). 

Learning More About Emacs

Online help for Emacs includes a tutorial. Look at Table 4.1.5 for how to access the online help. While you are learning Emacs and aren't familiar with moving between Emacs buffers, it is probably simpler to look at help when you are not editing a file. Then you can just exit Emacs without trying to figure out how to get back to your editing buffer.

Learning More About JOVE

    For an on-line tutorial on JOVE, run the command teachjove. (If you get an error message "Command not found", you may have to ask your system adminstrator ("root") where this is installed.)

Trouble in Emacs or JOVE

  Depending upon your system or local configuration of Emacs or JOVE, you may run into a problem where s freezes the screen instead of performing a text search. If this happens, type q to "unfreeze" your screen.

Using the vi Editor

 

Even if you plan to use another editor, you may want to know a little about the vi editor because it is the standard editor on UNIX systems. As do most editors, vi edits your file in a buffer and doesn't modify the original file until you ask it to.

Entering Text and Commands

Since vi commands are just letters like `i' and `x', you need to tell vi whether you are entering text or commands. When you enter vi, you are in Command mode, and vi assumes that everything you enter is a command. When you want to enter text, you need to enter a command to switch to Input mode. Vi then assumes that everything you enter is text until you press Escape to return to Command mode. In addition to Command and Input modes, vi also has a Line mode.

Command mode:
When you enter vi, you are in Command mode. In this mode you can move the cursor, enter vi commands, save your file, and exit from the editor. 

Input mode:
In order to enter text, you need to switch to Input mode.  Then you enter the text and return to Command mode by pressing the Escape key. While in Input mode, you can enter several lines of text, pressing Return after each line, but you cannot move the cursor until you Escape back to command mode.

Line mode:
From command mode, you can also issue line commands  to delete or change a range of lines, execute UNIX commands, and exit from the editor. You start these commands with a colon (":") and end them by pressing Return.

Invoking vi

 

Before you invoke vi, you should know where your Esc  key is. On many workstations, it is F11; on some terminals, it is labeled Esc. If you don't know where the Escape is, you can't get out of input mode and you won't be able to exit from vi normally.

You can invoke vi with vi or vi filename.

Common vi Commands

 

Table 4.2 lists common vi commands.  



Topic	Function	 		  	Command

-------	--------------------------------------	-----------------------------------
Before	Find ESCAPE key                  	[ESC] or [F11] or CTRL-[ 
-------	--------------------------------------	-----------------------------------
Invoke	Invoke vi to edit a file 		% vi filename 
	Invoke vi for read only      		% view filename 
	Invoke vi to recover from a crash	% vi -r filename 
-------	--------------------------------------	-----------------------------------
Exit  	Exit from vi and save your file		ZZ 
      	Exit from vi and save your file		:wq 
      	Exit from vi without saving file		:q! 
-------	--------------------------------------	-----------------------------------
Text 	Enter text before the cursor  		i text [ESC] 
      	Enter text after the cursor  		a text [ESC] 
      	Enter text at end of line    		A text [ESC] 
	Enter text on new line above cursor	O text [ESC] 
	Enter text on new line below cursor	o text [ESC] 

-------	--------------------------------------	-----------------------------------
Files 	Include another file             	:r filename 
	Save your file without exiting vi	:w filename 
	                                   	(filename optional)  
-------	--------------------------------------	-----------------------------------
Cursor	Move in any direction                	use arrow keys 
      	Move one word at a time, forward     	w 
      	Move to the beginning of the line    	^ 
      	Move to the end of the line          	$ 
      	Move down several lines              	CTRL-d 
      	Move up several lines                	CTRL-u 
      	Move to beginning of buffer          	1G 
      	Move to end of buffer                	G 
-------	--------------------------------------	-----------------------------------
Delete	If n in next commands is 1, omit it: 	
      	Delete n character(s) at cursor      	n x 
      	Delete n words(s) at cursor          	n dw 
      	Delete n line(s) at cursor           	n dd 

-------	--------------------------------------	-----------------------------------
Trouble	Undo the last change                 	u 
      	Undo last changes on current line    	U 
      	Repaint the screen                   	CTRL-L 
-------	--------------------------------------	-----------------------------------
Modify	Split a line at the cursor  		i [RETURN] [ESC]
      	Join two lines				J 
      	Replace one character at cursor 	r char
      	Replace several characters at cursor	R chars [ESC]
	Change string1 to string2		:1,\$s/string1/string2/g
-------	--------------------------------------	-----------------------------------
Find 	Find next occurrence of string,      	/string [RETURN] 
 		 and the next            	/ [RETURN]
		or		  	 	n
 	Find previous occurrence of string   	?string [RETURN] 
		and the next            	? [RETURN]
-------	--------------------------------------	-----------------------------------
Move	Move/copy a block of n text lines	move cursor to start of block 
      		 and Remove the lines    	n dd 
      		 or Copy (yank) them     	n yy 
	Then locate new position for lines,  	move cursor to new position 
      		 and Put lines above cursor	P
      		 or Put lines below cursor 	p
-------	--------------------------------------	-----------------------------------
UNIX	Issue UNIX commando			:! UNIX command
-------	--------------------------------------	-----------------------------------

Table 4.2: vi Commands

Trouble in vi

 

Vi on many systems saves a temporary file containing your edits. If the system crashes while you are editing a file, you may be able to recover most of what you were doing, but don't count on it. Save your file frequently while you are editing. To try to recover edits from this temporary file, enter the command "vi -r filename"   where the filename is the one you were editing before the crash.


inst@eecs.berkeley.edu