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

Spacemacs文件基本操作

有了前兩講的積累,我們知道了:

  1. 我們既要學(xué)習(xí)emacs的操作,也要學(xué)習(xí)vi的操作
  2. emacs是一個(gè)可擴(kuò)展的平臺,我們并不是從頭配置,而是使用一套成熟的集成方案,spacemacs. spacemacs已經(jīng)集成好了很多插件,比如模擬vi的evil,自動補(bǔ)全的ido等
  3. emacs本質(zhì)上是一些函數(shù)的組合,通過Alt-x執(zhí)行函數(shù)名,或者通過綁定到快捷鍵上使用,其本質(zhì)是一樣的

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

文件打開和保存

打開文件

emacs的標(biāo)準(zhǔn)做法是使用find-file函數(shù),綁定在C-x C-f鍵上。
在spacemacs中,曾經(jīng)默認(rèn)將C-x C-f綁定到ido-find-file上。而現(xiàn)在,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一樣,也是一個(gè)交互式補(bǔ)全的插件:https://emacs-helm.github.io/helm/
Helm只在emacs 24.4以上版本上才可以用。

打開另一個(gè)文件

標(biāo)準(zhǔn)emacs做法是find-alternate-file函數(shù),綁定到C-x C-v鍵上。spacemacs上,這個(gè)鍵被綁定到ido-find-alternate-file函數(shù)上。

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

將另一個(gè)文件的內(nèi)容插入到當(dāng)前文件

標(biāo)準(zhǔn)emacs的做法是insert-file函數(shù),綁定到C-x i鍵上。spacemacs默認(rèn)綁定到ido-insert-file函數(shù)上。

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

以二進(jìn)制的方式打開文件

emacs默認(rèn)沒有將其綁定到快捷鍵上,這個(gè)功能是hexl-find-file函數(shù)來實(shí)現(xiàn)的。打開后會進(jìn)入hexl-mode.
spacemacs將其綁定到『空格 f h』上。

保存文件

保存文件不涉及交互操作,所以ido和helm都不管,還是用的emacs默認(rèn)的save-buffer函數(shù)。
emacs默認(rèn)將其綁定在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))))

另存為文件

另存為文件又涉及交互操作了,標(biāo)準(zhǔn)emacs使用write-file函數(shù),而spacemacs使用ido-write-file函數(shù)。它們都是綁定到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對此沒有擴(kuò)展,都是使用save-buffers-kill-terminal函數(shù),綁定到C-x C-c鍵上。

(defun save-buffers-kill-terminal (&optional arg)
  (interactive "P")
  (if (frame-parameter nil 'client)
      (server-save-buffers-kill-terminal arg)
    (save-buffers-kill-emacs arg)))
```

## 文件操作小結(jié)

本節(jié)涉及的插件:ido, helm

|功能|函數(shù)|快捷鍵|leader鍵|
|--|--|--|
|打開文件|spacemacs/helm-find-files|C-x C-f|空格 f f|
||find-file|無|無|
||ido-find-file|無|無|
||helm-find-files|無|空格 f F|
|打開另一個(gè)文件|ido-find-alternate-file|C-x C-v|無|
||find-alternate-file|無|無|
|將另一個(gè)文件的內(nèi)容插入到當(dāng)前文件|ido-insert-file|C-x i|無|
||insert-file|無|無|
|保存文件|save-buffer|C-x C-s|空格 f s|
|另存為文件|ido-write-file|C-x C-w|無|
|以二進(jìn)制的方式打開文件|hexl-find-file|無|空格 f h|
|退出emacs|save-buffers-kill-terminal|C-x C-c|無|
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容