Experience and skills: Use of the Emacs editor

Source: Internet
Author: User
Title: experience and skills: Use of the Emacs editor. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
   A.1 Introduction to Emacs
We have already introduced a full-screen text editing tool Vi. Here we will introduce another powerful and widely used full-screen text editor Emacs.
Emacs is called Editor MACroS. because of its excellent portability, Emacs can be seen in almost any operating system in the world today. The current Emacs has gone beyond the original single text editing function. It can be used to manage files, read bulletin boards, or even perform WWW browsing. In addition, Emacs source code can be customized using C, C ++, Lisp, and other languages. For some programmers, this is undoubtedly a good news and greatly increases their flexibility.
The use and operations of the Emacs editor are the same as those of other standard text editors. The general buttons on the keyboard are used to input characters, and the editor operation commands are implemented by some special buttons on the keyboard, such as Ctrl, Alt and other control keys. Unlike Vi, the Emacs editor has no specific input mode or command mode. When entering text, you can also execute the edit command, such as using the Ctrl key to move the cursor and save the file, without having to endure the trouble of switching the mode.
The Emacs editor is a complex and flexible editor with hundreds of editing commands. The Emacs editor also has some special features, such as the Multi-Window feature. You can display both windows when editing files. You can also open and edit multiple files at the same time, and display each file in the corresponding editing window on the screen.
The Emacs editor provides powerful and flexible functions by cleverly controlling and operating the work buffer. The Emacs editor can be considered a buffer-oriented editor. When editing a file in any editor, the file is first copied to the work buffer, and all the editing operations are performed in the work buffer. Many editors only open one working buffer when editing files, so they can open only one file. The Emacs editor can open up and manage multiple working buffers at the same time. Therefore, you can edit multiple files at the same time and use the editing buffer to save, delete, or copy text, users can even open up their own buffers and save text in these buffers. If necessary, they can save the text in these buffers to files.
  
   A.2 start and exit Emacs
A.2.1 start Emacs
By typing:
$ Emacs [file name]
Enter the Emacs text editing program. If you do not type a file name, a new file is created instead of opening an existing file, as shown in the A-1 ).
Figure A-1 Emacs Program Interface
Whether a new file is created or an existing file is opened, it is put into the buffer zone in the processing method. Like Vi, the buffer content is not written to files as long as the user does not issue commands stored on the disk. At the same time, this allows users to copy and paste among multiple buffers, which is very convenient.
A.2.2 exit of Emacs
There are several feasible ways to exit after necessary modifications are made to the file. If you have not modified the file, use Ctrl + x and Ctrl + c to exit Emacs. If you have modified the file, use the preceding buttons to exit, in this case, the system will ask whether to save the file. If you type y, the system will save the file. If it is a new file, the system will prompt you to enter the file name. Save the file and exit. If you type n, the system will ask again if you do not want to save the content in the buffer to exit. Then, if you answer yes, the system will quit the modification. Of course, you can also save the file by running the command and then exit, you can press Ctrl + x and Ctrl + s to save the file and exit. If you want to save the editing program to another file that is different from the original file, you can use Ctrl + x, Ctrl + w, and then specify the new file name.
If you are performing Emacs-related operations and need to perform other operations in Linux, you can select either of the following methods.
1. Abort Emacs and return the Linux shell
You can abort any Linux application by pressing Ctrl + z. This combination key puts the current application in the background and provides you with another shell prompt. To reactivate Emacs, type the command fg to bring the background task back to the foreground. If the shell in use does not understand this command, type exit to reactivate Emacs.
2. Issue a shell command from Emacs
If you do not need a complete shell environment, you can use shell commands in Emacs. To use shell commands in Emacs, press Ctrl + u, Esc ,!, Enter a shell command. Enter the command and press Enter. Emacs passes the command to the Linux shell and the shell executes the command. If you do not add the Ctrl + u command before, Emacs will put the output result in a buffer called shell execution result. To close the window, run Ctrl + x.
  
   A.3 use the Emacs editor to create a file
