在说基本编辑命令之前,我们先加一个小tip,说说如何将函数和键绑定在一起。

(define-key global-map [?\C-l] 'recenter-top-bottom)

define-key函数需要三个参数,第一个是绑定表的名称,不同的模式下的描述表是不同的。第二个参数是键名,第三个参数是键要绑定的函数名。

移动光标

无模式和有模式概述

emacs是一种无模式的编辑器,这也是除了vi之外大部分编辑器的标准做法。每个输入的字符都会直接输入到缓冲区里。编辑要用到的功能函数,就只好绑定到组合键上,主要是Ctrl键,Esc或Alt键的组合键。

比如,最基本的光标移动。如果有上下左右键,就用上下左右键。没有的话,emacs会用C-f向右,C-b向左,C-n向下一行,C-p向上一行。C-a移动到行首,C-e移动到行尾。

大量使用Ctrl和Alt,Esc键,使得手需要经常移动,小指被过度使用。

而vi的采用正常模式和编辑模式分离来解决这个问题,在正常模式下,不能输入字符,所有的字符都被当成命令来执行。此时,j是下一行,k是上一行,h是向左,l向右。效率要比C-n,C-p,C-b,C-f要高。0到行首,$到行尾。

但是vi的问题就是,需要经常在正常模式和编辑模式来回切换。

spacemacs使用evil来模拟vi的这种模式,而且有些键的绑定与标准emacs有所不同。

光标左右移动

移动光标是最基本的命令了,这其中最基本的是光标左右移动,和上下移动。

我们先学习emacs的标准方式:

* 向右一个字符: C-f (forward-char)

* 向左一个字符: C-b (backward-char)

这两个函数都是用C语言实现的,所以没有lisp源码,目前我们暂时先关注lisp部分。

但是,在spacemacs的默认情况下,这两个绑定已经被取消了。因为spacemacs默认是用vi的模式方案,在正常模式下,使用h键左移,l键右移。

l键和右箭头键,绑定到evil-forward-char函数上. 最终,evil-forward-char还是会调用到forward-char来实现移动的功能的:

(evil-define-motion evil-forward-char (count &optional crosslines noerror)
  :type exclusive
  (interactive "<c>" (list evil-cross-lines
                           (evil-kbd-macro-suppress-motion-error)))
  (cond
   (noerror
    (condition-case nil
        (evil-forward-char count crosslines nil)
      (error nil)))
   ((not crosslines)
    ;; for efficiency, narrow the buffer to the projected
    ;; movement before determining the current line
    (evil-with-restriction
        (point)
        (save-excursion
          (evil-forward-char (1+ (or count 1)) t t)
          (point))
      (condition-case err
          (evil-narrow-to-line
            (evil-forward-char count t noerror))
        (error
         ;; Restore the previous command (this one never happend).
         ;; Actually, this preserves the current column if the
         ;; previous command was `evil-next-line' or
         ;; `evil-previous-line'.
         (setq this-command last-command)
         (signal (car err) (cdr err))))))
   (t
    (evil-motion-loop (nil (or count 1))
      (forward-char)
      ;; don't put the cursor on a newline
      (when (and evil-move-cursor-back
                 (not evil-move-beyond-eol)
                 (not (evil-visual-state-p))
                 (not (evil-operator-state-p))
                 (eolp) (not (eobp)) (not (bolp)))
        (forward-char))))))

而左箭头和h键,则是调用的evil-backward-char函数:

(evil-define-motion evil-backward-char (count &optional crosslines noerror)
  :type exclusive
  (interactive "<c>" (list evil-cross-lines
                           (evil-kbd-macro-suppress-motion-error)))
  (cond
   (noerror
    (condition-case nil
        (evil-backward-char count crosslines nil)
      (error nil)))
   ((not crosslines)
    ;; restrict movement to the current line
    (evil-with-restriction
        (save-excursion
          (evil-backward-char (1+ (or count 1)) t t)
          (point))
        (1+ (point))
      (condition-case err
          (evil-narrow-to-line
            (evil-backward-char count t noerror))
        (error
         ;; Restore the previous command (this one never happened).
         ;; Actually, this preserves the current column if the
         ;; previous command was `evil-next-line' or
         ;; `evil-previous-line'.
         (setq this-command last-command)
         (signal (car err) (cdr err))))))
   (t
    (evil-motion-loop (nil (or count 1))
      (backward-char)
      ;; don't put the cursor on a newline
      (unless (or (evil-visual-state-p) (evil-operator-state-p))
        (evil-adjust-cursor))))))

上下移动

就是行间移动,标准emacs的方式是:

* 向下一行:C-n (next-line)

* 向上一行:C-p (previous-line)

这两种方式在spacemacs中,在编辑模式下仍然可以使用。但是正常模式下已经被绑定到其他函数上了,因为有更方便的j和k可以用。

(defun next-line (&optional arg try-vscroll)
  (declare (interactive-only forward-line))
  (interactive "^p\np")
  (or arg (setq arg 1))
  (if (and next-line-add-newlines (= arg 1))
      (if (save-excursion (end-of-line) (eobp))
      ;; When adding a newline, don't expand an abbrev.
      (let ((abbrev-mode nil))
        (end-of-line)
        (insert (if use-hard-newlines hard-newline "\n")))
    (line-move arg nil nil try-vscroll))
    (if (called-interactively-p 'interactive)
    (condition-case err
        (line-move arg nil nil try-vscroll)
      ((beginning-of-buffer end-of-buffer)
       (signal (car err) (cdr err))))
      (line-move arg nil nil try-vscroll)))
  nil)

spacemacs支持在普通模式下使用j来移动到下一行,k来移动到上一行。j绑定的是evil-next-line函数,k绑定的是evil-previous-line函数。

(evil-define-motion evil-next-line (count)
  :type line
  (let (line-move-visual)
    (evil-line-move (or count 1))))

(evil-define-motion evil-previous-line (count)
  :type line
  (let (line-move-visual)
    (evil-line-move (- (or count 1)))))

上面两个函数都是对evil-line-move的封装,evil-next-line的参数是正的,evil-previous-line是负的。

(defun evil-line-move (count &optional noerror)
  (cond
   (noerror
    (condition-case nil
        (evil-line-move count)
      (error nil)))
   (t
    (evil-signal-without-movement
      (setq this-command (if (>= count 0)
                             #'next-line
                           #'previous-line))
      (let ((opoint (point)))
        (condition-case err
            (with-no-warnings
              (funcall this-command (abs count)))
          ((beginning-of-buffer end-of-buffer)
           (let ((col (or goal-column
                          (if (consp temporary-goal-column)
                              (car temporary-goal-column)
                            temporary-goal-column))))
             (if line-move-visual
                 (vertical-motion (cons col 0))
               (line-move-finish col opoint (< count 0)))
             ;; Maybe we should just `ding'?
             (signal (car err) (cdr err))))))))))

移动到行首或行尾

很多时候,我们需要移动到行首或行尾,而不是向左或向右一点一点移动。

我们还是先看emacs的标准实现方式:

* 到行首:C-a (move-beginning-of-line) spacemacs支持

* 到行尾:C-e (move-end-of-line) spacemacs不支持

move-beginning-of-line的实现如下:

(defun move-beginning-of-line (arg)
  (interactive "^p")
  (or arg (setq arg 1))

  (let ((orig (point))
    first-vis first-vis-field-value)

    ;; Move by lines, if ARG is not 1 (the default).
    (if (/= arg 1)
    (let ((line-move-visual nil))
      (line-move (1- arg) t)))

    ;; Move to beginning-of-line, ignoring fields and invisible text.
    (skip-chars-backward "^\n")
    (while (and (not (bobp)) (invisible-p (1- (point))))
      (goto-char (previous-char-property-change (point)))
      (skip-chars-backward "^\n"))

    ;; Now find first visible char in the line.
    (while (and (< (point) orig) (invisible-p (point)))
      (goto-char (next-char-property-change (point) orig)))
    (setq first-vis (point))

    ;; See if fields would stop us from reaching FIRST-VIS.
    (setq first-vis-field-value
      (constrain-to-field first-vis orig (/= arg 1) t nil))

    (goto-char (if (/= first-vis-field-value first-vis)
           ;; If yes, obey them.
           first-vis-field-value
         ;; Otherwise, move to START with attention to fields.
         ;; (It is possible that fields never matter in this case.)
         (constrain-to-field (point) orig
                     (/= arg 1) t nil)))))

最终会调用到我们后面要学的goto-char函数,通过goto-char跳到真正的位置上。

spacemacs支持vi的方式,在普通模式下,0移动到行首,移动到行尾∗0(evil−digit−argument−or−evil−beginning−of−line)∗ (evil-end-of-line)

evil-end-of-line其实还是要调用move-end-of-line函数来实现功能的。

(evil-define-motion evil-end-of-line (count)
  :type inclusive
  (move-end-of-line count)
  (when evil-track-eol
    (setq temporary-goal-column most-positive-fixnum
          this-command 'next-line))
  (unless (evil-visual-state-p)
    (evil-adjust-cursor)
    (when (eolp)
      ;; prevent "c$" and "d$" from deleting blank lines
      (setq evil-this-type 'exclusive))))

移动到缓冲区的头或尾

emacs的标准方式:

* 到缓冲区头 A-< (beginning-of-buffer)

* 到缓冲区尾 A-> (end-of-buffer)

spacemacs支持这两种方式,在正常模式下,还支持”<”键绑定beginning-of-buffer,”>”绑定end-of-buffer的方式。

我们先看下beginning-of-buffer,虽然也是goto-char的封装,但是确实不只是(goto-char 0)这么简单:

(defun beginning-of-buffer (&optional arg)
  (declare (interactive-only "use `(goto-char (point-min))' instead."))
  (interactive "^P")
  (or (consp arg)
      (region-active-p)
      (push-mark))
  (let ((size (- (point-max) (point-min))))
    (goto-char (if (and arg (not (consp arg)))
           (+ (point-min)
              (if (> size 10000)
              ;; Avoid overflow for large buffer sizes!
              (* (prefix-numeric-value arg)
                 (/ size 10))
            (/ (+ 10 (* size (prefix-numeric-value arg))) 10)))
         (point-min))))
  (if (and arg (not (consp arg))) (forward-line 1)))

end-of-buffer的话,除了goto-char之外,还得考虑recenter的问题

(defun end-of-buffer (&optional arg)
  (declare (interactive-only "use `(goto-char (point-max))' instead."))
  (interactive "^P")
  (or (consp arg) (region-active-p) (push-mark))
  (let ((size (- (point-max) (point-min))))
    (goto-char (if (and arg (not (consp arg)))
           (- (point-max)
              (if (> size 10000)
              ;; Avoid overflow for large buffer sizes!
              (* (prefix-numeric-value arg)
                 (/ size 10))
            (/ (* size (prefix-numeric-value arg)) 10)))
         (point-max))))
  ;; If we went to a place in the middle of the buffer,
  ;; adjust it to the beginning of a line.
  (cond ((and arg (not (consp arg))) (forward-line 1))
    ((and (eq (current-buffer) (window-buffer))
              (> (point) (window-end nil t)))
     ;; If the end of the buffer is not already on the screen,
     ;; then scroll specially to put it near, but not at, the bottom.
     (overlay-recenter (point))
     (recenter -3))))

移动到任意位置

emacs提供了两个函数,可以跳到任意一行,或者是任意一个字符。

* A-g g 或 A-g A-g (goto-line n) :跳转到第n行

* A-g c (goto-char n): 跳转到第n个字符

spacemacs还支持vi的方式来跳转行

* 行号 G (evil-goto-line),如果没有行号,则跳到缓冲区末尾

goto-char不出意料的,是用C实现的。

我们先来看看goto-line:

(defun goto-line (line &optional buffer)
  (declare (interactive-only forward-line))
  (interactive
   (if (and current-prefix-arg (not (consp current-prefix-arg)))
       (list (prefix-numeric-value current-prefix-arg))
     ;; Look for a default, a number in the buffer at point.
     (let* ((default
          (save-excursion
        (skip-chars-backward "0-9")
        (if (looking-at "[0-9]")
            (string-to-number
             (buffer-substring-no-properties
              (point)
              (progn (skip-chars-forward "0-9")
                 (point)))))))
        ;; Decide if we're switching buffers.
        (buffer
         (if (consp current-prefix-arg)
         (other-buffer (current-buffer) t)))
        (buffer-prompt
         (if buffer
         (concat " in " (buffer-name buffer))
           "")))
       ;; Read the argument, offering that number (if any) as default.
       (list (read-number (format "Goto line%s: " buffer-prompt)
                          (list default (line-number-at-pos)))
         buffer))))
  ;; Switch to the desired buffer, one way or another.
  (if buffer
      (let ((window (get-buffer-window buffer)))
    (if window (select-window window)
      (switch-to-buffer-other-window buffer))))
  ;; Leave mark at previous position
  (or (region-active-p) (push-mark))
  ;; Move to the specified line number in that buffer.
  (save-restriction
    (widen)
    (goto-char (point-min))
    (if (eq selective-display t)
    (re-search-forward "[\n\C-m]" nil 'end (1- line))
      (forward-line (1- line)))))

evil-goto-line写得简短一些:

(evil-define-motion evil-goto-line (count)
  :jump t
  :type line
  (if (null count)
      (with-no-warnings (end-of-buffer))
    (goto-char (point-min))
    (forward-line (1- count)))
  (evil-first-non-blank))

高效移动

重复执行命令

如果一行一行的移动,实在是太慢了,我们可以使用重复命令,给函数传递一个参数。

标准emacs的做法是Esc + 数字和C-u加数字两种方式:

* Esc n + 命令:执行n次命令。如果无法执行完n次,就尽最大的努力。比如向下移动n行,到是没到n行就到文件末尾了。那么就停在文件末尾。

例:

Esc 10 C-n,向下移动10行

* (universal-argument)函数,它绑定在C-u键上。

universal-argument如果不指定参数的话,默认执行4次。

但是在spacemacs上,universal-argument函数绑定在”空格 u”和”Alt-m u”两个键上。

C-u在spacemacs中被移做绑定到evil-scroll-up上,用于翻屏。

居中重绘屏幕

有的时候,需要重新绘制一下屏幕,让我们移动到的那行变为中心:

C-l (recenter-top-bottom)

(defun recenter-top-bottom (&optional arg)
  "Move current buffer line to the specified window line.
With no prefix argument, successive calls place point according
to the cycling order defined by `recenter-positions'.

A prefix argument is handled like `recenter':
 With numeric prefix ARG, move current line to window-line ARG.
 With plain `C-u', move current line to window center."
  (interactive "P")
  (cond
   (arg (recenter arg))         ; Always respect ARG.
   (t
    (setq recenter-last-op
      (if (eq this-command last-command)
          (car (or (cdr (member recenter-last-op recenter-positions))
               recenter-positions))
        (car recenter-positions)))
    (let ((this-scroll-margin
       (min (max 0 scroll-margin)
        (truncate (/ (window-body-height) 4.0)))))
      (cond ((eq recenter-last-op 'middle)
         (recenter))
        ((eq recenter-last-op 'top)
         (recenter this-scroll-margin))
        ((eq recenter-last-op 'bottom)
         (recenter (- -1 this-scroll-margin)))
        ((integerp recenter-last-op)
         (recenter recenter-last-op))
        ((floatp recenter-last-op)
         (recenter (round (* recenter-last-op (window-height))))))))))

undo

做错了,撤销是很关键的操作。

在标准emacs中,使用undo函数来进行这个操作。它绑定到C-_或C-/或C-x u三个键上。

在spacemacs中,C-x u被绑定到undo-tree-visualize函数上。 还可以用”空格 a u”来访问它。

(defun undo-tree-visualize ()
  "Visualize the current buffer's undo tree."
  (interactive "*")
  (deactivate-mark)
  ;; throw error if undo is disabled in buffer
  (when (eq buffer-undo-list t)
    (user-error "No undo information in this buffer"))
  ;; transfer entries accumulated in `buffer-undo-list' to `buffer-undo-tree'
  (undo-list-transfer-to-tree)
  ;; add hook to kill visualizer buffer if original buffer is changed
  (add-hook 'before-change-functions 'undo-tree-kill-visualizer nil t)
  ;; prepare *undo-tree* buffer, then draw tree in it
  (let ((undo-tree buffer-undo-tree)
        (buff (current-buffer))
    (display-buffer-mark-dedicated 'soft))
    (switch-to-buffer-other-window
     (get-buffer-create undo-tree-visualizer-buffer-name))
    (setq undo-tree-visualizer-parent-buffer buff)
    (setq undo-tree-visualizer-parent-mtime
      (and (buffer-file-name buff)
           (nth 5 (file-attributes (buffer-file-name buff)))))
    (setq undo-tree-visualizer-initial-node (undo-tree-current undo-tree))
    (setq undo-tree-visualizer-spacing
      (undo-tree-visualizer-calculate-spacing))
    (make-local-variable 'undo-tree-visualizer-timestamps)
    (make-local-variable 'undo-tree-visualizer-diff)
    (setq buffer-undo-tree undo-tree)
    (undo-tree-visualizer-mode)
    ;; FIXME; don't know why `undo-tree-visualizer-mode' clears this
    (setq buffer-undo-tree undo-tree)
    (set (make-local-variable 'undo-tree-visualizer-lazy-drawing)
     (or (eq undo-tree-visualizer-lazy-drawing t)
         (and (numberp undo-tree-visualizer-lazy-drawing)
          (>= (undo-tree-count undo-tree)
              undo-tree-visualizer-lazy-drawing))))
    (when undo-tree-visualizer-diff (undo-tree-visualizer-show-diff))
    (let ((inhibit-read-only t)) (undo-tree-draw-tree undo-tree))))

而C-_,C-/,在spacemacs中,被绑定在undo-tree-undo上。

小结

功能 函数名 快捷键 leader键
光标右移 forward-char
evil-forward-char l
光标左移 backward-char
evil-backward-char h
下移一行 next-line 正常模式C-n无效
evil-next-line j
上移一行 previous-line 正常模式C-p无效
evil-previous-line k
光标移至行首 move-beginning-of-line C-a
evil-digit-argument-or-evil-beginning-of-line 0
光标移至行尾 move-end-of-line
evil-end-of-line $
跳转到某一行 goto-line A-g g或A-g A-g
evil-goto-line G
跳到某一字符 goto-char A-g c
跳到缓冲区头 beginning-of-buffer A-<或>
跳到缓冲区尾 end-of-buffer A->或>
重复执行 universal-argument A-m u 空格 u
居中重绘屏幕 recenter-top-bottom C-l
撤销上一次的操作 undo
undo-tree-visualize C-x u
undo-tree-undo C-_或C-/

我也说说Emacs吧(4) - 光标的移动的更多相关文章

  1. Emacs 从入门到精通

    1 前言 不想再说废话了,既然你会阅读这篇文档,说明你多少对Emacs有 些兴趣,或者已 经非常熟悉Emacs的基础操作了,并且希望有所提高.因此我不需要再把"编辑器 之神,还是神的编辑器& ...

  2. [Emacs] 常用快捷键-- 生存指南

    Emacs 常用快捷键--生存指南 主要用来记录自己常用到的快捷键,记住这些快捷键可以保证你在Emacs中生存. 有可能不全,但是够用了(简单写文本). 保存和退出 使用 C-x C-s 保存文件. ...

  3. 一些有用的 Emacs 配置(窗口快速切换、一键透明效果、任意位置删除整行等)

    本篇文章记录的是一些有用的 Emacs 配置,有些是自己原创,有些是借鉴别人(能记起来出处的我放了链接). 规定:C 代表 Ctrl,M 代表 Alt. 1.设置一次跳跃 n 行的快捷键 按 C-M- ...

  4. emacs search, 讲的很清楚。

    默认情况下,Emacs采用了一种很待殊的”增量搜索”的功能,虽然它与我们常用的搜索方法在操作习惯上有很大的不同,但习惯后确实是十分的方便. 要让Emacs开始执行搜索,可以按C-s或C-r,前者是从光 ...

  5. Emacs 快速指南(中文翻译)

      Emacs 快速指南 目录 1. 小结(SUMMARY) 2. 基本的光标控制(BASIC CURSOR CONTROL) 3. 如果 EMACS 失去响应(IF EMACS STOPS RESP ...

  6. [转载]生活在 Emacs 中

    Brian Bilbrey2002 年 8 月 20 日发布 教程简介 本教程讲什么? Emacs 是一个流行的无模式文本编辑器,有许多强大的功能.本教程将教您使用 Emacs 的基础知识.为了让您很 ...

  7. Emacs 快速指南 - 原生中文手册

    Emacs 快速指南 -折叠目录 1. 小结(SUMMARY) 2. 基本的光标控制(BASIC CURSOR CONTROL) 3. 如果 EMACS 失去响应(IF EMACS STOPS RES ...

  8. Emacs显示光标在哪个函数

    Emacs24中打开which-function-mode即可. 在.emacs中添加一行: (which-function-mode 1) 调整which-function在mode-line中的显 ...

  9. ubuntu 下emacs 配置

    (set-language-environment 'Chinese-GB) (set-keyboard-coding-system 'utf-8) (set-clipboard-coding-sys ...

随机推荐

  1. 20145322何志威 《Java程序设计》课程总结

    课程总结 每周读书笔记链接汇总 •第一周读书笔记 •第二周读书笔记 •第三周读书笔记 •第四周读书笔记 •第五周读书笔记 •第六周读书笔记 •第七周读书笔记 •第八周读书笔记 •第九周读书笔记 •第十 ...

  2. Spring笔记2——Spring中Bean的装配

    1.引言 Spring中,对象无需自己负责查找或创建与其关联的其他对象,而是由容器负责把需要相互协作的对象引用赋予各个对象.创建应用对象之间的协作关系的行为通常称为装配(Wiring),这也是依赖注入 ...

  3. ThinkPHP将上传问件添加到数据库

    <?php namespace Home\Controller; /***************** use Think\Controller; ****命名空间****/ class Mes ...

  4. Linux命令:chmod、chgrp、chown的区别

    chmod是更改文件的权限: chgrp只是更改文件的属组: chown是更改文件的属主与属组. 1.chmod:更改文件的权限 文件权限的设置方式有两种,分别是数字和标记. mode : 权限设定字 ...

  5. sqlite的Top筛选

    select [CollectDateTime] as '时间',[Channel_34] as '通道34',[Channel_54] as '通道54' from [DataTable] wher ...

  6. 【GAN】GAN的原理及推导

    把GAN的论文看完了, 也确实蛮厉害的懒得写笔记了,转一些较好的笔记,前面先贴一些 原论文里推理部分,进行备忘. GAN的解释 算法流程 GAN的理论推理 转自:https://zhuanlan.zh ...

  7. UVa 11354 邦德(最小瓶颈路+LCA)

    https://vjudge.net/problem/UVA-11354 题意: 有n个城市m条道路,每条道路有一个危险系数.先在有若干个询问,要求找到一条从s到t的路,使得途径所有边的最大危险系数最 ...

  8. jmeter随笔

    1. ${__time(yyyy-MM-dd HH:mm:ss,)} 2. import use.GetRsaContent; String pfxPath = "pfx";Str ...

  9. flex-wrap:wrap弹性盒布局,自动换行

    给父盒子ul{display:flex; flex-wrap:wrap; }

  10. Java的 final 关键字

    本文主要探讨Java final 关键字修饰变量时的用法. !!!!文末有彩蛋!!!! 1.修饰类 当用final修饰一个类时,表明这个类不能被继承.也就是说,如果一个类你永远不会让他被继承,就可以用 ...