Updated 7/15/22 |
Learn Emacs in two hours!What is the best editor for both Unix and Windows? Emacs, of course!Why use Emacs?Emacs is a great editor, especially for code development, offering many more features than vi (though that is not saying much!) If you ever want to edit more than one file at a time, write C, Verilog, or VERA code, run repetitive tasks, give Emacs a try. Emacs also automatically saves your changes in case of system crashes, and its undo saves all your changes.Which Emacs should I run?Emacs was created in the late-1970's by Richard Stallman of the Free Software Foundation. Since then there have been many different flavors. For Unix systems, run the command 'xemacs' (X-Windows Emacs), or 'emacs' (not as good, does recognize all keys or the mouse, but does not need X-Windows). One big advantage of xemacs is that many commands are available using pull-down menus that also show the equivalent keyboard binding. Read about how to download and install Emacs on Windows.How do I get started?Start Emacs and run the tutorial by typing ^h t (Control h, followed by t). This takes about an hour, and will give you a good, basic understanding of how to use Emacs. Be sure to learn how to use multiple buffers and split the screen.What's next?As you will learn from the tutorial, Emacs has a lot of commands, and it can be intimidating to learn all the bindkeys. I've created a small chart showing the most common bindkeys which you can print and put on your wall. Try all the bindkeys on the small chart - I use them constantly. Here is a more comprehensive reference card. For more practice, check out this on-line tutorial .LanguagesWhen you open a .c file, you will notice that Emacs displays (C) at the bottom of the screen on the Mode Line, showing that you are in C mode. Some commands / bindkeys, such as Tab, behave differently depending on your mode. The other popular mode is text. Remember to use M-x auto-fill-mode to enable automatic wrapping.
On Unix put it in ~/elisp, on Windows put it in c:/Program Files/emacs/lisp/progmodes Shell modeBy now you should have realized that many Emacs commands (^a, ^e, M-u, etc.) also work in tcsh, the command shell. Have you thought about running Unix commands inside Emacs? Use M-x shell to start a command shell inside a Emacs buffer. Many users only run Unix shells inside Emacs, never an Xterm. Now you can easily cut / paste / search the output of programs, and never have to worry about information scrolling out of reach. Command line recall is done with M-p. If you want to start multiple shells, use M-x rename-buffer to rename the current shell buffer, then M-x shell to start the second.Compile CommandI love this one! Say you are editing a C file and want to compile it. Use M-x compile. Emacs will check if you have any unsaved buffers, and ask if you want to save them. Then it asks for your compile command, with a default of make. If you have a Makefile, great! If not, use make foo where foo is the name of your program. Emacs creates a second window, and compiles and runs your program. If there are any compile errors, use ^x ` (reverse quote) over and over to step through them. Emacs displays the offending source code line, opening the file if needed.This works well for anytime you have source code and a Makefile (though Emacs can not parse VCS compile error messages). Electric DiffThis one will knock your sox off! How do you find the differences between two files, for example, foo and its backup foo~? Unix diff is tolerable, and sdiff is only slightly better. The best is Emacs' Electric Diff, run with M-x ediff. It prompts you for two file names, splits the window in two and highlights the differences. Not only are the individual lines color coded, but the characters in the line are highlighted so you can quickly spot the difference between hello and he11o (the latter uses the number one). Using the popup window you can step through the two files, and even copy sections to merge changes. You'll never run diff again! This even works on Windows.Keyboard MacrosEmacs can interactively make complex repetitive changes to a file, without having to learn a language such as sed, awk, or Perl. (The following example is easier than it looks.) Say you have a list of signals, and now want to make a block of code to initialize them:integer alpha, beta, c; You can make this change in two steps. First, create the fixed parts of the new code (initial begin, end) and put a copy of the signal list in the middle: integer alpha, beta, c;
Next, position the cursor at the beginning of the alpha line, start defining a macro with ^x ( and make your edits to create the first line of code. I would enter 4 spaces, M-RightArrow to move to the end of the word 'alpha', type the rest of the line and do any final cleanups so the cursor was properly positioned before the next token, 'beta'. integer alpha, beta, c;
Now, finish the macro by typing ^x ) and you are ready to rock! Execute the macro a few times with ^x e: integer alpha, beta, c;
If you have many edits, use ^u ^x e to run the macro 4 times, ^u ^u ^x e to run it 16 times, and so on. This may execute slower than the equivalent Perl program, but since you never had to write or debug anything, you are still done sooner for all but the largest, 100-megabyte files. Auto save and Recovering FilesEmacs periodically saves your changes into #file# so that you won't lose your work, even if there is a crash. You can recover using the command M-x recover-file and following the prompts.Debugging C codeThe GNU debugger is gdb, and it is available from inside Emacs, of course. Start it with M-x gdb, then give the name of the executable to be debugged. (Don't forget to compile your C code with -g.) Set a breakpoint with b <function name>, then start execution with run, followed by any command line arguments (such as run -l simv.log when debugging a VCS problem). You should see the gdb output in one window and the source code in the other. Other useful commands are step to step to the next line, next which steps over routines, continue to resume execution, print to print a variable's value, bt to print a backtrace, and quit. All can be abbreviated.Enjoy!There is more to Emacs than can be put on a few pages. I didn't even mention M-x indent-region (cleans up code after lots of editing), M-x flush-lines, (used to trim a large file), M-x tabify and M-x untabify (switch between tabs and spaces in a selected region). Explore the web for more ideas. Please send me any comments or suggestions.Home SystemVerilog PLI Verilog Verification PMC Emacs Bike Personal Viewlogic |