Emacs Tips for binding keyboards

Source: Internet
Author: User

Emacs is a keyboard-bound technique. Foolproof method of binding

Many people are bound to some of the more special keys when it is not clear in

(Global-set-key ... ' My-funtion)

What to write in. Especially in the xterm in the time is more overwhelmed. In fact, there is a foolproof way to ensure that you must write the right. This approach is:

    1. M-x global-set-key RET Interactive bind your keys.
    2. C-x ESC ESC to bring up the previous "complex command".

Well, you can now see in the Minibuffer what you should write in. Emacs.

If you still fail ...

If you find that Emacs is not responding to your keys at the 1th step, then you should suspect that your window manager has blocked the button. For example, my FVWM settings set C-F3 to open a fvwmcommand, so Emacs cannot receive this button. If I want to bind a function to C-F3, I must let FVWM leave c-f3.

Binding a new prefix key

In fact, the above method only allows you to bind an existing prefix. You might want to bind a save-buffer to "C-c c-w c-b a". The above approach will not work. We must use a different approach:

(Global-set-key (kbd "C-c c-w c-b A") ' Save-buffer]
A little explanation.

The above "C-c c-w c-b A" is automatically the "C-c c-w", "C-c c-w c-b" are defined as a prefix-command. You can see them this way:

    1. C-h c-b Display Bindings
    2. C-x o switch to the window that displays the bindings
    3. C-x C-q Eliminate the read-only property of this window
    4. M-x delete-non-matching-lines RET prefix ret deletes all lines that do not contain the word "prefix".

Now you clearly see the "C-c c-w", "C-c c-w c-b" are all prefixcommand?

Create a new prefix command the above method only works on the key sequence where the key is already prefix command, and if your first key is not a prefix, then an error occurs. You can try:
(Global-set-key (kbd "c-z c-c c-w B") ' Find-file]

Error occurred: (Error "Key sequence c-z c-c c-w B uses invalidprefix characters")

So you have to set the first key to prefix in advance:

(Define-prefix-command ' Ctl-z-map) (Global-set-key (kbd "c-z") ' Ctl-z-map)

and then use

(Global-set-key (kbd "c-z c-c c-w B") ' Find-file]

On the line. Both "C-z c-c" and "C-z c-c c-w" are automatically defined as Prefixcommand.

Binding Chinese Commands

Now let me give you an example of how prefix command works. We can bind the Chinese disk two characters to Save-buffer. This way you can use the Chinese input method to type "save" two words, it is possible to keep the current buffer.

(Define-prefix-command ' Save-map) (Global-set-key (kbd "save") ' Save-map ') (Define-key-map (kbd "dish") ' Save-buffer)

Funny, huh? You can guess what's going on in there, right? It's too easy, isn't it? When you enter "save", you see Minibuffer:

This is because we bind the word "save" to the Prefix-command of -map . When you read "save", Emacs waits for the next command, which is defined in the map that is stored in-map . When it reads "Disk", it executes the save-buffer.

Note, however, that you really need to enter "save" two words in the document to hit C-q first. Just now I have played several times c-q, really tired ah. Or use some less commonly used phrases better, or add a ctrl what prefix, like this, "c-z disk."

(Define-prefix-command ' Ctl-z-map) (Global-set-key (kbd "c-z") ' Ctl-z-map) (Define-key ctl-z-map (kbd "Disk") ' Save-buffer ')

Look at us a little bit more BT:

(Define-prefix-command ' Ctl-z-map) (Global-set-key (kbd "c-z") ' Ctl-z-map) (Define-key ctl-z-map (KBD) "Save me the disk!" ") ' Save-buffer)

Haiyo Yes sir!!

Define-key will automatically create many prefix command. However, it is a good thing that you explicitly define the prefix command with Define-prefix-command, that is, you can easily define more commands in your prefix without having to write the entire prefix again.

(Define-prefix-command ' Ctl-z-map) (Define-prefix-command ' Save-map) (Define-prefix-command ' Plate-map) (Global-set-key (kbd "c-z") ' Ctl-z-map) (Define-key Ctl-z-map (kbd "save") ' Save-map ') (Define-key-map (kbd "Disk") ' Disc-map ') (Define-key plate-map (KBD "!) ") ' Save-buffer" (Define-key disk-map (kbd "to") ' Write-file "(Define-key disk-map (kbd" Exit ")   (Lambda () (    Interactive) c2/> (Save-buffer)    (Kill-emacs)))

So, to the "C-z disk-" This time, we defined 3 branches:

    • "C-z Disk!" "Means to save this file;
    • "C-z save to", means saving to another file;
    • "C-z Save", this does not have to explain it.

Another big benefit of customizing the prefix command is that you can modify the binding of many keys by modifying the top-level bind to the prefix command. For example, we can easily change the "save" to "saving":

(Define-key Ctl-z-map (kbd "Save") ' Disc-map]

In this way, "c-z Save! "," c-z Save to "," C-z Save Exit "has" C-Z disk! " "," C-z Save to "and" c-z save "meaning.

Emacs Tips for binding keyboards

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.