开发者

Navigating to Commonly/Recently Used Dirs, in Terminal (Unix/OS X)

Would it be possible to save certain locations that I've used recently/commonly (like /folder/folder/folder/) so that I don't have to manually开发者_JS百科 navigate through each dir between my current and destination dir?

Sort of like alt-tab, but for paths. I'm on OS X, but perhaps this can be done using basic Unix skills?

Thanks!


If you set up your shell to save commands history, using Ctrl+R hotkey may save you some time, which performs search of previously executed commands as you type. Another good thing to know is that most shells provide you with file/directory name completion if you press Tab key once/twice, which is also of great help.

EDIT: you can also make some symbolic links inside single (e.g. home) directory to quickly access directories. Use ln -s <path to target file/directory> <path to link> command. Target paths could either be relative or absolute.


One way that works in most shells (but is slightly different for different shells) is the directory stack. You can use pushd to push a directory, popd to pop the top directory from the stack, and dirs to move directories around and switch to directories in the middle of the stack. I just checked, and the man page for pushd on Mac OS X is useless; use the man page for your shell (likely bash) and search for pushd, etc. there.


In Bash, you can add directories to the CDPATH variable. If you try to cd to a directory that doesn't begin with a slash then the directories listed in CDPATH are searched for a matching destination.


In zsh there are a few options.

A shell can keep a stack of the most recent few directories you've cd'ed into if you use setopt AUTO_PUSHD. With completion set up (autoload -U compinit; compinit), type cd + and press tab, then you'll get a numbered list:

~% cd /Library
/Library% cd /System/Library
/System/Library% cd +
1 -- /Library
2 -- /Users/nicholas

So the most recent directory is +1, etc. (You can reverse this, as I do, with setopt PUSHD_MINUS so you use - instead of + and the most recent directory is -1).

Another option is directory hashing; you can create pseudo-"home directories" (~whatever). Some of mine, for example:

  hash -d v=/Volumes
  hash -d a=/Volumes/BanjoArchive
  hash -d pep=/System/Library/Frameworks/Python.framework/Versions/Current
  hash -d sp=$(print /Library/Python/*/site-packages(On[1]))

So I can just type cd ~pep or cd pep (if unambiguous) or even pep (if AUTO_CD is set). In situations other than cd, you can use the directory stack with ~ as well, e.g. ~+1 or ~-1.

zsh can even replace shell variables after a ~ with the AUTO_NAME_DIRS option, though I don't use it because it would clutter the variable list. Nevertheless, here's an example:

~%setopt AUTO_NAME_DIRS
~%v=/Volumes

~%cd ~v
~v%pwd
/Volumes

zsh also supports cdpath as one of the other answers mentions. For example, I have:

cdpath=($HOME /Volumes)

so I can just use the name of a mounted volume or directory in my home directory to cd to it.


I stumbled upon Z and it has solved all of my dir-navigating issues.


Either an alias (via placing it into your ~/.bashrc or ~/.profile depending on setup):

alias godocs='cd /home/me/my_docs`

or pushd/popd as @Jeremiah suggests.


Take a look at "Cd Deluxe" for a greatly improved "change directory" command: http://www.plan10.com/cdd/.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