[英]How do I make an emacs shortcut to switch between related files?
例如,我打开了文件 model/user.py,我想要一个打开 controller/user.py 的快捷方式。 或者我想切换到 test/model/testUser.py (人为的例子)
我想做一个 emacs 快捷方式,给定当前打开的文件,以各种方式打开相关文件。
如果“相关文件”遵循某种模式,我认为编写一些 elisp 函数来完成任务是微不足道的。 假设您有一个 model 并且需要打开他关联的 controller,您需要执行以下操作:
(defun my-open-related-controller ()
(interactive)
(let* ((name (buffer-file-name))) ;gets the filename of the current buffer
;; Of course, this is only an example. The point here is that you need
;; to "discover" the name of the related file based on the current one.
(setf name (replace-regexp-in-string "model" "controller" name))
;; Now you will open the file(if it isn't open already) and switch to it
(find-file name)))
然后您可以将 function 绑定到 F5:
(define-key name-of-the-mode-map [f5] 'my-open-related-controller)
如果要全局创建此绑定,请使用:
(global-set-key [f5] 'my-open-related-controller)
当然,这只是一个粗略的示例(因为您没有提供很多具体细节),但应该足以让您入门。 希望能帮助到你!
如果您不喜欢自己编写此代码并希望自定义现有库,您可能希望查看toggle.el 。 它旨在满足您的要求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.