您现在的位置是:首页 >其他 >Emacs初探网站首页其他

Emacs初探

weixin_110 2025-03-23 12:01:02
简介Emacs初探

原生 Emacs,也被称为 Vanilla Emacs

专业 Emacs 入门
Emacs使用手册.pdf

ControlCCtrl / Control
MetaMAlt / Option
ShiftSShift / Shift
SupersWin / Command
emacs
emacs -nw
退出 Emacs,C-x C-c

访问菜单,按`F10`键, 退出菜单,按`ESC`键三次
SPC 表示空格键

Alt-x 先按一下 Esc键松开,再按 x 键
Alt-x help-with-tutorial-spec-language 来更改语言
将 Emacs转为后台程序 C-z
将 Emacs转为前台程序 fg

中断正在输入的命令, C-g
打开教程:C-H 后,按 t

翻页:
 C-v (查看下一屏文字)PageDn
 Alt-v (查看上一屏文字) PageUp
光标:previous(前)、next(后)、back(退)、forward(进)
 头部 C-a ,  尾部 C-e
 向左移 C-b , 词 Alt-b
 向右移 C-f , 词 Alt-f
 上一行 C-p,  下一行 C-n
 文档开头 M-<
 文档末尾 M->
 文档中间 Alt-r
 让光标所在行处于屏幕顶部(不是移动行,而是视图滚动):输入"C-u 0 C-l"
 向右移动 8 个字符 C-u 8 C-f
 向下滚动 8 行    C-u 8 C-v
 Alt-m 来移动到行开头 Alt-a 至句首, Alt-e 至句尾


复制:Alt+w (复制) 或者 Ctrl+w (剪切)
粘贴: Ctrl+y
重复操作: C-u 数字 动作  =》如 C-u 12 C-n 表示向下 12 行
						   C-u 8 *,插入 ********
 
删除字符 C-d  删除单词 Alt-d
删除到行尾 C-k
删除到句尾 Alt-k
删除一段,先光标定位到开始处
	按住 Ctrl+shift+@
	再光标定位到结尾处,这样待删除的文字就被选中了,
	再C-w 删除

召回最后删除的文字 C-y
撤销 C-/ 或者 C-_

向前循环查找:C-s   向后循环查找:C-r  查找的过程用 Alt-c 快速切换是否大小写敏感   查找回车符用C-j 代替
正则查找:C-M-s

文件:
 	C-x C-f         打开文件。
    C-x C-s         保存文件。
    C-x s           保存多个缓冲区。
 	C-x C-r         只读打开文件
 	C-x C-q  已打开的切换为只读模式
    C-x C-b         列出缓冲区。
    C-x b           切换缓冲区。
    C-x C-c         离开 Emacs。
    C-x 1           关掉其它所有窗格,只保留一个。
    C-x u           撤销。
    
查阅帮助文档:
	查看C-f的文档解释:C-h k C-f
分屏:切换C-x o
	上下分屏:C-x 2
	左右分屏:C-x 3
	关闭分屏: C-x 0
	宽度调整: C-u 30 C-x { 或 } 或 ^

