Using Vi in Emacs
The two main commands are:
A D V E R T I S E M E N T
M-x vip-mode change from emacs mode to vi mode
C-z change from vi mode back to emacs mode
(Recall that there are also available viper-mode and vi-mode.) Both for
convenience and for easy remembering, I have in my .emacs file (which you should
copy, as mentioned earlier) these commands:
C-x v change from emacs mode to vi mode
C-x e change from vi mode back to emacs mode
Note that if there are two windows on a buffer, the vi commands work
on the two windows as a whole; it is just one buffer, i.e. just one file.
So, for instance, when I was searching for the string `WordCount' in the example
above, I first typed
using the standard vi search command `/', and then moved to the other
window and simply typed `n'. This is also a standard (though possibly new to
you) vi command, meaning, ``Repeat the last search.'' So it did look for
a string `WordCount', even though I had changed windows.
Just as ordinary vi will automatically read a file .exrc from your
home directory, and read in customization commands from it, one can do this here
too. In my .emacs file in my home directory, I have put the two lines
(local-set-key "]" 'vip-scroll)
(local-set-key "[" 'vip-scroll-back)
after the line
The latter line has emacs start up in vip-mode, and then the ``local''
commands apply to that mode. Whenever I am using vip-mode, the ] and [ keys will
have the same meaning as vi's usual scroll-forward and scroll-backward
commands, i.e. C-f and C-b.
Using Emacs for Compiling Programs
To compile a program whose source code you are editing in emacs, type
Since in a debugging session one will compile many times, to save typing, I
have in my .emacs file bound C-x c to M-x compile, so I need type only C-x c.
Here is what happens when I type the compile command:
/* introductory C program */
/* implements (a subset of) the Unix wc command -- reports character,
word and line counts; in this version, the "file" is read from the
standard input, since we have not covered C file manipulation yet,
but of course a real file can be read by using the Unix `<' redirection
feature */
#define MaxLine 200
char Line[MaxLine]; /* one line from the file */
int NChars = 0, /* total number of characters in the file */
NWords = 0, /* total number of words in the file */
Note the minibuffer here (`Compile command: make -k'). Though you can't see
it here, the cursor is right after the -k. Emacs is waiting for me to
confirm that this is the compile command which I want. I want a different one,
so I use the Delete key (or Backspace key, on some terminals) to erase the `make
-k', and then put in the compile command which I want, which in this instance is
`cc -g WC.c': The screen now looks like this:
/* introductory C program */
/* implements (a subset of) the Unix wc command -- reports character,
word and line counts; in this version, the "file" is read from the
standard input, since we have not covered C file manipulation yet,
but of course a real file can be read by using the Unix `<' redirection
feature */
#define MaxLine 200
char Line[MaxLine]; /* one line from the file */
int NChars = 0, /* total number of characters in the file */
NWords = 0, /* total number of words in the file */
Compile command: cc -g WC.c
I then hit the return key, and emacs creates a new buffer for the
compile:
/* introductory C program */
/* implements (a subset of) the Unix wc command -- reports character,
word and line counts; in this version, the "file" is read from the
standard input, since we have not covered C file manipulation yet,
cd /usr/home/matloff/Courses/40/Progs/
cc -g WC.c
(No files need saving)
Eventually, that new buffer will include a `Compilation finished' message,
and will display any syntax errors which it found. If there are any such errors,
I can keep typing
(C-x and then a backward apostrophe), and emacs will automatically go
to the buffer for my source file, and position the cursor at the source of the
error; this is a big convenience.
By the way, note the message `No files need saving'. If I had made a
modification to the source file, emacs would have asked me if I wanted to
save the file before compiling it.
I then invoke gdb. (Note: You probably will prefer to use ddd,
a very nice graphical user interface for gdb. To do this in coordination
with gdb, type "M-x gdb", then at the prompt change the command to "ddd
--ttt --gdb". Be sure ddd is in your search path.) Emacs will ask
me what the executable file name is, in this case a.out. Emacs will
create a new buffer for gdb. Then I give gdb a breakpoint command,
to stop at the function WordCount(), and I type r to run. When the program
reaches the breakpoint, my screen looks like
Breakpoint 1 at 0x40023c: file WC.c, line 43.
(gdb) r < z
Starting program: /usr/home/matloff/Courses/40/Progs/a.out < z
Breakpoint 1, WordCount () at WC.c:43
(gdb)
int WordCount()
{ int I,NBlanks = 0;
=> for (I = 0; I < LineLength; I++)
if (Line[I] == ' ') NBlanks++;
/* ordinarily the number of words is NBlanks+1, except in the case
in which the line is empty, i.e. consists only of the end-of-line
character */
There are now two buffers displayed on the screen, my gdb buffer at
the top and my WC.c buffer at the bottom. Note the marker `=>' in the latter; it
shows that my next source-line statement is
for (I = 0; I < LineLength; I++)
if (Line[I] == ' ') NBlanks++;
If for example I give gdb an n command now, it will execute that
statement (and the `=>' marker will then advance to the next line).
Note again that if I want to restore the WC.c buffer to be displayed in
full-screen instead of half-screen form, I merely need to type C-x o to switch
to the WC.c buffer, and then type C-x 1.
|