Create aliases in emacs?

本秂侑毒 提交于 2019-12-03 00:18:59

I'm not really clear on what you're asking for. I store my commonly-used files in registers in my .emacs:

(set-register ?c '(file . "c:/data/common.txt"))
(set-register ?f '(file . "c:/data/frequent.txt"))

Then I can jump to a file with jump-to-register (C-x r j):

For example, "C-x r j c" takes me to c:/data/common.txt (loading it if necessary).

Is that what you're looking for?

If you want an alias to a function, you use defalias:

(defalias 'nuke 'delete-trailing-whitespace)

But if you're complaining that Emacs's function names are too long, you should look into partial-completion-mode. With that turned on,

M-x d-t-w [RET]

will run delete-trailing-whitespace.

Here's what you need --

  • Define bookmarks that correspond to contexts you want to revisit. With Bookmark+ this need not be just a file. It can also be a whole Emacs desktop or a Dired buffer with its markings (saved), a set of other bookmarks,...

  • Use Icicles together with Bookmark+. Whenever you use C-x C-f etc. to visit a file, you can also visit a file or directory bookmark (complete against the bookmark name). Just hit C-x m when you are in the minibuffer for file-name completion.

I what you want is open a given file with just a command you can define a keyboard macro. This macro open the file ~/.bashrc

 C-x( # start defining the macro
 C-x C-f ~/.bashrc   
 C-x )  # end definition

Then you can name this macro

M-x name-last-kbd-macro
visitbashrc   #give it a name

then you can call it by name M-x visitbashrc

You can save the definition to a file, .emacs for instance by visiting this file and inserting the definition in it, with

M-x insert-kbd-macro

I have this setup below. So for example, if I want to find a file on my desktop (D:/Desktop), I type F3 j F3, and no matter what path I started with, the minibuffer now says "D:\Desktop\" and I'm ready to type any filename on the Desktop. I set my shortcut to j, k, l, i b/c those are close to where my left hand is, but with this setup, any shortcut up to 4 letters will work. So you can add an entry for your "awsome" file, say "awe", and you can type F3 awe F3 enter to visit your file. Don't know whether this is what you are looking for; but this save me a lots of typing ;)

(global-set-key [f3] 'ffap)

;comcplete shortcut in minibuffer
(define-key minibuffer-local-completion-map (kbd "<f3>")
                      'complete-minibuffer-path) 

(defun complete-minibuffer-path ()
  "Extension to the complete word facility of the minibuffer by
replacing matching strings to a specific path"
  (interactive)
  (setq found t)
  (cond
     ; just add new entries if needed; shortcut up to 4 letters will work
     ((looking-back "j" 5 nil) (setq directory "D:/Desktop/"))
     ((looking-back "k" 5 nil) (setq directory "D:/Documents/"))
     ((looking-back "l" 5 nil) (setq directory home-dir))
     ((looking-back "i" 5 nil) (setq directory "D:/Programs/"))
     (t (setq found nil)))
  (cond (found (beginning-of-line)
                (kill-line)
                (insert directory))
         (t (minibuffer-complete)))) 

This blog entry defines almost exactly what I wanted, basically let's me hit $ in the minibuffer and then presents me with a list of my bookmarks that I can jump to. I might tweak it so that if I specify a file bookmark it takes me to the file instead of the directory, but that's a nit, especially considering that the file the bookmark is of tends to be at the top of the list anyway.

I Moved this into the original question, but i'm leaving it here because people responded to it already

That's way better than what i've got, but what i feel like probably exists is something that would replace, for example:

(defun nuke ()
   "alias delete-trailing-whitespace"
   (interactive)
   (delete-trailing-whitespace))

and feel less hacky, and like it was actually doing what it was supposed to be doing, instead of doing something really heavy for what feels like something that should be really light.

Does that make sense?

Your assign to registers might actually be better than what i'm looking for, though.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!