help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: cd to the user's home dir


From: Bob Proulx
Subject: Re: cd to the user's home dir
Date: Sun, 18 May 2014 17:31:47 -0600
User-agent: Mutt/1.5.23 (2014-03-12)

Andrey Tykhonov wrote:
> Emanuel Berg writes:
> > Andrey Tykhonov writes:
> >> I would like to be able to `cd' into the user's home
> >> directory in the same way as I can `cd' in the shell:
> >>
> >> cd ~admin
> >>
> >> I try in Emacs:
> >>
> >> M-x cd RET ~admin RET
> >>
> >> but it doesn't work. Emacs seems doesn't provide such
> >> functionality. Am I wrong?
> >
> > `cd' changes the "default directory" of the current
> > buffer - e.g., so when you `find-file' in that buffer,
> > the prompt will by default be pointed at that
> > directory, as the most likely place you would want to
> > look.
> >
> > I don't really get what you want

I don't think I understand either.  Because the above works fine for
me.  If you use the emacs interactive cd function it will prompt in
the minibuffer with this:

  Change default directory: ~/

If I type in ~someuser/somepath that is expanded appropriately and
everything works fine.  When you say it does not work for you what is
it about it that does not work?

> Well, I have many projects which paths are long and it is very often
> required to `cd' to one of them. Very often and time consuming. For a shell
> I resolved such an issue by means of creating system user (I'm on Linux)
> and now I can just (with auto completion and quite nice representation in
> the PS1):
> 
> cd ~username
> 
> where username is a system user's home directory which is the project
> root. Well, `username' in my case is just a project's name.

That is fine.  But instead of creating a full user account in the
system it would be more typical to create variables and cd there.  Or
to create aliases.  In the shell, there is even variable name completion:

  cd $somelocation

> Such thing is quite good and handy for me and I would like the same but in
> Emacs, thus I want to be able to change "default directory" of the current
> buffer to the user's home directory.
> 
> I just wrote these simple functions which does allow to do described thing:
> 
> (defun system-users ()
>   (split-string
>    (shell-command-to-string "grep -o '^[^:]*' /etc/passwd | tr '\n' ' '") " 
> "))
> 
> (defun cu (user)
>   "cd to the USER's home directory."
>   (interactive
>    (list
>     (completing-read "User: " (system-users))))
>   (setq default-directory
>       (replace-regexp-in-string "\n" "" (shell-command-to-string

Look into using the (expand-file-name) function.  Use it like this:

  (expand-file-name "~/emacs")

It returns the expanded file name.  That would be a lot more portable
and easier to use than your brute force grep of the /etc/passwd file.
(Which is fine too but won't catch users in NIS/yp or LDAP or other
type of account database.

> Now I just wonder: is there any similar functionality in Emacs? Is there
> any way to cd to specific user's home directory?

Of course this is where I get a confused look on my face and say, yes,
simply use the emacs 'cd' function.

    cd is an interactive compiled Lisp function in `files.el'.

    (cd DIR)

    Make DIR become the current buffer's default directory.
    If your environment includes a `CDPATH' variable, try each one of
    that list of directories (separated by occurrences of
    `path-separator') when resolving a relative directory name.
    The path separator is colon in GNU and GNU-like systems.

Bob



reply via email to

[Prev in Thread] Current Thread [Next in Thread]