Hacker News 中文摘要

RSS订阅

在Emacs中编辑代码 -- Editing Code in Emacs

文章摘要

文章介绍了如何在Emacs中高效编辑代码,强调需要良好的文本编辑设置和形成肌肉记忆的操作习惯。作者建议避免使用鼠标和方向键,保持手指在键盘主行位置,并分享了自己常用的编辑命令和快捷键绑定配置。

文章总结

Emacs高效代码编辑指南

本文面向已熟悉Emacs基础操作的用户,重点介绍如何通过优化配置实现高效代码编辑。文章强调两个核心原则:建立良好的文本编辑环境,并通过持续练习形成肌肉记忆。

【核心编辑理念】 1. 禁用鼠标和方向键:通过remap和disable-mouse包实现,保持手指在home row位置 2. 模态编辑系统:区分命令模式(默认)和插入模式 - 命令模式:单键快速导航(如n=下一行,f=前进一词) - 插入模式:保留原生Emacs键位 - 模式切换:i进入插入,C-j返回命令模式

【高效导航方案】 1. 垂直移动: - 启用相对行号显示(display-line-numbers-type) - 基础命令:n/p移动行,r/t半页滚动 - 高级功能:l/L重定位屏幕光标,./,跳转函数块

  1. 水平移动:

    • 启用subword-mode处理驼峰命名
    • 优先使用语义跳转(j=前进S表达式,h=后退)
    • 行首尾跳转:m(缩进首), e(行尾)
  2. 搜索导航:

    • C-s/C-r标准搜索
    • so调用occur模式实现批量跳转
    • 项目级搜索推荐ag包+silver-surfer

【编辑操作优化】 1. 剪切板操作: - k前缀对应移动命令的kill操作(如kf=删词) - 特殊删除:ki删整词,kl删整行 - yank(粘贴)使用单键y

  1. 文件/窗口管理:
    • v前缀命令:vf打开文件,vs保存
    • vp前缀项目级操作
    • 窗口分割:v2/v3分屏

【实用工具】 1. 矩形编辑:kr删除矩形区域 2. 代码注释:;自动注释选区 3. 版本控制:vv调用magit 4. 推荐扩展包:diff-hl、visible-mark等

【配置建议】 1. 保持键位逻辑与Emacs惯例一致 2. 最小化外部依赖 3. 提供init.el配置参考(GitHub链接)

本文所有技巧同样适用于Vim,选择Emacs仅因作者使用习惯。通过系统化的键位设计和语义化导航,可显著提升代码编辑效率。

(注:原文中关于具体键位映射的详细列表、配置代码片段等细节内容已适当精简,保留核心方法论和最具代表性的操作示例)

评论总结

以下是评论内容的总结:

  1. 支持Vim式导航模式的观点

    • 建议将Emacs导航改为Vim式的模态编辑以提高效率,推荐使用evil-mode、god-mode等插件。
      • "turn Emacs navigation into Vim-like navigation, make the editing modal, and thus make navigation hyper-efficient."
      • "I really like Emacs' flexibility + evil-mode and reactivity."
  2. 传统Emacs导航的拥护者

    • 部分用户坚持使用传统Emacs控制方式,认为无需遵循Vim模式。
      • "I've used Emacs for 20 years and I never learnt to navigate in a file except backwards and forwards search."
      • "You don't need to worry about upholding traditional emacs orthodoxy."
  3. 对模态编辑的质疑

    • 有用户对模态界面持保留态度,认为其适用性有限。
      • "I'm not a fan of modal interfaces. But if it works for you, then knock yourself out."
      • "Seems to be an intro to a kind of but lite weight vi mode. I tend to like the regular mode in Emacs."
  4. 效率与实用性的平衡

    • 讨论鼠标和键盘的效率对比,认为在某些场景下鼠标更高效。
      • "for the remaining 2%, it's provably true that mouse is better - like, selecting an arbitrary block of code."
      • "I use Neovim daily, and there is no denying that 98% of the time, using mouse is less efficient."
  5. 其他导航工具推荐

    • 用户推荐了avy-mode等替代导航工具。
      • "My favorite way to move around in Emacs, and to move text around, is via avy-mode."
      • "Hmm, would be interesting to see a 'race' between this and isearch."
  6. 项目管理和插件讨论

    • 提到Emacs内置的"project"功能和其他插件如magit的使用体验。
      • "C-x p f (find any file in the current 'project', e.g. git repo)."
      • "I am toying with using it, but seems to be a rather big package."
  7. 新工具探索

    • 用户分享了对Doom Emacs和Lem等新工具的尝试和比较。
      • "nowadays I just use some pre-defined configuration like Doom Emacs."
      • "found Lem: https://github.com/lem-project/lem, looks promising."