Linux+emacs个性化定制

544 查看

1.最左侧显示行号

在emacs的配置文件(~./.emacs.d/init.el)文件里添加如下内容:

(add-to-list 'load-path "/usr/share/emacs/site-lisp")
(require 'linum)
(global-linum-mode t)

2. emacs无法输入中文(针对gentoo,其他linux系统可参考)

  1. 首先应该先检查字体配置, 然后检查是否开启eamcs的xft的USE标示符.

  2. 安装 font-cursor-misc 或 font-adobe-75dpi ,重启计算机测试(好像大部分是因为缺少这个语言包的问题)

  3. 安装:

    1). [ebuild  N    ] media-fonts/font-adobe-75dpi-1.0.0  USE="X nls" 0 kB
    2). [ebuild  N    ]  x11-apps/bdftopcf-1.0.2  USE="-debug" 0 kB
    3). [ebuild  N    ]  media-fonts/font-alias-1.0.1  USE="-debug" 0 kB
    4). [ebuild  N    ]  media-fonts/font-util-1.1.1  USE="-debug" 0 kB

3. 在emacs文件的标题栏显示该文件的绝对路径: (好处是当你打开多个同名文件时,易于区分)

(setq frame-title-format ("%S" (buffer-file-name "%f" (dired-directory dired-directory "%b"))))

4. 如果不想显示绝对路径,只是想显示该文件的名称

(setq frame-title-format "XX@%b"),其中XX为自己的电脑名,或自己可任意取名,甚至可以不需要。

5. linux下为emacs定制c/c++ 和python代码的自动提示

从 Emacs 24 开始,Emacs 对扩展包进行统一管理,极大程度上简化了扩展包的搜索与安装过程。打开 Emacs,然后执行 M-x list-packages ,如果网速足够快,应该很快就能看到 Emacs 官方提供的扩展包列表。

待软件包列表出现后,执行 C-s M-r ^ +company ,回车即可让光标跳到 company 扩展包信息所在的文本行,继而敲击 i 键(表示要安装该扩展包),再敲击 x 键(表示执行安装过程),稍等片刻,Emacs 会自动从下载 company 扩展包并安装至 $HOME/.emacs.d 目录。

提示:C-s 是开启 Emacs 查找模式, M-r 是将查找模式切换为正则表达式查找模式,而 C-s(ctrl +s) M-r(Alt + r) ^ + company 是用于匹配『位于行首且由多个空格与 company 字符串构成的字符串』的正则表达式。

如果以后要删除 company 扩展,步骤与安装步骤类似,只是将 i 键替换为 d 键(表示删除)。company 安装完毕后,在 Emacs 配置文件中做以下配置:

;; 代码补全
(add-hook 'c-mode-hook 'company-mode)
(add-hook 'c++-mode-hook 'company-mode)。

对于pcl的代码提示功能,只需要安装 company的关于C/C++的头文件即可.(目前是这样的)

在linux下,若emacs启动较慢,可通过hostname查看自己主机的名字,然后打开/etc/hosts文件,将其中localhost改为自己主机名

6.最后贴一下自己的emacs配置文件

;; 关闭启动时的 “开机画面”,将缺省主模式设为 text-mode
(setq inhibit-startup-message t)


;; 语法高亮
(global-font-lock-mode t)

;; 以y/n代表yes/no
(fset 'yes-or-no-p 'y-or-n-p)

;; 显示括号匹配
(show-paren-mode t)
(setq show-paren-style 'parentheses)

(transient-mark-mode t)

;; 在标题栏提示你目前在什么位置
(setq frame-title-format "yxg@%b")
;;(setq frame-title-format '("%S" (buffer-file-name "%f"  (dired-directory dired-directory "%b"))))


;; 默认显示100列就换行
(setq default-fill-column 100)

;; 去掉工具栏
;;(tool-bar-mode 0)

;; 去掉菜单栏
;;(menu-bar-mode 0)

(scroll-bar-mode 0)
(tooltip-mode 0)

;; 显示列号、行号
(setq column-number-mode t)
(setq line-number-mode t)

;; 回车缩进
(global-set-key "\C-m" 'newline-and-indent)
(global-set-key (kbd "C-<return>") 'newline)

;;(global-unset-key (kbd "C-SPC"))  
;;(global-set-key (kbd "M-SPC") 'set-mark-command)


;;;; c mode ;;;;
(defvar xgp-cfsi-left-PAREN-old nil
  "Command used to input \"(\"")
(make-variable-buffer-local 'xgp-cfsi-left-PAREN-old)

(defun xgp-cfsi-modify-alist (alist term new)
  (let ((tl (assq term (symbol-value alist))))
    (if tl
        (setcdr tl new)
      (add-to-list alist (cons term new)))))

(defun xgp-cfsi (style)
  "Deciding whether using CFSI."
  (interactive "Style: ")
  (c-set-style style)
  (setq indent-tabs-mode
        nil
        c-hanging-semi&comma-criteria
        (quote (c-semi&comma-inside-parenlist)))

  (xgp-cfsi-modify-alist 'c-hanging-braces-alist 'class-open nil)
  (xgp-cfsi-modify-alist 'c-hanging-braces-alist 'class-close nil)
  (local-set-key " " 'self-insert-command))

(defun xgp-cfsi-erase-blanks ()
  "Erase all trivial blanks for CFSI."
  (interactive)
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward "[ \t]+$" nil t)
      (replace-match "" nil nil))))

(defun linux-c-mode()
  (define-key c-mode-map [return] 'newline-and-indent)
  (setq imenu-sort-function 'imenu--sort-by-name)
  (interactive)
  (imenu-add-menubar-index)
  (which-function-mode)
  (c-toggle-auto-state)
  (c-toggle-hungry-state)
  (setq indent-tabs-mode nil)
  (xgp-cfsi "linux"))
(add-hook 'c-mode-common-hook 'linux-c-mode)

;; MELPA 扩展包仓库
(when (>= emacs-major-version 24)
  (require 'package)
  (add-to-list
   'package-archives
   '("melpa" . "http://melpa.org/packages/")
   t)
  (package-initialize))

;; 代码补全
(add-hook 'c-mode-hook 'company-mode)
(add-hook 'c++-mode-hook 'company-mode)
;;(eval-after-load 'company
;;  '(add-to-list 'company-backends 'company-irony))

;; 左侧行号显示
(add-to-list 'load-path "/usr/share/emacs/site-lisp")
(require 'linum)
(global-linum-mode t)


(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(column-number-mode t)
 '(show-paren-mode t))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(default ((t (:family "Liberation Mono" :foundry "1ASC" :slant normal :weight normal :height 98 :width normal)))))


;;;;;;;;;;;;;;;;;;;;; for zero ;;;;;;;;;;;;;;;;;;;;;;;;;;
(add-hook 'c-mode-hook
          (lambda ()
           (font-lock-add-keywords nil
            '(("\\(@ *[^@#
]+ *#\\) *[;]*[ 
]+" 1 font-lock-function-name-face t)
          ("^ *\\(@\\) *$" 1 font-lock-function-name-face t)))))

(defun zero-quantum ()
  (interactive)
  (insert "@ ")
  (insert " #\n\n@")
  (backward-char 5))

(define-prefix-command 'ctl-z-map)
(global-set-key (kbd "C-z") 'ctl-z-map)
(global-set-key (kbd "C-z c") 'c-mode)
(global-set-key (kbd "C-z t") 'tex-mode)
(global-set-key (kbd "C-z m") 'markdown-mode)
(global-set-key (kbd "C-z q") 'zero-quantum)