Introduction to Emacs: common custom functions

Source: Internet
Author: User
Tags isearch

One of the fun of using Emacs is to see that your editing efficiency increases with the accumulation of time. With the help of elisp, you can copy the excellent functions of other editors to make it feel great. Babar K. Zafar has an article on the webpage that collects Emacs skills. Very useful. I especially like functions that implement common VI functions: in this way, I don't have to lose the previously used editing functions. When does elicpse support its own scripting language. We can easily read and manipulate eclipse runtime and Java ast without creating N classes to add file links to the console. Ward cunnhan, wiki creator, is doing the job. I hope he can do it quickly. The following describes some of your frequently used skills. The original Article has more skills and is strongly recommended. Let's talk about how to try these skills first. Emacs is simply the interpretation environment of elisp, which happens to implement the editor. Therefore, it is easier to check with elisp functions. Suppose we open an Emacs Buffer: Now C-x 2, add a new buffer, then C-x O, and switch the cursor to the new buffer: switch to the * Scratch * buffer. The default * Scratch * buffer mode provided by Emacs is elisp-mode, which is suitable for editing and testing elisp. Enter the C-x B command, then press scratch, and press enter to enter the code in the * Scratch * buffer. Then, M-x eval-buffer, the entered code is loaded and run. The following are the techniques provided by Babar: Enhanced searchC-s in Emacs corresponds to progressive search. However, we need to search for a certain pattern more often, so the most commonly used is incremental Regular Expression search. There is an annoying problem with regular expression search: The Search end time mark does not necessarily stop at the beginning of the matching string. Fortunately, this problem is easy to solve: (Local-set-Key [(control S)] 'isearch-forward-Regexp)
(Local-set-Key [(Control R)] 'isearch-backward-Regexp); always end searches at the beginning of the matching expression.
(Add-hook 'isearch-mode-end-hook 'm-Goto-match-beginning) (defun custom-Goto-match-beginning ()
"Use with Isearch hook to end search at first char of match ."
(When Isearch-Forward (goto-Char Isearch-other-end) rebind the standard search keys C-S and C-R from the first two rows and replace Isearch with RegEx-Isearch. Custom functions are added to the next three rows. The key statement is (goto-Char Isearch-other-end), which ensures that the cursor stays at the beginning of the matching string instead of the default end. Automatic completionThe hippie-Expansion of Emacs is very powerful and can help us to automatically complete many repeated inputs. I used to use eclipse and Vim, So I bound the automatically completed Ctrl-space to the automatically completed, and bound the CTRL-ENTER to the Ctrl-space default corresponding set-mark:
(global-set-key [(control space)] 'hippie-expand)
(global-set-key [(control return)] 'set-mark-command)
(require 'hippie-exp)
(setq hippie-expand-try-functions-list
         '(try-expand-dabbrev
             try-expand-dabbrev-all-buffers
               try-expand-dabbrev-from-kill
               try-complete-file-name-partially
               try-complete-file-name
               try-complete-lisp-symbol-partially
               try-complete-lisp-symbol
               try-expand-whole-kill))
  Brackets automatically completedEmacs is used for programming most of the time. Programming is rarely used to encounter unpaired parentheses. In this case, why not let Emacs pair us? The following statements are automatically completed (, [, {, and <.
(setq skeleton-pair t)
(local-set-key (kbd "[") 'skeleton-pair-insert-maybe)
(local-set-key (kbd "(") 'skeleton-pair-insert-maybe)
(local-set-key (kbd "{") 'skeleton-pair-insert-maybe)
(local-set-key (kbd "<") 'skeleton-pair-insert-maybe)
  Move the entire rowThis feature is learned from the popular editor textmate on the Apple machine. In textmate, alt-up will move the entire line of text where the cursor is located up one line, while alt-down will move the entire line of text where the cursor is located down one line. This is useful when adjusting the statement order. With this function, we can say goodbye to the tedious "select all rows-cut-move the mouse-line feed-paste. (Local-set-Key [(meta up)] 'move-line-up)
(Local-set-Key [(meta down)] 'move-line-down)
(defun move-line (&optional n)
 "Move current line N (1) lines up/down leaving point in place."
 (interactive "p")
 (when (null n)
    (setq n 1))
 (let ((col (current-column)))
    (beginning-of-line)
    (next-line 1)
    (transpose-lines n)
    (previous-line 1)
    (forward-char col)))
(defun move-line-up (n)
 "Moves current line N (1) lines up leaving point in place."
 (interactive "p")
 (move-line (if (null n) -1 (- n)))) 
(defun move-line-down (n)
 "Moves current line N (1) lines down leaving point in place."
 (interactive "p")
 (move-line (if (null n) 1 n)))
  Line feed alignmentThis seemingly simple function is too useful !!! I don't know how much unnecessary cursor movement is saved. Most of the time, after modifying a line of statements, we want to jump to the next line immediately, and the cursor automatically moves to the correct indent position. In a normal Editor, The cursor must move to the end of the line before wrapping the line, and then press Enter. CTRL-o in VI solves this problem, so that we can switch to the next line anywhere in a row and indent it automatically without worrying about dividing the last line into two halves. Emacs supports Ctrl-O with the following configuration. Powerful enough?
(local-set-key [(control o)] 'vi-open-next-line)
(defun vi-open-next-line (arg)
 "Move to the next line (like vi) and then opens a line."
 (interactive "p")
 (end-of-line)
 (open-line arg)
 (next-line 1)
 (indent-according-to-mode))
Matching bracketsIt is also a cool feature from VI. When the cursor is in parentheses, enter %. The cursor automatically jumps to another matching bracket.
(local-set-key "%" 'match-paren)
(defun match-paren (arg)
 "Go to the matching parenthesis if on parenthesis otherwise insert %."
 (interactive "p")
 (cond ((looking-at "//s/(") (forward-list 1) (backward-char 1))
        ((looking-at "//s/)") (forward-char 1) (backward-list 1))
        (t (self-insert-command (or arg 1)))))
  Copy current rowMost of the time, we want to place the entire line of statements with the cursor in the kill-ring (Windows clipboard. But selecting the entire line, CTRL-W is too cumbersome. In slickedit, if no area is selected, the copy command will automatically copy the entire line where the cursor is located, and cut the entire line where the cursor is located. Emacs can do this: (defadvice kill-ring-save (before slickcopy activate compile)
"When called interactively with no active region, copy a single line instead ."
(Interactive
(If Mark-active (List (region-beginning) (region-end ))
(List (line-beginning-position)
(Line-beginning-Position 2 )))))

(Defadvice kill-region (before slickcut activate compile)
"When called interactively with no active region, kill a single line instead ."
(Interactive
(If Mark-active (List (region-beginning) (region-end ))
(List (line-beginning-position)
(Line-beginning-Position 2 )))))

 

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.