閱讀941 返回首頁    go 阿裏雲 go 技術社區[雲棲]


我也說說Emacs吧(3) - 文件基本操作

Spacemacs文件基本操作

有了前兩講的積累,我們知道了:
1. 我們既要學習emacs的操作,也要學習vi的操作
2. emacs是一個可擴展的平台,我們並不是從頭配置,而是使用一套成熟的集成方案,spacemacs. spacemacs已經集成好了很多插件,比如模擬vi的evil,自動補全的ido等
3. emacs本質上是一些函數的組合,通過Alt-x執行函數名,或者通過綁定到快捷鍵上使用,其本質是一樣的

有了以上基礎知識,我們正式開始學習emacs. 我們先從最簡單的打開和保存文件開始。

文件打開和保存

打開文件

emacs的標準做法是使用find-file函數,綁定在C-x C-f鍵上。
在spacemacs中,曾經默認將C-x C-f綁定到ido-find-file上。而現在,C-x C-f綁定到spacemacs/helm-find-files上:

(defun spacemacs/helm-find-files (arg)
  "Custom spacemacs implementation for calling helm-find-files-1.
Removes the automatic guessing of the initial value based on thing at point. "
  (interactive "P")
  (let* ((hist (and arg helm-ff-history (helm-find-files-history)))
         (default-input hist)
         (input (cond ((and (eq major-mode 'dired-mode) default-input)
                       (file-name-directory default-input))
                      ((and (not (string= default-input ""))
                            default-input))
                      (t (expand-file-name (helm-current-directory))))))
    (set-text-properties 0 (length input) nil input)
    (helm-find-files-1 input)))

也可以通過『空格 f f』的方式來使用spacemacs/helm-find-files.
可能通過【空格 f F】的方式來直接使用helm插件包中的helm-find-files:

(defun helm-find-files (arg)
  (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 (and helm-ff-transformer-show-only-basename
                                       (null hist))
                                  (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))))))

Helm和ido一樣,也是一個交互式補全的插件:https://emacs-helm.github.io/helm/
Helm隻在emacs 24.4以上版本上才可以用。

打開另一個文件

標準emacs做法是find-alternate-file函數,綁定到C-x C-v鍵上。spacemacs上,這個鍵被綁定到ido-find-alternate-file函數上。

(defun ido-find-alternate-file ()
...
  (interactive)
  (ido-file-internal 'alt-file 'find-alternate-file nil "Find alternate file: "))

將另一個文件的內容插入到當前文件

標準emacs的做法是insert-file函數,綁定到C-x i鍵上。spacemacs默認綁定到ido-insert-file函數上。

(defun ido-insert-file ()
  (interactive)
  (ido-file-internal 'insert 'insert-file nil "Insert file: " nil nil 'ido-enter-insert-buffer))

以二進製的方式打開文件

emacs默認沒有將其綁定到快捷鍵上,這個功能是hexl-find-file函數來實現的。打開後會進入hexl-mode.
spacemacs將其綁定到『空格 f h』上。

保存文件

保存文件不涉及交互操作,所以ido和helm都不管,還是用的emacs默認的save-buffer函數。
emacs默認將其綁定在C-x C-s鍵上,spacemacs又提供了"空格 f s"的快捷鍵

(defun save-buffer (&optional arg)
  (interactive "p")
  (let ((modp (buffer-modified-p))
    (make-backup-files (or (and make-backup-files (not (eq arg 0)))
                   (memq arg '(16 64)))))
    (and modp (memq arg '(16 64)) (setq buffer-backed-up nil))
    (if (and modp
             (buffer-file-name)
             (not noninteractive)
             (not save-silently))
    (message "Saving file %s..." (buffer-file-name)))
    (basic-save-buffer (called-interactively-p 'any))
    (and modp (memq arg '(4 64)) (setq buffer-backed-up nil))))

另存為文件

另存為文件又涉及交互操作了,標準emacs使用write-file函數,而spacemacs使用ido-write-file函數。它們都是綁定到C-x C-w鍵上。

(defun ido-write-file ()
  (interactive)
  (let ((ido-process-ignore-lists t)
    (ido-work-directory-match-only nil)
    (ido-ignore-files (cons "[^/]\\'" ido-ignore-files))
    (ido-report-no-match nil)
    (ido-confirm-unique-completion t)
    (ido-auto-merge-work-directories-length -1))
    (ido-file-internal 'write 'write-file nil "Write file: " nil nil 'ignore)))

退出emacs

spacemacs對此沒有擴展,都是使用save-buffers-kill-terminal函數,綁定到C-x C-c鍵上。

文件操作小結

本節涉及的插件:ido, helm

|功能|函數|快捷鍵|leader鍵|
|-----|-----|-----|
|打開文件|spacemacs/helm-find-files|C-x C-f|空格 f f|
||find-file|無|無|
||ido-find-file|無|無|
||helm-find-files|無|空格 f F|
|打開另一個文件|ido-find-alternate-file|C-x C-v|無|
||find-alternate-file|無|無|
|將另一個文件的內容插入到當前文件|ido-insert-file|C-x i|無|
||insert-file|無|無|
|保存文件|save-buffer|C-x C-s|空格 f s|
|另存為文件|ido-write-file|C-x C-w|無|
|以二進製的方式打開文件|hexl-find-file|無|空格 f h|
|退出emacs|save-buffers-kill-terminal|C-x C-c|無|

最後更新:2017-05-28 00:43:21

  上一篇:go  阿裏雲6.1-6.10雲主機大促 ECS雲服務器和獨享雲虛擬主機幸運券分享
  下一篇:go  福利!阿裏雲獨享雲虛擬主機幸運券免費領取