工欲善其事,必先利其器之—MAC下安装与配置emacs

安装应用

mac os下安装emacs有如下方式
* 通过homebrew安装

brew install emacs --with-cocoa
  • 通过MacPorts安装
sudo port install emacs-app
  • 通过Emacs for OSX下载安装包
    emacs 26.1.1下载地址
    工欲善其事,必先利其器之—MAC下安装与配置emacs_第1张图片

注:这里建议于第三种方式安装,根据以住的经验兼容性问题会少很多!

环境配置

命令环境

在.bash_profile文件添加如下代码,方便使用emas打开文件

# for emcas
# export EDITOR="emacsclient -n "
# alias e="emacsclient -n
# /Applications/Emacs.app/Contents/MacOS/bin
EMACS_HOME=/Applications/Emacs.app/Contents/MacOS
# 在终端下打开文件,替换命令行默认的emacs
alias emacs="${EMACS_HOME}/Emacs -nw"
# GUI方式打开文件
alias e="${EMACS_HOME}/bin/emacsclient -n"

如下在命令行下没有替换系统emacs的使用,用–version可以看到是系统自动的

luogw@luogw-MacBook-Pro channel_template$ emacs --version
GNU Emacs 22.1.1
Copyright (C) 2007 Free Software Foundation, Inc.
GNU Emacs comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of Emacs
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.

替换系统emacs的使用后(在.bash_profile中添加alias emacs=”${EMACS_HOME}/Emacs -nw” 并source .bash_profile)

luogw@luogw-MacBook-Pro ~$ emacs --version
GNU Emacs 26.1
Copyright (C) 2018 Free Software Foundation, Inc.
GNU Emacs comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of GNU Emacs
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.

.emacs

;;==============================================================
;; base configure for common using
;;=============================================================
;; start server , 这样在终端下主可以通过emacsclient -n 直接于GUI emacs打开文件
(require 'server)
(unless (server-running-p "server")
(server-start))

;; Added by Package.el.  This must come before configurations of
;; installed packages.  Don't delete this line.  If you don't want it,
;; just comment it out by adding a semicolon to the start of the line.
;; You may delete these explanatory comments.
(package-initialize)

(require 'package)
(add-to-list 'package-archives
             '("melpa-stable" . "https://stable.melpa.org/packages/"))
(package-initialize)

;; package server
;; (setq package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
;; ("melpa-stable" . "https://stable.melpa.org/packages/")))

;; (setq package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
;; ("melpa-stable" . "https://stable.melpa.org/packages/")
;; ("marmalade" . "http://marmalade-repo.org/packages/")
;; ("melpa" . "http://melpa.milkbox.net/packages/")))

;; background color , 苹果绿,爱护眼睛
(when window-system
  (custom-set-faces
   '(default ((t (:background "#B4EEB4"))))))


;; copy content from elsewhere to emacs or emacs copied to other places
(setq x-select-enable-clipboard t)

;; show line number
(require 'linum)
(global-linum-mode t)

;; kill current buffer
(global-set-key (kbd "C-x k") (lambda () (interactive) (kill-buffer (current-buffer))))


;; open .emacs
(global-set-key (kbd "M-h") (lambda () (interactive) (find-file "~/.emacs")))

(blink-cursor-mode nil)

;; hide memubar,false
;;(menu-bar-mode -1)

;; hide toolbar,false
(tool-bar-mode -1)

;; show file path in window's tile
(setq frame-title-format
'("%S" (buffer-file-name "%f"
(dired-directory dired-directory "%b"))))

;; hide scroll-bar
(scroll-bar-mode nil)
(setq scroll-preserve-screen-position t)

(when window-system
  (global-hl-line-mode t)
  (set-face-background 'hl-line "#CAFF70"))

;; 成对显示括号,但不来回弹跳
(show-paren-mode t)
(setq show-paren-style 'parentheses)

;; tab width
(standard-display-ascii ?\t "#---")
(setq default-tab-width 4)
(setq-default indent-tabs-mode nil)
(defun my-c-mode-hook ()
(c-set-style "stroustrup")
(c-set-offset 'innamespace 0))
(add-hook 'c-mode-hook 'my-c-mode-hook)
(add-hook 'c++-mode-hook 'my-c-mode-hook)

;; ibuffer
(require 'ibuffer)
(global-set-key "\C-x\C-b" 'ibuffer)

;; yes /no -> y/n
(fset 'yes-or-no-p 'y-or-n-p)

;; close autosave
(setq backup-inhibited t)
;; stop creating those #autosave# files
(setq auto-save-default nil)

;; ido
(require 'ido)
(ido-mode t)

;; 字体设置,使用等宽字体
(dolist (charset '(kana han symbol cjk-misc bopomofo))
  (set-fontset-font (frame-parameter nil 'font)
                    charset
                    (font-spec :family "STHeiti" :size 12)))
(set-default-font "Bitstream Vera Sans Mono 12")

;;======================================================
;; 自定义功能或函数
;;======================================================
;; 换行格式转化 dos to unix
(defun dos2unix ()
  "Automate M-% C-q C-m RET RET"
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (while (search-forward (string ?\C-m)  nil t)
      (replace-match "" nil t))))

;; using pgup,pgDn scroll
(defun window-half-height ()
(max 1 (/ (1- (window-height (selected-window))) 2)))
(defun scroll-up-half ()
(interactive)
(scroll-up (window-half-height)))
(defun scroll-down-half ()
(interactive)
(scroll-down (window-half-height)))
(global-set-key [next] 'scroll-up-half)
(global-set-key [prior] 'scroll-down-half)

;; occur
(defun isearch-occur ()
"*Invoke `occur' from within isearch."
(interactive)
(let ((case-fold-search isearch-case-fold-search))
(occur (if isearch-regexp isearch-string (regexp-quote isearch-string)))))

(define-key isearch-mode-map (kbd "C-o") 'isearch-occur)

插件安装

使用包管理器安装插件,推荐如下插件
* ace-jump-mode : 方便光标跳转的插件
* smex: 一个”M-x”快捷键的增强工具,它能够使得在Emacs中调用各种命令更为方便,能更智能地对命令进行补全,还能根据使用者调用命令的频率来猜测用户可能会执行的命令
* ag : the silver searcher搜索的前端

插件安装步骤

1.列出插件
使用package-list-pacgages函数显示列插件列表

M-X : package-list-packages

2.选择插件
工欲善其事,必先利其器之—MAC下安装与配置emacs_第2张图片
3.安装插件

插件配置

在.emacs文件中添加如下配置

;;======================================================
;; plugin settings , 插件配置
;;======================================================

;;==================
;; smex是一个"M-x"快捷键的增强工具,它能够使得在Emacs中调用各种命令更为方便,能更智能地对命令进行补全,还能根据使用者调用命令的频率来猜测用户可能会执行的命令
;;==================
(smex-initialize)
(global-set-key (kbd "M-x") 'smex)
;;==================
;; ace-jump, 方便光标跳转的插件
;;=================
(define-key global-map (kbd "C-c SPC") 'ace-jump-mode)
(setq ace-jump-mode-scope 'window)

;;==================
;; ag , the silver searcher搜索的前端
;;==================
(setq ag-highlight-search t)
;; (setq ag-reuse-buffers t)
;; Its value is ("--smart-case" "--nogroup" "--column" "--")
(setq ag-arguments (list "--ignore" "TAGS" "--smart-case" "--nogroup" "--column" "--"))
(global-set-key (kbd "C-c f") 'ag-project-at-point)

你可能感兴趣的:(开发环境,工具效率,工欲善其事,必先利其器)