emacs 进阶:了解命令

本文介绍了Emacs中命令的实现方式及使用方法,包括如何通过Emacs Lisp (ELisp)进行命令调用和扩展。文章还讲解了如何利用Emacs内置的功能进行调试,并提供了使用ELisp进行开发的基本指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

命令组成

由于emacs本身就是emacs lisp编写的,所以在emacs中一个命令其实就是一个函数。

(defun helm-M-x (_arg &optional command-name)
"Preconfigured `helm' for Emacs commands.
It is `helm' replacement of regular `M-x' `execute-extended-command'.
Unlike regular `M-x' emacs vanilla `execute-extended-command' command,
the prefix args if needed, can be passed AFTER starting `helm-M-x'.
When a prefix arg is passed BEFORE starting `helm-M-x', the first `C-u'
while in `helm-M-x' session will disable it.

You can get help on each command by persistent action."
快捷键命令说明
M-!shell-command使用系统shell 运行命令并输出到buffer "Shell Command Output"
M-shell-command-on-region运行Shell命令并输出到编辑区域
M-xhelm-M-x运行emacs命令
-term打开buffer"terminal",运行终端
-eshell创建一个交互式的Elisp shell。

可以通过设置shell-file-name变量的值来改变默认shell

 (setq shell-file-name "/bin/bash")

eshell的用法

elisp

快速入门

掌握基本语法规则后就应该多看人家写的代码!
利用edebug更好更快地理解代码
假如想要知道打开文件是怎么操作的。

  1. 首先定位函数的代码。

    • 已知快捷键C-x C-f。依次键入C-h k C-x C-f
      然后就会显示函数的信息,点击helm-files.el就能看到代码。

    • 如果不知道快捷键,则键入C-h a ,利用命令的名称来搜索可能的选项。

    (defun helm-find-files (arg)
      "Preconfigured `helm' for helm implementation of `find-file'.
    Called with a prefix arg show history if some.
    Don't call it from programs, use `helm-find-files-1' instead.
    This is the starting point for nearly all actions you can do on files."
      (interactive "P")
      (let* ((hist            (and arg helm-ff-history (helm-find-files-history)))
             (smart-input     (or hist (helm-find-files-initial-input)))
             (default-input   (expand-file-name (helm-current-directory)))
             (input           (cond (helm-find-file-ignore-thing-at-point
                                     default-input)
                                    ((and (eq major-mode 'org-agenda-mode)
                                          org-directory
                                          (not smart-input))
                                     (expand-file-name org-directory))
                                    ((and (eq major-mode 'dired-mode) smart-input)
                                     (file-name-directory smart-input))
                                    ((and (not (string= smart-input ""))
                                          smart-input))
                                    (t default-input)))
             (input-as-presel (null (nth 0 (file-attributes input))))
             (presel          (helm-aif (or hist
                                            (and input-as-presel input)
                                            (buffer-file-name (current-buffer))
                                            (and (eq major-mode 'dired-mode)
                                                 smart-input))
                                  (if helm-ff-transformer-show-only-basename
                                      (helm-basename it) it))))
        (set-text-properties 0 (length input) nil input)
        (helm-find-files-1 input (and presel (null helm-ff-no-preselect)
                                      (concat "^" (regexp-quote presel))))))
  2. 将光标放到函数名上,键入 C-u C-M-x可添加中断入口。M-x edebug-mod开启后会在菜单栏多出edebug的选项。

  3. 使用命令C-x C-f便会触发中断。

  4. n可单步执行

IELM

这是一个很方便的工具。不同于M-x只能调用命令,IELM可以使用lisp语言,对于实现emacs扩展实在是太便捷了!

更多更详细的内容还是得看文档!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值