如何將Emacs打造成現代的C/C++ IDE(一 把基本環境搭起來)

爲什麼選擇EMacs? 網絡上已經有很多文章說明這個問題, 對我而言Just for fun.

 

接下來我會演示如何在一個Centos 7.6 上安裝最新版本的GNU Emacs. 經過以下步驟, 你就可以得到一個可以在SSH終端上使用起來還不錯的C/C++編輯器。

目錄

0. 環境準備

1. 下載和編譯emacs. 

2. 下載spacemacs配置

3. 設置c-c++-layer

4. 設置終端色彩

5. 效果演示


 

0. 環境準備

sudo yum install -y gcc gcc-c++ ncurses ncurses-devel gnutls gnutls-utils

 

1. 下載和編譯emacs. 

去官網拿到下載地址

$ mkdir downloads && cd downloads
$ wget http://mirrors.nju.edu.cn/gnu/emacs/emacs-26.1.tar.gz
$ tar xf emacs-26.1.tar.gz
$ cd emacs-26.1

# 這裏創建一個目錄用於安裝自己編譯的一些程序
$ mkdir ~/local
$ ./configure --prefix=/home/sujinpei/local --with-gnutls=no
$ make && make install

$ export PATH=$HOME/local/bin:$PATH

# 查看一下emacs的版本是否正確
$ emacs --version

# 將路徑添加到PATH環境變量
echo -en "export PATH=$HOME/local/bin:$PATH\n" >> ~/.bashrc

2. 下載spacemacs配置

# 這個步驟需要git支持, 如果沒安裝先運行: sudo yum install -y git
git clone https://github.com/syl20bnr/spacemacs ~/.emacs.d

# 如果~/.emacs.d已經存在, 請先刪除或者備份

# 切換到develop分支

cd ~/.emacs.d/

git checkout develop

 啓動emacs根據提示完成spacemacs的安裝。在Installing packages的時候可能會很慢甚至報錯。C-g(ctrl+g)取消安裝, 然後C-x C-c退出。

打開~/.spacemacs文件, 將user-init部分修改如下

(defun dotspacemacs/user-init ()
  "Initialization for user code:
This function is called immediately after `dotspacemacs/init', before layer
configuration.
It is mostly for variables that should be set before packages are loaded.
If you are unsure, try setting them in `dotspacemacs/user-config' first."
  (setq configuration-layer-elpa-archives
      '(("melpa-cn" . "http://elpa.emacs-china.org/melpa/")
        ("org-cn"   . "http://elpa.emacs-china.org/org/")
        ("gnu-cn"   . "http://elpa.emacs-china.org/gnu/")))
  )

然後重啓emacs, emacs會繼續完成spacemacs的安裝, 這時候安裝的速度應該會提升很多。如果安裝依然失敗, 就需要用emacs --debug-init命令來啓動, 排查一下問題, 一般是網絡問題。

 

3. 設置c-c++-layer

打開.spacemacs找到dotspacemacs-configuration-layers

dotspacemacs-configuration-layers
    '(
      ;; ----------------------------------------------------------------
      ;; Example of useful layers you may want to use right away.
      ;; Uncomment some layer names and press `SPC f e R' (Vim style) or
      ;; `M-m f e R' (Emacs style) to install them.
      ;; ----------------------------------------------------------------[<0;81;16M]
      c-c++
      helm
      auto-completion
      better-defaults
      emacs-lisp
      ;; git
      ;; markdown
      multiple-cursors
      neotree
      ;; org
      ;; (shell :variables
      ;;        shell-default-height 30
      ;;        shell-default-position 'bottom)
      ;; spell-checking
      ;; syntax-checking
      ;; version-control
      )

重啓emacs, 等待相關的包安裝完成。C-x C-f創建一個C/C++的文件編輯一下, 語法高亮, 一些自動補全功能, 但是顏色可能有一些問題, 如何解決呢? 請看下面的步驟。

 

4. 設置終端色彩

export TERM=xterm-256color

 

5. 效果演示

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章