1. 程式人生 > >Emacs的簡單配置與常用快捷鍵

Emacs的簡單配置與常用快捷鍵

u盤 lis 代碼補全 搜索 line 開頭 配色方案 fin key

在我的一篇日誌中提到過Emacs,這裏把我自己使用時的一些簡單配置列出來以供大家參考。

  1. 界面配置
      1.1 安裝插件
      先創建目錄~/.emacs.d/plugins/,然後進入這個目錄,這裏用來擺放我們要下載和使用的插件(大家可以按照自己喜好設定,不一定非要這樣設置)。
        1.1.1 下載window-numbering插件,用於窗口快速切換;

    git clone https://github.com/nschum/window-numbering.el.git

        1.1.2 下載emacs-neotree插件,用於顯示左側樹形目錄;

    git clone https://github.com/jaypei/emacs-neotree.git

        1.1.3 下載tabbar插件,用於設置標簽欄;

    git clone https://github.com/dholm/tabbar.git

        1.1.4 安裝emacs-goodies-el插件,用於配色主題。

    sudo apt install emacs-goodies-el

      下載安裝好上述插件後,編輯~/.emacs文件(如果沒有就創建)。文件內容如下:

    ;; ---------------------界面設置---------------------
    ;; 關閉啟動畫面
    (setq inhibit-startup-message 1)
    ;; 隱藏工具欄
    (tool-bar-mode 0)
    ;; 顯示行號
    (global-linum-mode 1)
    ;; 設置行號格式
    (setq linum-format "%d ")
    ;; 顯示列數
    (setq column-number-mode 1)
    ;; 當前行高亮
    (global-hl-line-mode 1)
    ;; 設置窗口的寬度與高度
    (set-frame-width (selected-frame) 120)
    (set-frame-height (selected-frame) 40)
    ;; 設置窗口切換快捷方式(通過Alt+1,2,3,...來快速切換窗口)
    (add-to-list ‘load-path "~/.emacs.d/plugins/window-numbering.el")
    (require ‘window-numbering)
    (window-numbering-mode 1)
    ;; 設置左側樹形目錄
    (add-to-list ‘load-path "~/.emacs.d/plugins/emacs-neotree")
    (require ‘neotree)
    (neotree-toggle)  ;; 讓啟動時就顯示左側目錄
    (global-set-key [f8] ‘neotree-toggle)  ;; 設置快捷鍵f8來打開隱藏目錄
    (setq projectile-switch-project-action ‘neotree-projectile-action)
    ;; 垂直分屏,上面窗口分配的大小
    (windmove-right)              ;; 切換到右邊的窗口
    ;; 註意,window的尺寸比frame小,我這裏window實際只有34,frame有40
    ;; 所以在劃分大小時要計算好比例
    (split-window-vertically 25)
    ;; 設置標簽欄
    (add-to-list ‘load-path "~/.emacs.d/plugins/tabbar")
    (require ‘tabbar)
    (tabbar-mode 1)
    (global-set-key [(meta k)] ‘tabbar-forward)  ;; 快捷鍵M-k前翻
    (global-set-key [(meta j)] ‘tabbar-backward) ;; 快捷鍵M-j後翻
    ; close default tabs,and move all files into one group
    (setq tabbar-buffer-list-function
    (lambda ()
        (remove-if
          (lambda(buffer)
             (find (aref (buffer-name buffer) 0) " *"))
          (buffer-list))))
    (setq tabbar-buffer-groups-function
      (lambda()(list "All")))
    (set-face-attribute ‘tabbar-button nil)
    ;; 設置標簽欄顏色
    (set-face-attribute ‘tabbar-default nil
                    :background "gray23"
                    :foreground "white")
    (set-face-attribute ‘tabbar-selected nil
                    :inherit ‘tabbar-default
                    :background "gray"
            :foreground "black"
                    :box ‘(:line-width 2 :color "white") )
    (set-face-attribute ‘tabbar-unselected nil
                    :inherit ‘tabbar-default
                    :box ‘(:line-width 1 :color "gray"))
    ;; 設置標簽之間的間距
    (custom-set-variables ‘(tabbar-separator (quote (0.4))))
    ;; 設置代碼配色主題
    (require ‘color-theme)
    (color-theme-initialize)
    ;;(color-theme-bharadwaj-slate)  ;; 配色方案
    ;;(color-theme-charcoal-black)
    (color-theme-goldenrod)
    ;;(color-theme-classic)

      其中的一些尺寸、顏色大家看自己的喜好,自行設置。保存、退出,再打開Emacs,效果如下:
    技術分享圖片

  2. 功能配置
      2.1 下載插件auto-complete,用於代碼補全。下載目錄和上面的一樣。
    git clone https://github.com/auto-complete/auto-complete.git

      2.2 然後還是編輯~/.emacs文件,增加以下內容:

    ;; ---------------------功能設置---------------------
    ;; 關閉自動備份
    (setq make-backup-files nil)
    ;; 把urdf文件做為xml文件模式處理
    (setq auto-mode-alist (cons ‘("\\.urdf$" . nxml-mode) auto-mode-alist))
    ;; 把xacro格式的文件做為xml文件模式處理
    (setq auto-mode-alist (cons ‘("\\.xacro$" . nxml-mode) auto-mode-alist))
    ;; 把launch格式的文件做為xml文件模式處理
    (setq auto-mode-alist (cons ‘("\\.launch$" . nxml-mode) auto-mode-alist))
    ;; 自動縮進
    (global-set-key (kbd "RET") ‘newline-and-indent)
    ;; ---------------------代碼補全---------------------
    ;; 配置auto-complete
    (add-to-list ‘load-path "~/.emacs.d/plugins/auto-complete/")
    (require ‘auto-complete-config)
    (add-to-list ‘ac-dictionary-directories
    "~/.emacs.d/plugins/auto-complete/dict/")
    (ac-config-default)

     
      上面那三個文件格式處理代碼大家可以不用加,那是我用在ROS仿真建模用的。(以上代碼被分成好幾塊擺放,是因為放在一塊兒的話,博客顯示會有問題)
      
      以上只是對Emacs很簡單的一些配置,我目前也只是用到這些(其他功能在用其他工具),所以就沒有進行更深入的訂制了。網絡上相關文章比較多,大家根據自己的實際需要進行取舍。此外,大家可以把配置好的~/.emacs.d目錄與~/.emacs文件復制保存在自己的備份中(移動硬盤、U盤、雲盤……),以後重裝系統或在別的電腦上安裝時直接復制粘貼過去就可以使用,不用再重新下載編輯。

  


  
  下面列出一些常用的快捷鍵,其中C-開頭的表示是按Ctrl鍵,M-開頭的表示是按Alt鍵。

  1. 基本命令
    a. 關閉Emacs:C-x C-c
    b. 強制取消當前操作:C-g
  2. 文件操作命令
    a. 打開/新建文件:C-x C-f
    b. 保存當前緩沖區:C-x C-s
    c. 當前緩沖區另存為:C-x C-w
    d. 關閉當前緩沖區並打開新文件:C-x C-v
    e. 關閉當前緩沖區:C-x k
  3. 窗口命令
    a. 切換窗口:C-x o
    b. 快捷窗口切換:M-1, 2, 3, ...(註意:需要下載配置window-numbering.el插件,前面已有介紹。)
    d. 進入shell模式:M-x shell(這裏是按了Alt+x鍵後,再鍵盤輸入shell命令)
  4. 編輯命令
    a. 復制標記區內容:M-w
    b. 粘貼:C-y
    c. 剪切:C-w
    d. 刪除一行:C-k
    e. 撤銷:C-x u
    f. 全選:C-x h
  5. 搜索命令
    a. 向下搜索:C-s
    b. 向上搜索:C-r

Emacs的簡單配置與常用快捷鍵