编辑Emacs的配置文件(通常是.emacs或init.el
;; 设置复制快捷键为 Ctrl + c
(global-set-key (kbd "C-c") 'kill-ring-save)

;; 设置粘贴快捷键为 Ctrl + v
(global-set-key (kbd "C-v") 'yank)
Emacs 有一个 evil 插件可以在 Emacs 中直接使用 Vim 的快捷键
预装了很多插件:Doom Emacs 和 Spacemacs
专门适配 macOS 的 Aquamacs, 改键软件 Karabiner-Elements
Emacs Lisp 源码文件的后缀名是 .el
注释:分号(;)开头
自动依次寻找配置文件
~/.emacs
~/.emacs.el
~/.emacs.d/init.el
~/.config/emacs/init.el

大牛的配置
    Spacemacs
    Doom Emacs
    Centaur Emacs
    Steve Purcell
    redguardetoo (陈斌 一年成为Emacs高手作者)

改国内的软件源:
(setq package-archives '(("gnu"   . "http://mirrors.tuna.tsinghua.edu.cn/elpa/gnu/")
                         ("melpa" . "http://mirrors.tuna.tsinghua.edu.cn/elpa/melpa/")
                         ("org" . "http://mirrors.tuna.tsinghua.edu.cn/elpa/org/")))

中文相关:在文本编辑中,中文处理起来比其它基于字母的语言麻烦很多,因此需要很多配置。
    Emacs-rime ,中文输入
    cns 分词。提供基于结巴分词的词语移动、删除功能
    ace-pinyin 。用两个中文的拼音首字母来进行可见区域内的光标跳跃
    unicad 。自动识别文件的编码,默认情况下, Emacs 打开 GBK 等格式时文件可能会乱

笔记管理:
    denote 。管理卢曼笔记
    xeft 。管理一般意义上的笔记,其往往没有复用的价值,只提供后续查阅。比如软件使用方法的记录等, xeft 提供了非常高效的全文检索功能
    org-journal 。管理日记,我可以用它来方便地对日记进行加密,以及基于日期范围的检索
检索:
	consult+orderless+vertico 三件套。 consult 已经足够覆盖大部分使用情形了,比如搜索文件夹、搜索 buffer ,对当前文件按行检索等等
	rg/color-rg 。基于 ripgrep 的多文件搜索、替换,在批量处理文本时非常好用

设置f2打开配置文件
(defun open-my-init-file()
(interactive)
(find-file "~/.emacs.d/init.el"))
(global-set-key (kbd "") 'open-my-init-file)
(setq inhibit-splash-screen t)
https://blog.csdn.net/killerpro/article/details/52455700


插件: https://zhuanlan.zhihu.com/p/441612281   https://blog.csdn.net/u010164190/article/details/129732734
ivy(包括counsel、swiper)是一个命令自动补全的插件,把swiper和swiper-backward绑定的C-s和C-r,替换掉emacs自带的搜索。当按下C-s时,就会自动调用ivy还搜索,更方便快捷地在文件中多处匹配之前跳转
	(add-to-list 'load-path "~/.emacs.d/swiper")
    (require 'ivy)
    (require 'counsel)
    (require 'swiper)
    (ivy-mode)
    (setq ivy-use-virtual-buffers t)
    (setq enable-recursive-minibuffers t)
    (global-set-key "C-s" 'swiper)
    (global-set-key "C-r" 'swiper-backward)
    (global-set-key (kbd "M-x") 'counsel-M-x)
    (global-set-key (kbd "C-x C-f") 'counsel-find-file)

projectile是以项目为单位进行编辑插件。默认会把git、mercurial hg的根目录当作项目根目录,也可以手动创建一个.projectile的空文件作为项目根路径的标记。这里(https://docs.projectile.mx/projectile/index/html).
	F12 f xxx来打开当前项目下的文件,就不用指定具体路径
	F12 s g就会自动调用grep在整个项目下搜索,并且会自动过滤一些临时文件目录
	可以指定用ag(F12 s s)或者ripgrep(F12 s r)来搜索,速度比linux自带的grep快得多  #从这里https://melpa.org下载ag安装包
        (add-to-list 'load-path "~/.emacs.d/projectile")
        (add-to-list 'load-path "~/.emacs.d/ag")
        (add-to-list 'load-path "~/.emacs.d/dash")
        (add-to-list 'load-path "~/.emacs.d/s")
        (require 'projectile)
        (require 'ag)
        (projectile-mode +1)

auto-complete是一个自动补全框架,支持多种语言的语法关键字,其中就包括verilog。
auto-complete也会自动收集RTL或者Testbench中的变量,当再次输入变量时,只需要敲前两个字母就会弹出候选菜单,通过快捷键或者方向箭头来选取。极大的提高了写verilog的速度。
    (add-to-list 'load-path "~/.emacs.d/auto-complete")
    (add-to-list 'load-path "~/.emacs.d/popup-el")
    (add-to-list 'load-path "~/.emacs.d/fuzzy-el")
    (require 'auto-complete-config)
    (ac-config-default)

也可以再结合ctags、etag等工具来生成verilog的调用关系,方便在模块和实例化、函数调用和声明之前跳转。
具体方法可以参考Synopsys用户大会上华为的一篇文章《Using Verdi to Generate vi and emacs Tagging Databases》。

ace-window: 窗口超过 3 个后就很难使用 C-x o 进行切换了。ace-window 对 C-x o 重新绑定,使用时可以为每个 window 编个号,用编号进行跳转。
(use-package ace-window
  :ensure t
  :bind (("C-x o" . 'ace-window)))

~/.emacs.d/init.el

;;; init.el --- Load the full configuration -*- lexical-binding: t -*-
;;; Commentary:

;; This file bootstraps the configuration, which is divided into
;; a number of other files.

;;; Code:

(eval-when-compile
  (require 'use-package))

(let ((normal-gc-cons-threshold (* 20 1024 1024))
      (init-gc-cons-threshold (* 128 1024 1024)))
  (setq gc-cons-threshold init-gc-cons-threshold)
  (add-hook 'emacs-startup-hook
            (lambda () (setq gc-cons-threshold normal-gc-cons-threshold))))

;; 在新的窗口中打开文件
(global-set-key (kbd "C-c o") 'find-file-other-window)
 
;; 在新的框架中打开文件(需要先定义一个函数)
(defun open-file-in-new-frame (filename)
  "Open FILENAME in a new frame."
  (interactive "FFile name: ")
  (make-frame)
  (switch-to-buffer (find-file-noselect filename)))
(global-set-key (kbd "C-c f") 'open-file-in-new-frame)

;; 自定义两个函数
;; Faster move cursor
(defun next-ten-lines()
  "Move cursor to next 10 lines."
  (interactive)
  (next-line 10))

(defun previous-ten-lines()
  "Move cursor to previous 10 lines."
  (interactive)
  (previous-line 10))
;; 绑定到快捷键
(global-set-key (kbd "M-n") 'next-ten-lines)            ; 光标向下移动 10 行
(global-set-key (kbd "M-p") 'previous-ten-lines)        ; 光标向上移动 10 行



(require 'package)
(setq package-archives '(("gnu"    . "https://mirrors.tuna.tsinghua.edu.cn/elpa/gnu/")
                         ("nongnu" . "https://mirrors.tuna.tsinghua.edu.cn/elpa/nongnu/")
                         ("melpa"  . "https://mirrors.tuna.tsinghua.edu.cn/elpa/melpa/")))
(package-initialize)



(setq inhibit-startup-message t)             ; 关闭启动 Emacs 时的欢迎界面
(setq make-backup-files nil)                 ; 关闭文件自动备份
(setq confirm-kill-emacs #'y-or-n-p)         ; 在关闭 Emacs 前询问是否确认关闭,防止误触
(setq display-line-numbers-type #'relative)   ; (可选)显示相对行号
(setq-default c-default-style "awk")
(add-hook 'prog-mode-hook #'show-paren-mode) ; 编程模式下,光标在括号上时高亮另一个括号
(add-hook 'prog-mode-hook #'hs-minor-mode)   ; 编程模式下,可以折叠代码块

(electric-pair-mode t)                       ; 自动补全括号
(column-number-mode t)                       ; 在 Mode line 上显示列号
(delete-selection-mode t)                    ; 选中文本后输入文本会替换文本(更符合我们习惯了的其它编辑器的逻辑)
(global-auto-revert-mode t)                  ; 当另一程序修改了文件时,让 Emacs 及时刷新 Buffer
(global-set-key (kbd "M-w") #'kill-region)              ; 交换 M-w 和 C-w,M-w 为剪切
(global-set-key (kbd "M-m") #'move-beginning-of-line)   ; 交换 C-a 和 M-m,M-m 为到真正的行首
(global-set-key (kbd "C-w") #'kill-ring-save)           ; 交换 M-w 和 C-w,C-w 为复制
(global-set-key (kbd "C-a") #'back-to-indentation)      ; 交换 C-a 和 M-m,C-a 为到缩进后的行首
(global-set-key (kbd "C-c '") 'comment-or-uncomment-region) ; 为选中的代码加注释/去注释
(global-display-line-numbers-mode 1)         ; 在 Window 显示行号

(add-to-list 'default-frame-alist '(width . 90))  ; (可选)设定启动图形界面时的初始 Frame 宽度(字符数)
(add-to-list 'default-frame-alist '(height . 55)) ; (可选)设定启动图形界面时的初始 Frame 高度(字符数)

(savehist-mode 1)                            ; (可选)打开 Buffer 历史记录保存
(when (display-graphic-p) (toggle-scroll-bar -1) (tool-bar-mode -1)) ; 图形界面时关闭滚动条


(use-package ivy
  :ensure t                          ; 确认安装,如果没有安装过 ivy 就自动安装    
  :init                              ; 在加载插件前执行命令
  (ivy-mode 1)                       ; 启动 ivy-mode
  :custom                            ; 自定义一些变量,相当于赋值语句 (setq xxx yyy)
  (ivy-use-virtual-buffers t)        ; 一些官网提供的固定配置
  (ivy-count-format "(%d/%d) ") 
  :bind                              ; 以下为绑定快捷键
  ;("C-s" . 'swiper-isearch)          ; 绑定快捷键 C-s 为 swiper-search,替换原本的搜索功能
  ;("C-r" . 'swiper-backward)        
  ("M-x" . 'counsel-M-x)             ; 使用 counsel 替换命令输入,给予更多提示
  ;("C-x C-f" . 'counsel-find-file)   ; 使用 counsel 做文件打开操作,给予更多提示
  ("M-y" . 'counsel-yank-pop)        ; 使用 counsel 做历史剪贴板粘贴,可以展示历史
  ("C-x b" . 'ivy-switch-buffer)     ; 使用 ivy 做 buffer 切换,给予更多提示
  ("C-c v" . 'ivy-push-view)         ; 记录当前 buffer 的信息
  ("C-c s" . 'ivy-switch-view)       ; 切换到记录过的 buffer 位置
  ("C-c V" . 'ivy-pop-view)          ; 移除 buffer 记录
  ("C-x C-SPC" . 'counsel-mark-ring) ; 使用 counsel 记录 mark 的位置
  ("<f1> f" . 'counsel-describe-function)
  ("<f1> v" . 'counsel-describe-variable)
  ("C-c C-r" . 'ivy-resume)
  ("<f6>" . 'ivy-resume)
  ("<f1> o" . 'counsel-describe-symbol)
  ("<f1> l" . 'counsel-find-library)
  ("<f2> i" . 'counsel-info-lookup-symbol)
  ("<f2> u" . 'counsel-unicode-char)
  ("C-c g" . 'counsel-git)
  ("C-c j" . 'counsel-git-grep)
  ("C-c k" . 'counsel-ag)
  ("C-x l" . 'counsel-locate)
  ("C-S-o" . 'counsel-rhythmbox)
  ("C-r" . 'counsel-minibuffer-history)
  )

(use-package flycheck
  :ensure t
  :hook                        ; 为模式设置 hook
  (prog-mode . flycheck-mode))

(use-package projectile
  :ensure t
  :init
  (projectile-mode 1)
  :bind 
  (:map projectile-mode-map
    ("s-p" . projectile-command-map)
    ("C-c p" . projectile-command-map)
    ("<f12>" . projectile-command-map)
  ))

(add-hook 'text-mode-hook #'flyspell-mode)
(add-hook 'prog-mode-hook #'hs-minor-mode)

(use-package ace-window
  :ensure t
  :bind (("C-x o" . 'ace-window)))

(use-package which-key
  :ensure t
  :init (which-key-mode))

(use-package avy
  :ensure t
  :bind
  (
  ("M-j" . #'avy-goto-char-timer)
  ("C-:" . #'avy-goto-char)
  ("C-'" . #'avy-goto-char-)
  ("M-g f" . #'avy-goto-line)
  ("M-g w" . #'avy-goto-word-1)
  ("M-g e" . #'avy-goto-word-0)
  ("C-c C-j". #'avy-resume)
  ))
  
(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.
 '(package-selected-packages '(flycheck ivy)))
(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.
 )


(provide 'init)

;;; init.el ends here

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。