参考 effective emacs中文版 来提升emacs
C-\ 关闭输入法
C-h k 再输入一个快捷键 查看这个快捷键绑定了什么函数
C-h w 查看函数绑定了哪些快捷键 通常可以先C-h f查看当前快捷键绑定了哪个函数,再看看这个函数绑定了哪些快捷键,这样可以选出替代的较好的快捷键。
C-h b 列举所有绑定的快捷键
C-h f 查看函数帮助
C-h m 查看本modes下的帮助
C-x m 发邮件
C-x 1 清除所有分屏
C-x 3 增加一个横屏
C-x o 分屏间切换
C-x b 跳转到指定 buffer, 若输入的文件不存在,则开启草稿本,很实用
C-x C-b 列举所有打开的buffer
C-x s 保存文件
C-x k 杀灭buffer
很干脆的杀buffer,不想每次回答个 yes or no 之类的
(global-set-key "\C-xk" #'(lambda () (interactive) (kill-buffer (current-buffer))))
C-w 来删一个词 backspace 实在太远了. 剪切就用 C-x C-w 替代
(global-set-key "\C-w" 'backward-kill-word)
(global-set-key "\C-x\C-w" 'kill-region)
C-M-n 转到大括号结束
C-M-p 转到大括号开始
C-M-a 转到上一个函数
C-M-e 转到下一个函数
C-u space 本页面回到跳转处 用于跳转后本页面返回
C-x space 跨页面回到跳转处 用于跳转后跨页面返回
C-a 移动到行首
C-e 移动到行尾
C-p 上一行
C-n 下一行
C-l 光标行调整到屏幕中间 再按光标行成为最高行 再按成为最低行
(global-set-key "\C-xg" 'goto-line)
(global-set-key "\C- " 'set-mark-command)
(global-set-key "\M- " 'set-mark-command)
M-x 执行外部命令
C-x C-m 也被我绑定成了 M-x
C-x C-e 执行lisp 函数
C-u C-x C-e 执行lisp函数,结果回写到当前buffer
C-u 10 C-n 向下移动10行
这2个不光用于增量搜索,主要用于快速到达文本的某处,C-n C-p 移动光标实在太慢了
C-r 向前增量搜索
C-s 向后增量搜索
复制当前行
(defun copy-line (arg)
"Copy lines (as many as prefix argument) in the kill ring"
(interactive "p")
(kill-ring-save (point)
(line-end-position))
(message "%d line%s copied" arg (if (= 1 arg) "" "s")))
(global-set-key "\C-o" 'copy-line)
打开文件并跳转到指定行
(defun goto-file-line(filename line)
(find-file filename)
(goto-line line)
)
录制宏
C-x ( 开始录制
.....此处为宏的操作
C-x) 结束录制
执行宏
1、C-x e 执行1次
2、C-u 10 C-x e 执行10次
(defun remove-dos-eol ()
"Do not show ^M in files containing mixed UNIX and DOS line endings."
(interactive)
(setq buffer-display-table (make-display-table))
(aset buffer-display-table ?\^M []))
(add-hook 'c++-mode-hook 'remove-dos-eol)
(add-hook 'typescript-mode-hook 'remove-dos-eol)
Emacs下面的redo就是用undo的undo实现的.
比如删了一个字符, 然后用C-/ undo了, 但你后面又想删掉这个字符(也就是redo), 这时你可以先使用一个非编辑命令(如C-f), 再用C-/就是redo了.
M-x eshell 打开
(eshell 2) 开启多个eshell
(eshell 3) 开启多个eshell
eshell
cd some-folder/ 可以把 cd 去掉 直接输入 文件夹名称
cd = 罗列最近访问过的文件路径
cd -2 切换到cd = 列出的序号为2的目录
别名示例
alias rm 'rm -f $*'
可以打开别名文件自己编辑 别名文件 ~/.emacs.d/eshell/alias
打开后自己编辑,保存后重启eshell即可
我的别名文件参考
alias d dired $1
alias kubectl kubectl -s http://192.168.255.130:8080 $*
alias cdm cd /home/wangjunsheng/memo
C-x r b 跳转到指定的书签
C-x r l 列举所有书签
C-x r m 添加书签
Installing_Emacs_on_GNU/Linux
sudo add-apt-repository ppa:ubuntu-elisp/ppa
sudo apt-get update
sudo apt-get install emacs-snapshot emacs-snapshot-el
这是在电脑上装了个新emacs, 老的并没有消失
我升级到 27版本后 遇到了 find-file 无法 tramp 的问题,
就是/sudo::localhost/etc/hosts 之类的路径在新版的emacs默认不支持了
需要在 .emacs 中显式开启
(require 'tramp)
archmage 可以解析chm文件,并伪装成HTTP服务器。于是我们可以用浏览器查看chm。
ubuntu 安装
sudo apt-get install archmage
作为HTTP服务器使用
archmage -p 7777 APUE.chm
也可以把 chm 转换成 text文件
需要安装 elinks
sudo apt-get install elinks
装好后执行 text 转换
archmage --convert=text APUE.chm
同目录下会生成 APUE.txt 文件
emacs 下查看 chm , 需要装 ChmView。
ChmView是为archmage定制的视图,方便在emacs上显示。
安装
切换到你的 emacs 常用插件目录,获取 chm-view.el 文件
wget https://www.emacswiki.org/emacs/download/chm-view.el
安装 w3m
sudo apt-get install w3m w3m-img
手动下载 https://github.com/wanderlust/ 的 apel 和 flim 放到emacs能加载的目录
然后下载 https://github.com/emacs-w3m/emacs-w3m
autoconf
./configure
make
把 emacs-w3m 也放到emacs能加载的目录去
最后修改.emacs配置, 路径不一样相同,自己改成合适的
(add-to-list 'load-path "~/emacs/emacs-w3m/shimbun")
(add-to-list 'load-path "~/emacs/emacs-w3m")
(require 'w3m-load)
(setq browse-url-browser-function 'w3m-browse-url)
(defalias 'w3 'w3m-browse-url)
最后用 chm-view-file APUE.chm
来访问
eww www.qq.com
F1 查看man
(global-set-key[(f1)](lambda()(interactive)(man(current-word))))
M x 模式名字 切换当前buffer的模式 比如 c++-mode
之前 vs va用惯了 Alt+g 代码跳转,所以无论是go 还是 js 还是 c 我都把 跳转的快捷键设成这个
(define-key go-mode-map [(meta ?g)] 'godef-jump)
(define-key emacs-lisp-mode-map [(meta ?g)] 'find-function)
(setq indent-tabs-mode nil)
(setq c-default-style "linux")
(setq c-basic-offset 4)
(setq default-tab-width 4)
ag https://github.com/ggreer/the_silver_searcher.git
emacs ag 前端 https://github.com/Wilfred/ag.el.git
git grep xxx (这个和 emacs 无关)
ag 安装 用法
(global-set-key "\C-\M-f" 'ag-files)
(semantic-mode) 开启
(global-set-key [(meta ?g)] 'semantic-ia-fast-jump)
C - i 代码格式化
M-/ 代码补全
通常源码会分布在很多的文件夹下,要把这些目录加到 补全的搜索路径中 就可以跳转了
(defun vpath_for_gateway () (interactive)
(semantic-add-system-include "~/work/lora/simulator/base" 'c-mode)
(semantic-add-system-include "~/work/lora/simulator/3rd" 'c-mode)
)
(vpath_for_gateway) C-x C-e 执行下
M-x gdb
gdb -i=mi exename // 默认的 程序名字自己换
也许还要
M-x gdb-many-windows // 进入多窗口页面
b file.c:6
b function_name
b line
d 1 删除断点
r 可以带参数
n 下一行
s 进入行数
p 显示变量值
p /x 16进制显示
p *array@6
c 继续
quit 退出
info threads 查看所有的线程
thread id 切换到id指定的线程
t id t thread 的简写
backtrace 查看调用堆栈
frame N:切换到栈编号为N的上下文中
emacs 下调试, 直接用鼠标点击调用堆栈即可
(setq send-mail-function (quote smtpmail-send-it))
(setq smtpmail-smtp-server "192.168.0.10")
(setq smtpmail-smtp-service 25)
(setq user-full-name "wangjunsheng")
(setq user-mail-address "[email protected]")
根目录 .authinfo 文件中配置账号信息
~ $ cat .authinfo
machine 192.168.0.10 login [email protected] port 25 password xxx
C-x m 编写邮件
C-c C-c 发送邮件
指定文件后缀用 哪种mode 打开
(add-to-list 'auto-mode-alist (cons "\\.message\\'" 'message-mode))
(add-to-list 'auto-mode-alist (cons "\\.h\\'" 'c++-mode))
更新到27.0.50版后 还要加下面两个配置
(setq smtp-server "192.168.0.10")
(setq smtp-fqdn "zlg.cn")
如果是新版的emacs, 还得在 .emacs 中加上
(require 'tramp)
本地root权限
(find-file "/sudo:localhost:")
第一次要 C-f C-x 下运行
(find-file "/ssh:[email protected]:")
(find-file "/ssh:[email protected]|sudo:192.168.0.2:")
注意: 中间不能有空格
terminal下运行ssh 后启动远程机器的emacs
我通过下面的设置让远程机器的emacs在terminal下全屏
;;设置窗口位置为屏库左上角(0,0)
(set-frame-position (selected-frame) 0 0)
;;设置宽和高,我的十寸小本是110,33,大家可以调整这个参数来适应自己屏幕大小
(set-frame-width (selected-frame) 150)
(set-frame-height (selected-frame) 45)
;;稍微改了下背景色
(set-background-color "white")
(set-foreground-color "black")
编辑yml 文件 好歹有个语法高亮嘛
下载, 在你安装emacs插件的目录下载, 我的插件都放在 ~/emacs/下 , 所以我在 ~/emacs 目录下运行 git clone
git clone https://github.com/yoshiki/yaml-mode.git
(add-to-list 'load-path "~/emacs/yaml-mode")
(require 'yaml-mode)
(add-to-list 'auto-mode-alist '("\\.yml\\'" . yaml-mode))
(setq yaml-indent-offset 4)
apt-get install ghostscript
apt-get install xpdf
可以试试
C-c C-t 进入 PDF 的文本模式
文本方式查看PDF,相当爽啊
M-x doc-view-mode 切换回 PDF 默认查看模式
go-mode 安装
在另一篇文章里有写
(add-to-list 'load-path "/home/wangjunsheng/go/emacs/go-mode")
(require 'go-mode)
(define-key go-mode-map [(meta ?g)] 'godef-jump)
(custom-set-variables '(gofmt-command "goimports"))
(define-key go-mode-map "\C-c\C-f" 'gofmt)
我把 gofmt 改成了 goimports 它不仅会格式化 还会自动控制 import
goimports安装
git clone https://github.com/golang/tools.git $GOPATH/src/golang.org/x/tools
嫌慢,可以去码云上找个替代链接
go install golang.org/x/tools/cmd/goimports
goimports 程序会被安装到 $GOPATH/bin 目录下
package包管理介绍
安装
M-x package-install RET tide RET
如果 tide 找不到 或 不存在, 请添加
(add-to-list'package-archives '("melpa" . "http://melpa.milkbox.net/packages/") t)
然后在输入 M-x package-refresh-contents,刷新package信息
安装过程中 请耐心等待 挺久的。。。。 有次我不小心点了下 ctrl+G ,结果安装了个半成品。坑死了。
安装过程中 emacs 处于假死状态。
装好后 如下配置 .emacs 文件
(require 'tide)
(defun setup-tide-mode ()
(interactive)
(tide-setup)
(flycheck-mode +1)
(setq flycheck-check-syntax-automatically '(save mode-enabled))
(eldoc-mode +1)
(tide-hl-identifier-mode +1)
;; company is an optional dependency. You have to
;; install it separately via package-install
;; `M-x package-install [ret] company`
(company-mode +1))
;; aligns annotation to the right hand side
(setq company-tooltip-align-annotations t)
;; formats the buffer before saving
(add-hook 'before-save-hook 'tide-format-before-save)
(add-hook 'typescript-mode-hook #'setup-tide-mode)
(setq typescript-indent-level 2)
;; 我喜欢用 alt+g 跳转
(define-key tide-mode-map [(meta ?g)] 'tide-jump-to-definition)
;; 用习惯了 C-x C-SPC 跳转回去
(define-key tide-mode-map (kbd "C-x C-SPC") 'tide-jump-back)
在.emacs配置文件中加入如下配置:
(require 'package)
(package-initialize)
(add-to-list'package-archives '("melpa" . "http://melpa.milkbox.net/packages/") t)
然后在输入 M-x package-refresh-contents,刷新package信息
然后安装
https://github.com/xuchunyang/youdao-dictionary.el
M-x package-install RET youdao-dictionary RET
再配下 .emacs 加点快捷键之类的
;; Enable Cache
(setq url-automatic-caching t)
;; Example Key binding
(global-set-key (kbd "C-c i") 'youdao-dictionary-search-from-input)
(global-set-key (kbd "C-c u") 'youdao-dictionary-search-at-point)
windows HOME 路径设置
打开注册表,添加注册表项:
HKEY_LOCAL_MACHINE\SOFTWARE\GNU\Emacs
在这个路径下面新建值:HOME, 值是 C:\
启动windwos程序
(browse-url "calc.exe") ;; 计算器
(browse-url "c:/Program Files/TTC/TotalCMD.exe") ;; totalcmd
改键
可以不用改了,利用手掌边缘来按机械键盘的ctrl 蛮好用的
[windows平台现在用一个叫remapkey.exe的程序可以轻松实现了]
Caps Lock 改为 Ctrl 保护小拇指,提升效率, Caps Lock 不要了
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout
在右侧右键新建, 选择"二进制值", 名称为: "Scancode Map"
~/.bashrc 中加入下面这行,重启 (没有严格测试,但这样搞了后,我电脑的可以激活了)
export LC_CTYPE=zh_CN.UTF-8
/etc/environment 中加入
LC_CTYPE="zh_CN.UTF-8"
然后用root账户登录,之后再重启电脑
字体,在 菜单 options的 set default font 中选择自己喜欢的。
然后 M-x describe-font
拷贝 字体描述。写到 .emacs 文件中, 我在centos下的 字体配置如下
(set-default-font "-bitstream-Bitstream Charter-normal-normal-normal-*-16-*-*-*-*-0-iso10646-1")
附录,我的emacs配置,方便自己拷贝 有些地方不完善
;(desktop-load-default)
;(desktop-read)
(desktop-save-mode 1)
(server-start)
(menu-bar-mode 1)
(if (< emacs-major-version 23)
(progn
(set-terminal-coding-system 'chinese-iso-8bit)
(set-keyboard-coding-system 'chinese-iso-8bit)
(setq locale-coding-system 'chinese-iso-8bit)
(prefer-coding-system 'chinese-iso-8bit))
(progn
(set-terminal-coding-system 'utf-8) ;
(set-keyboard-coding-system 'utf-8)
(setq locale-coding-system 'utf-8)
(prefer-coding-system 'utf-8)
))
(setq Info-scroll-prefer-subnodes nil) ;info scrolling
(setq inhibit-startup-message t) ;no starup message
(setq-default indent-tabs-mode nil)
(setq display-line-number-format "%4d| ")
(setq uniquify-buffer-name-style 'reverse)
(ido-mode 1)
(icomplete-mode 1)
(global-font-lock-mode t) ;better color
;(global-whitespace-mode t) ;better space color
(transient-mark-mode 0)
(setq scroll-preserve-screen-position nil)
(show-paren-mode t)
(tool-bar-mode 0)
(global-hl-line-mode 1)
(setq-default truncate-lines t)
(blink-cursor-mode 0)
(fringe-mode (cons 3 3))
(setq indent-tabs-mode nil)
(setq c-default-style "linux")
(setq c-basic-offset 4)
(setq tab-stop-list())
(setq default-tab-width 4)
(setq ring-bell-function 'ignore)
(scroll-bar-mode -1)
;(setq inverse-video t)
;(setq mode-line-inverse-video t)
(setq default-major-mode 'text-mode)
(setq display-time-day-and-date t)
(display-time)
(global-set-key [(meta f7)] 'emacs-vs-talker)
(global-set-key [(meta f8)] 'emacs-vs-build)
(defalias 'llm 'longlines-mode)
(global-set-key [(control ?x) (meta ? )] 'pop-global-mark)
(global-set-key [(meta ?h)] #'(lambda () (interactive) (find-file "~/.emacs")))
(global-set-key "\C-xg" 'goto-line)
(global-set-key "\C-w" 'backward-kill-word)
(global-set-key "\C-x\C-w" 'kill-region)
(global-set-key "\C- " 'set-mark-command)
(global-set-key "\M- " 'set-mark-command)
;(global-set-key " " (quote set-mark-command))
(global-set-key "\C-xk" #'(lambda () (interactive) (kill-buffer (current-buffer))))
(global-set-key "\C-x\C-k" #'(lambda () (interactive) (kill-buffer (current-buffer))))
(global-set-key [(control ?=)] 'call-last-kbd-macro)
(defalias 'yes-or-no-p 'y-or-n-p)
(global-set-key [(control down)] #'(lambda () (interactive) (scroll-up 1)))
(global-set-key [(control up)] #'(lambda () (interactive) (scroll-down 1)))
(global-set-key "\C-z" 'ignore)
(global-set-key "\C-x\C-z" 'iconify-or-deiconify-frame)
(global-set-key "\C-xp" #'(lambda () (interactive) (other-window -1)))
(global-set-key [insert] 'ignore)
(global-set-key [(control ?x) insert] 'overwrite-mode)
(global-set-key "\C-cp" 'anything)
(global-set-key "\C-co" 'occur)
(global-unset-key "\C-xf")
(global-set-key "\C-x\C-b" 'ibuffer)
(global-set-key [f6] 'semantic-ia-complete-symbol-menu)
(global-set-key [f7] 'compile)
(global-set-key [(control f5)] 'view-mode)
(global-set-key [(control f6)] 'hexl-mode)
(global-set-key "\C-c\C-m" 'execute-extended-command)
(global-set-key "\C-x\C-m" 'execute-extended-command)
(defalias 'op 'find-file)
(defalias 'oo 'find-file-other-window)
(defalias 'qrr 'query-replace)
(defalias 'rstr 'replace-string)
(defalias 'ptm 'pop-to-mark-command)
(defalias 'emacs 'open)
(global-set-key "\C-c\M-b" 'pop-to-mark-command)
(global-set-key (kbd "C-c C-b") 'browse-kill-ring)
(global-set-key "\C-xx" 'hippie-expand)
(global-set-key "\247" (quote hippie-expand))
(add-to-list 'auto-mode-alist (cons "\\.css\\'" 'css-mode))
(add-to-list 'auto-mode-alist (cons "\\.h\\'" 'c++-mode))
(defalias 'switch-to-next-buffer 'bury-buffer)
(defun switch-to-previous-buffer ()
(interactive)
(switch-to-buffer (nth (- (length (buffer-list)) 1) (buffer-list))))
(global-set-key [C-tab] 'switch-to-previous-buffer)
(global-set-key [C-S-iso-lefttab] 'switch-to-next-buffer)
;;Îļþ¸ñʽת»»
(defun dos-unix () (interactive)
(goto-char (point-min))
(while (search-forward "\r" nil t) (replace-match "")))
(defun unix-dos () (interactive)
(goto-char (point-min))
(while (search-forward "\n" nil t) (replace-match "\r\n")))
(defun maximize-frame ()
"×î´ó»¯×Ô¼ºµÄ´°Ìå"
(interactive) (w32-send-sys-command #xf030))
(defvar jr-a-temporary-point)
(defun remember-a-temporary-point ()
"Joyer's temporary mark-command"
(interactive)
(make-local-variable 'jr-a-temporary-point)
(message "a new Joyer's mark is setted.")
(setq jr-a-temporary-point (copy-marker (point-marker))))
(defun back-to-a-temporary-point ()
"Go back to Joyer's temporary mark-command"
(interactive)
(make-local-variable 'jr-a-temporary-point)
(when jr-a-temporary-point
(let ((old jr-a-temporary-point))
(message "pop from Joyer's mark.")
(setq jr-a-temporary-point (copy-marker (point-marker)))
(goto-char old))))
(global-set-key (quote [3 67108921]) (quote remember-a-temporary-point));"C-cC-9"
(global-set-key (quote [3 67108920]) (quote back-to-a-temporary-point)) ;"C-cC-8"
;(global-set-key [(f10)] 'jr-run-current-file)
;(global-set-key [(f9)] 'jr-compile-current-file)
(global-set-key [(f10)] 'smart-run)
(global-set-key [(f9)] 'smart-compile)
(global-set-key [(f7)] 'compile)
(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.
'(blink-cursor-mode nil)
'(default-input-method "chinese-py")
'(display-time-mode t)
'(js-indent-level 2)
'(js-paren-indent-offset 2)
'(js-square-indent-offset 2)
'(js-switch-indent-offset 2)
'(js2-basic-offset 2)
'(kill-do-not-save-duplicates t)
'(show-paren-mode t)
'(tool-bar-mode nil)
'(transient-mark-mode nil))
(global-set-key[(f1)](lambda()(interactive)(man(current-word))))
(global-set-key [(meta ?g)] 'semantic-ia-fast-jump)
(global-set-key [(control ?')] 'semantic-ia-complete-symbol)
(semantic-mode)
(put 'upcase-region 'disabled nil)
(define-key emacs-lisp-mode-map [(meta ?g)] 'find-function)
(global-set-key "\C-xj" #'(lambda() (interactive) (find-file "~/memo/shortcut")))
(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 (:inherit nil :stipple nil :background "#d3d7cf" :foreground "#000000" :inverse-video nil :box nil :strike-through nil :overline nil :underline nil :slant normal :weight normal :height 128 :width normal :foundry "DAMA" :family "Ubuntu Mono"))))
'(highlight ((t (:background "darkseagreen2")))))
(set-language-environment 'UTF-8)
(set-locale-environment "UTF-8")