Don't waste your time by cd-ing in the terminal
huyng.com
huyng.com
`z` tracks your most used directories. After a short learning phase, it will take you to the directory, based on its usage frequency and a hint you give it on the command line. Say I am often cd'ing into /var/www - then after a while I can just type `z ww`.
It's become so ingrained in my fingers that whenever I switch to a new shell it's the first thing I port over:
I think it's illustrative that the developer went through a Python-based version on the path to building z.
1. Go to the directory in question
2. Type "save nm" where "nm" is any short name for the directory, like "blog" or "blg"
3. From now on, type "cd nm" whenever you want to go there.
No session restart required, even.
I copied the code that enables this workflow from http://dotfiles.org/~jacqui/.bashrc (or was it https://gist.github.com/117528)
The idea is discussed here: http://hints.macworld.com/article.php?story=2002071600512379...
setopt cdablevars
nm=/path/to/whatever
cd nm shopt -s cdable_vars foo=/etc/
cd $foo touch ~/.zshmarks && source ~/.zshmarks
function b() {
echo "hash -d $1=\"`pwd`\"" >> ~/.zshmarks && source ~/.zshmarks
} echo "export nm=`pwd`/blog"' >> ~/.profile
. ~/.profile
cd $nm
There's no need for magic when you know how to shell. shopt -s cdable_vars
nm=`pwd`/blog
cd nm
No need for hacks when you know how to shell. $ cd /
$ nm=/foo
$ ls | grep nm
$ cd nm
$ pwd
/foo
$ cd ~
$ ls | grep nm
nm
$ cd nm
$ pwd
~/nm
$ echo FFFFFUUUUUUUUU
Inflexible: $ cd nm/jan/img
cd: No such director nm/jan/img
$ echo SHIT
SHIT
$ cd $nm/jan/img
Worse, It's nonstandard and only saves you one chracter.This is my blog.
FWIW, I've actually switched away from using the script mentioned in the link posted by OP and have moved towards using an improved version below.
http://www.huyng.com/bashmarks-directory-bookmarks-for-the-s...
This new version has 3 commands:
- "s" for save current directory as a bookmark
- "g" for jump to bookmark's directory
- "l" for list all bookmarks.I see a lot of people -- particularly vi users -- cd'ing back and forth through a large directory tree. I usually tell them to get a terminal emulator that lets you easily manage many terminals open. Open one per directory you want to operate in, for instance. Learn how to switch back and forth between the different shells.
But most importantly, don't quit the program to switch back and forth between directories and files. Learn to use your editor of choice properly: how to view directory listings, how to switch back and forth, etc. Vim can do this just fine btw. The choice of tool here doesn't matter so much. Just pick one and learn it. This applies to your choice of terminal emulator, shell, editor, etc.
This is a common source of headache when using the terminal as an IDE. Not only is it easy to get lost in a sea of terminal-tabs, but it's also quite cumbersome to restore the state of 5+ tabs after a shutdown or disconnect. Even more so when GNU screen enters the mix.
What I'd really like to have is a terminal that can attach to a remote GNU screen and display the screen-windows as local tabs.
Yes, screen, tmux and vim have split window modes, but those are cumbersome to operate (and I'm saying that as a year-long ion3 user) and the scrollback issue has not been addressed by either up to this day.
Or, to put it more generally: Terminals are sadly stuck firmly in the 1970s. There has been near zero innovation beyond emulating them in tabbed windows and setting xterm titles. Heck, we're moving backwards. OSX ships with a terminal that doesn't even support 256 colors. ZModem is unheard of except in fairly exotic/old emulators such as Zoc. Support for "advanced" terminal features (double-size fonts, graphics mode) is rare.
I spend >8hrs/day inside a terminal. I would happily pay a 3 digit license fee for a modern terminal emulator that adds the features I mentioned and innovates beyond. There's infinite room for innovation by leveraging special ESC-sequences (server tells terminal what to do), drag & drop, integrating with tools like screen, ssh or even building new CLI tools that interface with the terminal in a smart way...
I want vim to tell my terminal to display NerdTree in a native side-car widget like TextMate. I want an "open" CLI-command that downloads the target-file from the remote server and displays it locally without me having to futz with scp. I want to drag & drop files onto the server that I'm currently ssh'd into. I want the term to maintain my entire session (including all tabs and remote connections) across reboots. And, yes, I'd like to have my remote screen windows line up neatly as native tabs.
You can get this by editing files over sshfs.
In general though, I find that sort of persistence to be somewhat unstable. I'd rather know that everything I have done is documented and backed up than just sitting in some sort of dump of memory.
Been there, it's a kludge and tends to not cope well with network disconnects. Also when sshfs flakes out then it has a habit of leaving you with 0-byte files.
Also note this doesn't fully address the problem. I literally want to drag/drop into the current directory of whatever server I'm ssh'd into (possibly with multiple ssh-hops in between).
This has been possible in the 80's, it's called ZModem. The server-side part is still in your favorite linux distribution (lrzsz). Sadly the client-side support has disappeared from almost all terminal emulators (Afaik only Zoc still supports it).
With Zoc you can just drop a file and it will send the ZModem init-string, which conveniently launches the 'rz' binary on the server. Likewise you can say 'sz file', Zoc will detect the init-string and open a download-dialog.
Forgotten technologies... not all of them were bad.
I started trying to build out something like this based on vim's netrw stuff but it never really worked out. I'm not even sure if what I want is part of vim or if it's some nebulous other tool.
Obviously this requires that you use Emacs instead of Vim though. Don't be afraid though, the grass is pretty green over here :-).
Vim has this functionality built right in: http://vimdoc.sourceforge.net/htmldoc/pi_netrw.html#netrw
However, as said above, for me Tramp/netrw are kludges that tend to add more problems than they solve. A real solution can't be constrained to a particular editor.
Though the latency of my satellite connection outweighs the shortcomings of sshfs. The thing about tramp is that where sshfs hangs up directory listing and file loading, tramp regularly hangs up the editor. (And tramp should really turn off autosave by default.)
* http://www.skamphausen.de/cgi-bin/ska/CDargs
* http://github.com/joelthelion/autojump
* http://github.com/flavio/jump
Also, the title is misleading. I thought it was actually a commandline tool that removed the need to cd most of the time, like lightning: http://tagaholic.me/2010/04/08/lightning-speed-for-your-shel...
(cd /path; command)
This will cd to /path, run command, but return you to your original working directory. This works because the parens create a sub-process, and the cd command only affects that sub-process.And there's a wikipedia entry for this even: http://en.wikipedia.org/wiki/Pushd_and_popd
help <command>Some helpful aliases to manage them:
alias 1='cd -1'
alias 2='cd -2'
alias 3='cd -3'
alias 4='cd -4'
alias 5='cd -5'
alias 6='cd -6'
alias 7='cd -7'
alias d='dirs -v'
alias h='history'
alias j='jobs'
Just one of the many reasons to use ZSH.
What shell doesn't have a directory stack?
setopt cdablevars
proj=$HOME/Projects
cd proj
pwd # => /Users/sjs/Projects function directory_up {
pushd -1
zle reset-prompt
}
zle -N directory-up directory_up
bindkey "\e[5~" directory-up
Ditto for "pushd +0" and PgDn.For instance, I have a project called "structured-prediction" deep in some folder hierarchy, and I can just type "j stru" or "j pred" or even "j uct" and it goes to the directory.
The best part is that it figures this out automatically. You don't have to remember to bookmark anything.
A weekend project, perhaps...
alias tree="find . -name .svn -prune -o -name .DS_Store -prune -o -name .git -prune -o -print | sed -e 's;[^/]*/;|___;g;s;___|; |;g'"
It shows your directory listing in the form of a tree. Easier to visualize.
Krusader also has a built in console pane.
Not strictly a terminal as the console is running over KDE over X, ..., but works for me.
Will give it a shot however, good effort.
c(){ cd "$@" && ls;}
This has probably saved me days over the years, as I almost always want to list a directory after I change into.Having "cd foo" potentially take you to an entirely different location can be dangerous, to say the least.
cd foo
rm -f *
Removing predictability from the 'cd' command is not a good idea. If you want a smart 'cd' then just call it 'j' or something else. Easier to type, too.And you do know that CDPATH also affects shell scripts, right?
I hear what you're saying, though I'm not really persuaded by this argument. When you 'cd' into a directory along your CDPATH, Bash will print out where you end up when you arrive. Here's an example of what I mean (easier to see than explain):
circe ~ ❯❯ cd bin
circe bin ❯❯ cd ithaca
/Users/circe/code/ithaca
circe ithaca [master•] ❯❯
The regular cd simply takes me where I asked. When I cd and use CDPATH (in the second case), I get told where I end up. Sure, there might be four or five different 'ithaca' folders on various machines and even on one machine, but I think that extra print-out really makes it unlikely that I will get confused.> And you do know that CDPATH also affects shell scripts, right?
No, I'm embarrassed to say, I never thought of this. And this part does sound like a potential problem. When I write Bash scripts, I always use full paths, but I see where my having CDPATH set puts me at danger from other people's scripts. Technomancy gives a concrete example above. Although I think this is bad practice on their part (not to use full paths), I appreciate the warning.
However, your question suggests that you're probably one of the chosen few who could actually use this feature safely. My general advice against it was aimed at the 99% other people who think "Oh convenient!" without being fully aware of the implications.
So, no, Bash doesn't implicitly export the variable. (Thanks to everyone for helping me improve my dotfiles a little bit.)
function to {
cd ~/Sites/$1/
}
eg. cd ~/Sites/coenhyde.com$ to coenhyde.com
> alias to='cd ~/Sites/'
Edit: On testing it doesn't work. 'to sitename' just goes straight to ~/Sites/. I knew there must have been a reason i used a function instead of an alias.
alias ba='vim ~/.bash_aliases; source ~/.bash_aliases'
Just make aliases for all the directories you go to a lot. I have twenty-something different aliases that start with 'cd'.A bookmarking system seems like overkill to solve this problem.
For example:
cd /Users/username/Library/Application\ Support
s app_support
works great but if you do this it won't work: g app_support
I opened an issue on GitHub and after that I tried to fix it on my own. I never wrote a bash script and I'm really proud to have fixed this problem on my own.Here are changes I made:
# save current directory to bookmarks
touch ~/.sdirs
function s {
cat ~/.sdirs | grep -v "export DIR_$1=" > ~/.sdirs1
mv ~/.sdirs1 ~/.sdirs
escaped_path=${PWD/ /\\ }
echo "export DIR_$1=$escaped_path" >> ~/.sdirs
}
# jump to bookmark
function g {
source ~/.sdirs
path=$(eval $(echo echo $(echo \$DIR_$1)))
# replace whitespaces with "\ " for escaping
escaped_path=${path/ /\\ }
cd_eval="cd $escaped_path"
eval $cd_eval
}
Hope this helps you guys like it helped me.
And if there is a way to do this in an more elegant way, please let me know. This would help me to improve my none existing bash skills :DEdit: I opened up a fork and commited all my changes to this repo. I also opened a pull request and I hope my fix will get accepted.
GitHub fork: https://github.com/Oemera/bashmarks
Thanks
Ömer
In the meantime, this kicks the pants off separate Finder + Terminal action: http://decimus.net/DTerm/
I'm always plugged in to my server with tramp and I've got multiple projects all bookmarked. It makes hopping around real easy, makes it feel like a browser more than an editor.
http://geocities.com/h2428/petar/bash_acd.htm
http://geocities.com/h2428/petar/bash_hist.htm
highly recommended.
2. Use real shell like ksh, where history search actually works
3. cd $OLDPWD is occasionally helpful. Occasionally.
Kids...
Instead of trying to remember where all your libraries are located, it will just open them up for you. Great for trying to debug misbehaving code.
I've been using this for a few years and it's been a joy.
ie: hwst - cd /usr/Brad/Desktop/Dropbox/Brad/howas.it/repos/howasit_alpha/ etc...
cd /home/nick
ln -s /var/www www
cd www
echo $PWD ---> /home/nick/www
which will break scripts.