build a macro that opens a directory and prompts for file name

2 views
Skip to first unread message

Norm

unread,
Jan 23, 2009, 10:13:03 AM1/23/09
to
Hello all,

I have been googleing and reading the docs trying to figure out a
solution to this. I spend the majority of my time working in a single
directory on a CIFS server and I am always doing a C-x C-f \\server\dir
\long_sub_dir\subdir\filename

In the spirit of emacs I'd like to automate it so that when I do C-co
it knows the directory and simply prompts me for the file to be opened
or if I leave it blank and press RET it lists the directory.

Any suggestions on how to accomplish this.

Many thanks,

Norm

Chris McMahan

unread,
Jan 23, 2009, 10:27:30 AM1/23/09
to
Norm <Norma...@gmail.com> writes:

I use bookmarks to take me to the directories themselves.

- Go to the directory that contains the file you want
- Press C-x r m to memorize that location, and give it a name you can
remember

to navigate to that directory, just press C-x r b. You'll be prompted
for the bookmark name, and it will open that directory.

See the help for the function 'command bookmark-set' for details

- Chris

--
(. .)
=ooO=(_)=Ooo=====================================
Chris McMahan | first_init...@one.dot.net
=================================================

Norm

unread,
Jan 23, 2009, 10:38:34 AM1/23/09
to
On Jan 23, 10:27 am, Chris McMahan <cmcma...@one.net> wrote:
>   Chris McMahan | first_initiallastn...@one.dot.net
>   =================================================

Thanks Chris. That is exactly what I need. Wonderful.

Drew Adams

unread,
Jan 24, 2009, 2:16:47 AM1/24/09
to Norm, help-gn...@gnu.org
> > > I have been googleing and reading the docs trying to figure out a
> > > solution to this. I spend the majority of my time working
> > > in a single directory on a CIFS server and I am always doing a
> > > C-x C-f \\server\dir\long_sub_dir\subdir\filename
> >
> > > In the spirit of emacs I'd like to automate it so that
> > > when I do C-co it knows the directory and simply prompts me for
> > > the file to be opened or if I leave it blank and press RET it
> > > lists the directory.
> >
> > I use bookmarks to take me to the directories themselves.
>
> Thanks Chris. That is exactly what I need. Wonderful.

I too use and recommend using bookmarks.

But from your description, all you want is a command that reads a file name in
some predefined directory. You can do that just by binding `default-directory'
to the directory and calling `find-file':

