将Emacs添加到右键快捷菜单中

将Emacs添加到右键快捷菜单中

通过右键菜单打开文件

长期用Emacs写代码,但是每次都是从命令行启动,有个麻烦事,针对单个文件,且该文件处在目录较深时,还要在命令中一层层敲进去,程序猿的时间都是很宝贵的,下面就来看看如何将Emacs添加到右键菜单中:

从“run”中运行“regedit”启动注册表; 在打开的注册表中找到“HKEY_CLASSES_ROOT\*”,这里需要注意,其中的“*”可不是指在“HKEY_CLASSES_ROOT”下,而该项的下面有个子项叫“*”。在该子项下新建一个名为“shell”的项,如果没有,直接新建,如果有,可以不用新建;

此时,在“HKEY_CLASSES_ROOT\*\shell”上右键,选中“新建”->“项”,键入“Emacs 编辑(&M)”。然后在“HKEY_CLASSES_ROOT\*\shell\”下新建一个项,命名为"通过 Emacs 打开",再在“HKEY_CLASSES_ROOT\*\shell\通过emacs打开”下新建一个项,命名为“command”;

然后,选中“HKEY_CLASSES_ROOT\*\shell\通过emacs打开\command”项,在右边的栏目中,双击“名称”为“默认”的项,双击,在打开的对话框中的“数值数据”框内添加你自己Emacs的安装目录下的“bin”目录,并在后面空一格添加“%1”,确定即可,如我的是“D:Program Files (x86)\emacs-28.2\bin\runemacs.exe "%1"”。

此时,你在一个源代码文件上右键点击时,就可以看到有了“Emacs编辑”可选项!

Open Emacs here

给右键菜单添加“Open Emacs here”这个选项

打开注册表,定位到HKEY_CLASSES_ROOT\Directory\Background\shell(如果Background下面没有shell目录,就自己新建一个)。

在shell下右键-新建项,其名称为“Open Emacs here”,此为右键菜单显示名称。

在shell下右键-新建-字符串,命名为Icon,双击编辑,其值为“D:Program Files (x86)\emacs-28.2\bin\runemacs.exe”,在右键菜单中会自动显示该软件图标。

在"Open Emacs here"下右键-新建-项,命名为command,其值为"D:Program Files (x86)\emacs-28.2\bin\runemacs.exe"。

此时,在任何一个目录空白处右键,即可出现Open Emacs here选项,单击即可启动emacs并在Emacs中自动进入当前目录。考虑到PowerShell右键选项需要按住Shift才会在右键菜单中显示出来,想把Open Emacs here也搞成这样子。只需这样操作:

在Open Emacs here下右键-> 新建字符串,命名为“Extended”(与Icon字段同级)即可。

通过注册表文件实现添加 Emacs 到右键菜单

使用此注册表文件之前需要根据 Emacs 的实际安装目录将注册表文件中的路径替换一下

假如注册表文件中的某路径不存在,那么就先进入这个不存在的路径的上一级存在的路径,逐级递进,这样注册表在执行的时候就会将不存在的路径一级一级的创建出来,如果注册表文件中的有效路径后连着跟了两个或两个以上的不存在的路径名,则添加注册表会失败

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
  Windows Registry Editor Version 5.00

  [HKEY_CLASSES_ROOT\*\shell]
  [HKEY_CLASSES_ROOT\*\shell\Edit with Emacs]
  "icon"="D:\\Program Files (x86)\\emacs-28.2\\bin\\runemacs.exe"
  [HKEY_CLASSES_ROOT\*\shell\Edit with Emacs\command]
  @="D:\\Program Files (x86)\\emacs-28.2\\bin\\runemacs.exe \"%1\""

  [HKEY_CLASSES_ROOT\Directory\Background\shell]
  [HKEY_CLASSES_ROOT\Directory\Background\shell\Open Emacs here]
  "icon"="D:\\Program Files (x86)\\emacs-28.2\\bin\\runemacs.exe"
  [HKEY_CLASSES_ROOT\Directory\Background\shell\Open Emacs here\command]
  @="D:\\Program Files (x86)\\emacs-28.2\\bin\\runemacs.exe"

将 Emacs 添加进右键菜单的注册表文件下载

点击下载

下载该注册表文件后双击运行即可

通过bat脚本实现添加 Emacs 到右键菜单

如果运行该bat脚本失败的话可以尝试以管理员身份运行

运行该脚本后会提示输入 emacs 安装路径,将自己电脑上的emacs的实际安装路径输入即可,如 D:Programs\emacs-28.2\bin\runemacs.exe

脚本中的 REM 起注释作用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  @echo off

  REM 等待用户输入参数
  set /p runemacs_path=请输入 runemacs.exe 路径:%1
  REM /p 是 echo 命令的一个选项,用于暂停输出并等待用户按下任意键继续

  REM 使用输入的参数进行操作
  REM echo runemacs.exe 路径:%runemacs_path%  

  REM 对斜杠进行转义
  set runemacs_path=%runemacs_path:\=\\%

  echo 转义后的 runemacs.exe 路径: "%runemacs_path%"

  echo Windows Registry Editor Version 5.00 >> auto_set_runemacs.reg

  REM echo. 表示输出一个空行
  echo.  >> auto_set_runemacs.reg

  echo [HKEY_CLASSES_ROOT\*\shell] >> auto_set_runemacs.reg
  echo [HKEY_CLASSES_ROOT\*\shell\Edit with Emacs] >> auto_set_runemacs.reg
  echo "icon"="%runemacs_path%" >> auto_set_runemacs.reg
  echo [HKEY_CLASSES_ROOT\*\shell\Edit with Emacs\command] >> auto_set_runemacs.reg
  echo @="%runemacs_path% \"%%1\"" >> auto_set_runemacs.reg

  echo.  >> auto_set_runemacs.reg

  echo [HKEY_CLASSES_ROOT\Directory\Background\shell] >> auto_set_runemacs.reg
  echo [HKEY_CLASSES_ROOT\Directory\Background\shell\Open Emacs here] >> auto_set_runemacs.reg
  echo "icon"="%runemacs_path%" >> auto_set_runemacs.reg
  echo [HKEY_CLASSES_ROOT\Directory\Background\shell\Open Emacs here\command] >> auto_set_runemacs.reg
  echo @="%runemacs_path%" >> auto_set_runemacs.reg

  REM /eait的作用是等start命令执行完后再运行批处理脚本
  start /wait auto_set_runemacs.reg
  del /f auto_set_runemacs.reg

  REM 其他操作...

将 Emacs 添加进右键菜单的注册表文件下载

点击下载

下载该注册表文件后双击运行即可

右键菜单启动emacs默认以管理员身份运行

emacs添加到右键菜单打开文件,普通用户是没有问题的,但是通过在任务栏修改emacs的属性以管理员身份运行的时候再通过右键打开emacs就打不开了,这时候可以将开始菜单中的emacs快捷方式的属性也更改为以管理员身份运行,或者直接在emacs的安装目录将emacs的属性更改

Licensed under CC BY-NC-SA 4.0