5

I want to abbreviate path in emacs.

The standard behavior of emacs is : C-x C-f then typeset the complete name of a file. E.g.

C-x C-f ~/latex/project/style/test.sty

One can use for quick completion of the path.

As I frequently use two of three directories, I'm wondering if it's possible to gave some « abbreviated path ». E.g.

C-x C-f lstyle/test.sty

expanded automatically in « ~/latex/project/style/test.sty »

Thank you for any help !

Porter
  • 23
  • 6

7 Answers7

3

Something that hasn't been mentioned yet, but is just another opportunity to do it (in vanilla Emacs, no need to use Helm or IDO). You could use bookmarks. Specifically for opening files you would C-x r m - create a bookmark (you will be prompted for a name to give to the bookmark), and then C-x r b would later invoke that bookmark (will prompt you for the name you had previously given to the bookmark).

This will save you some typing, if your primary use of the path is to open files, but if you need to insert it as text, then it won't replace the abbreviation.

One more thing, perhaps, if you didn't know. Once you've typed the path after C-x C-f you can then use:

  1. C-x C-fM-p or M-n to navigate the history of the files you've opened up and down.

  2. C-x C-fM-r to search the history by providing it with regular expression (one downside though, you won't see the match it is going to make while you type the regexp).

2

The best way to achieve that is using abbrevs. They are short words which expand to anything when typed (so the expansion will happen as soon as you hit "/" or SPACE or some punctuation).

Add the following to your ~/.emacs file or to your ~/.emacs.d/init.el file (I prefer the second):

(setq-default abbrev-mode t)
(define-abbrev global-abbrev-table "lstyle" "~/latex/project/style")

Then just give it a shot, type "lstyle/" anywhere and it will expand to "~/latex/project/style".

Malabarba
  • 4,473
  • 2
  • 30
  • 49
2

Use standard option directory-abbrev-alist. From the doc string:

Alist of abbreviations for file directories.
A list of elements of the form (FROM . TO), each meaning to replace
FROM with TO when it appears in a directory name.  This replacement is
done when setting up the default directory of a newly visited file.

FROM is matched against directory names anchored at the first
character, so it should start with a "\\`", or, if directory
names cannot have embedded newlines, with a "^".

FROM and TO should be equivalent names, which refer to the
same directory.  Do not use `~' in the TO strings;
they should be ordinary absolute directory names.

Use this feature when you have directories which you normally refer to
via absolute symbolic links.  Make TO the name of the link, and FROM
the name it is linked to.
Drew
  • 29,895
  • 7
  • 74
  • 104
  • Yes, it does work for me. But what should I do to make it workable with ido? Any ideas? – Andrii Tykhonov Oct 16 '14 at 16:57
  • 1
    @AndreyTykhonov: Dunno; I don't use Ido. Perhaps someone else will be able to help. If not, try mailing list `help-gnu-emacs@gnu.org` for the question, or try `M-x report-emacs-bug` to log an enhancement request. – Drew Oct 16 '14 at 20:10
1

Not exactly what you ask for, but you may use recentf mode together with ido mode to access your recently open files quickly. Put this code in your initialization file:

;; ido-mode
(ido-mode t)

;; recent mode
(recentf-mode 1)

;; define a function to use recentf from ido
(defun recentf-ido-find-file ()
  "Open a recent file using IDO mode"
  (interactive)
  (let ((file (ido-completing-read "Recent file: " recentf-list nil t)))
    (when file
      (find-file file))))

(global-set-key (kbd "C-c f") 'recentf-ido-find-file)

Then you can press C-c f to open your recent files very quickly.

Daniel Martín
  • 7,815
  • 1
  • 29
  • 34
0

There are several solutions for things like that. It is hard to say in advance which one you will like more.

A popular approach is to use ido (see here and here), other is helm.

juanleon
  • 9,220
  • 30
  • 41
0

While looking for the answer on the same question I found the following example:

(define-abbrev-table 'my-abbrev-table
  '(("xy" "/path/to/the/directory")))

(add-hook
 'minibuffer-setup-hook
 (lambda ()
   (abbrev-mode 1)
   (setq local-abbrev-table my-abbrev-table)))

(defadvice minibuffer-complete
  (before my-minibuffer-complete activate)
  (expand-abbrev))

;; If you use partial-completion-mode
(defadvice PC-do-completion
  (before my-PC-do-completion activate)
  (expand-abbrev))
Andrii Tykhonov
  • 538
  • 6
  • 11
0

Using this answer as reference:

(global-set-key (kbd "<f5>") 'lawlist-bookmark)

(defun lawlist-bookmark (choice)
  "Choices for directories and files."
  (interactive "c[d]ropbox | [b]oxsync")
    (cond
     ((eq choice ?d)
      (interactive)
      (counsel-find-file "C:/Users/acer/Dropbox/"))
     ((eq choice ?b)
      (counsel-find-file "C:/Users/acer/Box Sync/"))
     (t (message "Quit"))))

Here counsel-find-file was used instead of find-file to allow interactive browsing in the mini-buffer.