Unix Power ToolsUnix Power ToolsSearch this book

23.6. Job Control and autowrite: Real Timesavers!

I see too many people using a series of commands like the ones that follow. Programmers do this when they write and compile programs. Writers use this when they're making a draft file and running it through the formatter. They're probably wasting a lot of time and effort:

% vi somefile
   ...Edit somefile, then quit vi...
% someprog somefile
   ...Process somefile...
% vi somefile
   ...Edit somefile again...
% someprog somefile
   ...Process somefile again...

Each time they restart vi, they have to reset options and move the cursor to the place they were working before. After they restart, vi has forgotten the previous search (the n command), the previous action (the . command), the previous regular expression, the named and numbered buffers...

In the same way, why quit any other program (that isn't an editor) if you aren't done with it? The programs lose their state. For instance, quitting a man (Section 2.1) or info (Section 2.9) command when you're in the middle of a document means that when you start it again, it'll be at the start. It will have forgotten the last term you searched for.

If your system has job control (Section 23.1), that solves all these problems. (If it doesn't, you can still use a shell escape (Section 17.21).) Instead of quitting vi, get into command mode and write your buffer with the :w command. Stop the editor with the CTRL-z command. Then process the file. When you're ready to do more editing, bring your vi job back into the foreground with fg. The editor will be just where it was.

Even better, you can set vi's autowrite option. If you've made any changes to the buffer before you press CTRL-z, vi will automatically write the buffer. You won't need to remember to type :w before you stop the editor. You can set autowrite at a colon (:) prompt, but I set it in my .exrc file (Section 17.5) instead.

You don't absolutely have to write your file before suspending vi. It's a good piece of advice, but not required by the job control mechanism. Typing CTRL-z will suspend the editor whether you've written out your files or not.

-- JP



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.