【问题标题】:Shortcut for inserting environments in `org-mode`在 `org-mode` 中插入环境的快捷方式
【发布时间】:2013-10-02 19:41:07
【问题描述】:

我正在使用org-mode 来组织自己(到目前为止非常有用!)。不过写的有点烦

  #+begin_comment
  ...
  #+end_comment

每次我想插入一个环境。

问题

对于给定的环境是否有插入#+begin_#+end_ 的快捷方式?

以同样的方式C-c C-o comment RET 插入

\begin{comment}

\end{comment}

latex-mode.

【问题讨论】:

    标签: emacs org-mode key-bindings


    【解决方案1】:

    Org 有一个名为“Easy templates”的工具:http://orgmode.org/manual/Easy-Templates.html

    缺少评论模板,但您可以添加:

    (add-to-list 'org-structure-template-alist '("C" "#+begin_comment\n?\n#+end_comment"))
    

    并通过键入<C 后跟TAB 来使用它。

    或者,您可以使用 yasn-p。

    【讨论】:

    • 我刚刚发现还有一个插入src-blocks的按键:C-c C-v d.
    • 而且,更好的是,如果在代码块中调用它会将其拆分为两个单独的代码块,如果在代码块之外调用它将用新代码块围绕当前选择
    【解决方案2】:

    现在相应的模板部分称为Structure Template,插入序列由C-c C-, 调用。我没有(require 'org-tempo),它被描述为支持像<s TAB 这样的插入键。

    评论环境已在org-structure-template-alist 中定义。所以评论将由

    插入
    C-c C-, C
    

    仍然可以添加用户定义的序列,例如,

    C-c C-, [TAB|RET|SPC] src python :results output :session
    

    交付

    #+begin_src python :results output :session
    #+end_src
    

    (emacs 25.2.2,组织模式 9.2)

    【讨论】:

      【解决方案3】:

      您可以查看“org-auctex-keys.el”,这是我创建的一种次要模式,用于在 Org 文档中提供 AUCTeX 键绑定。

      在这种情况下,您可以使用 C-c C-e 插入环境(提示输入环境名称),就像 AUCTeX 所做的那样。

      如果您有兴趣,请查看https://github.com/fniessen/org-auctex-key-bindings

      【讨论】:

        【解决方案4】:

        不像 Michael Markert 的回答那样优雅,但可能更具扩展性。

        1) 你可以选择一个区域并在它周围放置方块,或者你可以将方块放在点上。

        2) 关键字扩展和历史记录。

        3) 击键:C-c b

        该命令可以进一步扩展。例如,对于 src 块,可以支持各种开关,如 -n -r 和导出到文件。

        (defun list-major-modes ()
          "Returns list of potential major mode names (without the final -mode).
        Note, that this is guess work."
          (interactive)
          (let (l)
            (mapatoms #'(lambda (f) (and
                         (commandp f)
                         (string-match "-mode$" (symbol-name f))
                         ;; auto-loaded
                         (or (and (autoloadp (symbol-function f))
                              (let ((doc (documentation f)))
                            (when doc
                              (and
                               (let ((docSplit (help-split-fundoc doc f)))
                                 (and docSplit ;; car is argument list
                                  (null (cdr (read (car docSplit)))))) ;; major mode starters have no arguments
                               (if (string-match "[mM]inor" doc) ;; If the doc contains "minor"...
                                   (string-match "[mM]ajor" doc) ;; it should also contain "major".
                                 t) ;; else we cannot decide therefrom
                               ))))
                         (null (help-function-arglist f)))
                         (setq l (cons (substring (symbol-name f) 0 -5) l)))))
            (when (called-interactively-p 'any)
              (with-current-buffer (get-buffer-create "*Major Modes*")
            (clear-buffer-delete)
            (let ((standard-output (current-buffer)))
              (display-completion-list l)
              (display-buffer (current-buffer)))))
            l))
        
        (defvar org-insert-block-hist nil
          "History for command `org-insert-block'")
        (defvar org-insert-block-hist/src:major nil
          "History for major mode in org src blocks.")
        (defvar org-insert-block-list (append org-protecting-blocks
                           '("comment" ""))
          "List of block types offered as completion for command `org-insert-block'")
        ;; block_src switches: -n () -r (references) -l "((%s))" (label format) -k (keep labels)
        (defvar org-insert-block-list-specials
          "Assoc list of Commands for reading additional specification of org-blocks.")
        (setq org-insert-block-list-specials
              '(("src" . (concat " " (completing-read "Major mode:"
                                (list-major-modes)
                                nil nil
                                (car org-insert-block-hist/src:major)
                                '(org-insert-block-hist/src:major . 1)
                                )))))
        
        (defun org-insert-block (bl &optional b e attributes)
          "Put region between b and e into org-block of kind bl.
        If b or e is nil then put org-block limiters around point.
        The string attributes is inserted behind the string #+begin_... "
          (interactive
           (let ((usereg (use-region-p))
             (blKind (completing-read "Input block kind (tab: completion, uparrow: history):"
                       org-insert-block-list nil nil (car org-insert-block-hist) '(org-insert-block-hist . 1))))
             (list
              blKind
              (when usereg (region-beginning))
              (when usereg (region-end))
              (let ((spec (assoc blKind org-insert-block-list-specials)))
            (when spec (eval (cdr spec)))
            ))))
          (let ((begBlock (concat "\n#+begin_" bl attributes "\n"))
            (endBlock (concat "\n#+end_" bl "\n")))
            (if (and b e)
            (save-restriction
              (narrow-to-region b e)
              (goto-char (point-min))
              (insert begBlock)
              (goto-char (point-max))
              (insert endBlock)
              (indent-region (point-min) (point-max)))
              (let ((p (point)))
            (insert endBlock)
            (goto-char p)
            (insert begBlock))
              )))
        (add-hook 'org-mode-hook '(lambda ()
                        (local-set-key (kbd "C-c b") 'org-insert-block)))
        

        【讨论】:

        • 上面的评论本身就值得一票(加上@malcook的东西)
        猜你喜欢
        • 2010-11-10
        • 1970-01-01
        • 1970-01-01
        • 2014-02-16
        • 1970-01-01
        • 2013-06-22
        • 1970-01-01
        • 2018-11-06
        • 1970-01-01
        相关资源
        最近更新 更多