(defun foo ()
"Visit a file in my favorite directory."
(interactive)
(let ((default-directory "/my.server.somewhere:/dir/longsubdir/subdir/"))
(call-interactively 'find-file)))

Pascal J. Bourguignon

unread,
Jan 26, 2009, 5:15:01 AM1/26/09
to
"Drew Adams" <drew....@oracle.com> writes:

>> > > I have been googleing and reading the docs trying to figure out a
>> > > solution to this. I spend the majority of my time working
>> > > in a single directory on a CIFS server and I am always doing a
>> > > C-x C-f \\server\dir\long_sub_dir\subdir\filename
>> >
>> > > In the spirit of emacs I'd like to automate it so that
>> > > when I do C-co it knows the directory and simply prompts me for
>> > > the file to be opened or if I leave it blank and press RET it
>> > > lists the directory.
>> >
>> > I use bookmarks to take me to the directories themselves.
>>
>> Thanks Chris. That is exactly what I need. Wonderful.
>
> I too use and recommend using bookmarks.
>
> But from your description, all you want is a command that reads a file name in
> some predefined directory. You can do that just by binding `default-directory'
> to the directory and calling `find-file':

This is not even needed!

You only have to type C-x C-f, that's all. By default it is in the
default-directory, which is a buffer local variable, so if your
current buffer is in the right directory, you've got nothing more to
do.

So the answer, and you won't say you've not been warned, is not to
kill your old buffers! Just leave them alone in emacs. Thus, with
your old buffer in the right directory, you just type C-x C-f and the
other file name.

If you really need to kill your buffer before opening another file,
you could open a directory buffer:
C-x C-f \\server\dir\long_sub_dir\subdir\ RET

Note that in this directory buffer you may just click (or type RET) on
a file to open it.


Also, you could use some more sophisticated package such as speedbar,
but it would be overkill...

--
__Pascal Bourguignon__

Drew Adams

unread,
Jan 26, 2009, 1:15:50 PM1/26/09
to Pascal J. Bourguignon, help-gn...@gnu.org

All of what Pascal says is true and pertinent.

FYI, you can also just use Emacs (yes, Emacs) command `cd' to change the
`default-directory' at any time. No need to open a Dired buffer.

Stefan Kamphausen

unread,
Jan 27, 2009, 5:04:06 AM1/27/09
to
Hi,

"Drew Adams" <drew....@oracle.com> writes:

> FYI, you can also just use Emacs (yes, Emacs) command `cd' to change the
> `default-directory' at any time. No need to open a Dired buffer.

FWIW, cdargs ([1]please excuse the self-ad) offers a kind of bookmarks for
the shell and ships with some elisp to use the cdargs bookmarks from
Emacs, too.

Cheers,
Stefan

Footnotes:
[1] http://www.skamphausen.de/cgi-bin/ska/CDargs

--
Stefan Kamphausen --- http://www.skamphausen.de
a blessed +42 regexp of confusion (weapon in hand)
You hit. The format string crumbles and turns to dust.

roodw...@ureach.com

unread,
Jan 27, 2009, 5:45:55 AM1/27/09
to
Stefan Kamphausen <ska...@gmx.net> writes:


Bookmarks, which I use, work great for small numbers of
files. But since I generate tons of small files, I keep an
index list of files, with their descriptions, plus the
entire path and file name for that file. So they look
something like this:

/home/rod/vacation.txt How I spent my summer vacation.

I have a macro that copies the entire file name and path and
pastes it into C-x C-f. I've bound it to a function key so
my index kind of works like a hyperlink.

Rod

Kevin Rodgers

unread,
Jan 28, 2009, 3:09:39 AM1/28/09
to help-gn...@gnu.org
roodw...@ureach.com wrote:
> Bookmarks, which I use, work great for small numbers of
> files. But since I generate tons of small files, I keep an
> index list of files, with their descriptions, plus the
> entire path and file name for that file. So they look
> something like this:
>
> /home/rod/vacation.txt How I spent my summer vacation.
>
> I have a macro that copies the entire file name and path and
> pastes it into C-x C-f. I've bound it to a function key so
> my index kind of works like a hyperlink.

If you're visiting your index file, then `M-x find-file-at-point'
will do the trick. Or just `C-x C-f' with this in your ~/.emacs:

(ffap-bindings)

--
Kevin Rodgers
Denver, Colorado, USA

Sebastian Tennant

unread,
Jan 29, 2009, 8:08:22 AM1/29/09
to help-gn...@gnu.org
Quoth Norm <Norma...@gmail.com>:

I don't have time to read this (fairly long) thread but I've been using
a (tiny) macro for years which I think may meet your needs perfectly.

You can get it from here:

http://article.gmane.org/gmane.emacs.sources/3157

In your case, you could define a command 'M-x cifs' in your ~/.emacs
like so:

(require 'commandir)

(commandir cifs ; command name
"CIFS hosted filename: " ; prompt
"\\server\dir=long_sub_dir\subdir" ; directory
"<name-of-file-you-visit-most" ; default
)

The great thing is you can define as many commands as you like:

(commandir cifs2 ...)


HTH,

Sebastian
--
Emacs' AlsaPlayer - Music Without Jolts
Lightweight, full-featured and mindful of your idyllic happiness.
http://home.gna.org/eap

Reply all
Reply to author
Forward
0 new messages