Follow these steps to create a new file using Emacs:
1. Start emacs (type emacs and press Enter) to see the screen shown in the figure A-1.
2. Add file content to the buffer.
3. Save the content in the buffer to a file (assuming the file name is mydata ). Press Ctrl + x, Ctrl + s, type the file name (mydata), and press enter to save the content in the buffer to the file (mydata. The following information is displayed on the Status line:
Wrote/root/mydata
Indicates that a new file (mydata) has been created and saved to the disk.
4. Press Ctrl + x and Ctrl + c to exit Emacs.
If unsaved content exists when you exit Emacs, Emacs prompts you to save the file. Enter the file name you want to edit after the Emacs command. If the file name does not exist, the file will be created. In the following example, you are going to use the Emacs editor to edit the file named mydata.
$ Emacs mydata
Like the Vi editor, the Emacs editor is a full-screen editor. When the edited file is a newly created file, the screen is blank except the two rows at the bottom of the screen, and the cursor is placed in the upper left corner of the screen. Shown in the A-2.
Figure A-2 Emacs editor editing screen
A.3.1 META key, Emacs screen, and editing mode
The editing operations of the Emacs editor are similar to those of many common word processors. The Emacs editor only has one input mode for editing mode. If you type any character key, the entered character is entered into the edited file, and all the character keys are used to enter the character, not to enter the command.
You can use the META-x key combination on some computers. On a computer without the META key, META-x is equal to pressing Esc and then x. In some systems, the META key is Alt (or the Alt key on one side ). The Emacs Editing Command is basically a combination of Ctrl-x and META-x key commands (simplified as C-x and M-x ). The operation of the Alt-key command series is basically the same as that of the Ctrl-key command series. That is, you must hold down the Alt-key, press the corresponding key, and then release the two keys at the same time. However, the Esc key command series are slightly different: first, type the Esc key, then open the key, and then type the corresponding button. Esc keys are used more frequently than Alt keys because many early keyboards do not have Alt keys. Therefore, this chapter mainly introduces these META keys. Remember that these META keys can also be replaced by Alt keys on the terminal.
The Emacs editor screen shown in the figure A-1 and A-2 shows the following features: first there is a set of menu options (Buffers, Files, Tools, Edit, Search, Mule, Help ). All Emacs commands can be executed from this drop-down menu. The bottom line of the screen is a small buffer, which is mainly used to display used commands. The last and second lines are called status lines, the following information is displayed: file name, current mode, cursor position, and relative position of the current position in the file. All screens above the status line are called buffers.
The status line displays the status information of the text being edited. A status line consists of the following parts:
-- 1-: ST-F1 BulfferName (mode) -- Lx-Place ----------
The first domain is ST (Status), which indicates whether the modified file has been saved if the file has been modified recently. If the field is an asterisk (**), it indicates that the text has been modified but not saved. If the field is a hyphen ("--"), indicates that the file has not been modified since it was last saved.
The BufferName field indicates the name of the working buffer, which is the name of the currently edited file. Lx indicates the current position of the cursor, and Place field indicates where the cursor is currently in the edited file. For example, if the Place field is 40%, the cursor is currently approximately 40% of the file. In the following example, the status line indicates that the file has not been saved since the last modification. The working buffer name is mydata and the cursor is in the row 4th of the file.
-- 1-: **-F1 mydata (Fundamental) -- L4 -- All -----------
Emacs has the "auto-completion" function in the small buffer zone. This function can save a lot of time for users. When you enter a text string, a long line command, or a file name in the buffer area, you can use the automatic complementing function. If there are more than two options, it also displays the Option List. This function is also effective for Ctrl + x, Ctrl + f, and other file editing functions.
  
   A.4 edit operations and commands
The editing commands of the Emacs editor can perform many operations. Other Editors also have similar editing operations. All the editing commands in the Emacs editor are basically completed and implemented through a command sequence consisting of Ctrl, Alt, or Esc. All these editing commands also have the corresponding command name. You can enter these commands in the small buffer zone. If you enter Emacs from xterm, you can select menu options from the drop-down menu at the top of the editor screen to execute the corresponding commands. This section describes some common commands.
A.4.1 mobile commands
The Emacs editor has a set of basic cursor movement commands. The Ctrl + f command is used to move the cursor forward (right shift) one character, while the Ctrl + B command is used to move the cursor backward (left shift) one character. The Emacs editor treats a file as a stream string rather than a series of text lines. The move cursor command moves the cursor one character to the left along the streaming text (for example, when the cursor moves left at the beginning of the line of the current line, the cursor returns to the end of the last line ). The same is true for moving the cursor forward.
There is also a set of editing commands that allow users to move the cursor or move the cursor on the entire screen as a unit of action in the file. The Ctrl + n command will move the cursor to the next line. If the cursor is in the last line of the screen, the screen will roll down to display the next line of the current line on the screen. The Ctrl + p command will move the cursor to the previous line. If the cursor is at the top of the screen, the screen will roll up to display the first line of the current line on the screen. Ctrl + v command and Esc v command scroll the entire screen text. The Ctrl + v command rolls the text forward and displays the next screen, while the Esc command rolls the text back and displays the previous screen.
Since the above commands were developed without direction keys on the keyboard, many new users may not be used to this operation. In fact, the current keyboard has direction keys, you can use up to down four direction keys to replace these four commands. Even the Emacs program supports PageUp and PageDown keys.
You can also move the cursor in units of measurement, such as words and paragraphs. The META key commands Esc f and Esc B are used to move the cursor before and after words.
The Esc a command can move the cursor to the beginning of the sentence, while the Esc e command will move the cursor to the end of the sentence. The Ctrl + a command will move the cursor
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.