My favorite new one that I have been using a lot: function cheat() { curl cht.sh/$1 } It queries the cht.sh cheatsheet of various Unix commands. 'cheat tar' prints: # To extract an uncompressed archive: tar -xvf /path/to/foo.tar # To create an uncompressed archive: tar -cvf /path/to/foo.tar /path/to/foo/ # To extract a .gz archive: tar -xzvf /path/to/foo.tgz # To create a .gz archive: tar -czvf /path/to/foo.tgz /path…
Ask HN: Best things in your bash_profile/aliases?
241–250 of 288 posts
Re: Ask HN: Best things in your bash_profile/aliases?
#242Sudo re-run last command. alias hadouken='sudo $(history -p !!)'
Re: Ask HN: Best things in your bash_profile/aliases?
#243Re: Ask HN: Best things in your bash_profile/aliases?
#244They work kind of like z/j, but you don't have to have been there before and it will constrain to the current tree.
down SUB1 SUB2[...] does a breadth-first search down-tree from your current location, and takes you to the first directory with all the given substrings in order in its path.
This differs from just wrapping basic find . -ipath, which does depth-first. That way, down libs lands you in ./src/libs instead of ./src/a_subcomponent/foo/bar/libs.
up SUB1 does the same, but moves you up the tree instead. I didn't do multiple substrings here because I never needed them--I mostly use them with down to anchor to a subtree (e.g., down a_sub libs if I wanted the above result).
Both are case-insensitive as written. Change ipath to path in down and grep -oi to grep -o in up to make them case sensitive instead.
They may or may not be symlink safe, and may or may not be spaces-in-paths safe. Neither is in play in my source bases.
Last note, these were originally written in fish (ask me for those versions if you want them) before I ported them to bash, so may not be examples of stunning bash code. They've been tested on bash 3.2 (Mac), 4.x, and 5.0.
function joinstr {
local IFS="$1"
shift;
echo "$*"
}
function down {
if [ -z "$1" ]; then
echo "Usage: down SUBSTR [SUBSTR...]"
return 1
fi
# foo bar baz becomes *foo*bar*baz*
local pathspec=( "$@" )
pathspec="*"$(joinstr "*" ${pathspec[@]})"*"
local depth=1
local dest=""
# iterative depth first traversal to simulate breadth-first
while true; do
# Anything at all at this depth?
local first_result=$(find . -mindepth "${depth}" -maxdepth "${depth}" -type d -print -quit)
if [ -z "${first_result}" ]; then
# nope, we're done
break
fi
# How about what we're looking for?
dest=$(find . -mindepth "${depth}" -maxdepth "${depth}" -ipath "${pathspec}" -type d -print -quit)
if [ -n "${dest}" ]; then
# found it!
break
fi
depth=$((${depth} + 1))
done
if [ -z "${dest}" ]; then
echo "down: could not find path matching ${pathspec}"
return 2
fi
echo "down: changing directory to ${dest}"
cd "${dest}"
}
function up {
if [ -z "$1" -o -n "$2" ]; then
echo "Usage: up SUBSTR"
return 1
fi
# look for substr starting at parent
local dest=$(echo ${PWD%/*} | grep -oi ".*$1[^/]*")
if [ -z "${dest}" ]; then
echo "up: could not find path matching *$1*"
return 2
fi
echo "up: changing directory to ${dest}"
cd "${dest}"
}Re: Ask HN: Best things in your bash_profile/aliases?
#245Earlier quoted context omitted.
There are man pages though, but still I suppose that might be useful to you. Anyways, the only options for tar that I commonly use are "c", "x", and "t"; I will use other programs for decompression, and for extracting into a directory, will use cd.
If the man pages had useful examples the “cheat sheets” would not be needed. So “duh use man pages” is a wrong and annoying answer in this context.
Re: Ask HN: Best things in your bash_profile/aliases?
#246I have two programs that open Firefox. I also have some other programs, too. fav #!/bin/bash -- f0() { echo 'select moz_bookmarks.title || '"'"' = '"'"' || url from moz_places, moz_bookmarks on moz_places.id = moz_bookmarks.fk where parent = 2;' | sqlite3 /home/user/.mozilla/firefox/twht79zd.default/places.sqlite } f1() { firefox `echo 'select url from moz_places, moz_bookmarks on moz_places.id = moz_bookmarks.fk whe…
#!/bin/bash --
curl 'http://ipinfo.io'
ipinfo is a lot more verbose. Depending on your needs...Ex result: $curl ipinfo.io { "ip": "XXX.XXX.XXX.XXX", "hostname": "XXX.XXX.XXX", "city": "XXX", "region": "XXX", "country": "US", "loc": "XXX,XXX, "postal": "XXX", "phone": "XXX", "org": "ASXXX XXX XXX XXX" }
Re: Ask HN: Best things in your bash_profile/aliases?
#247My favorite new one that I have been using a lot: function cheat() { curl cht.sh/$1 } It queries the cht.sh cheatsheet of various Unix commands. 'cheat tar' prints: # To extract an uncompressed archive: tar -xvf /path/to/foo.tar # To create an uncompressed archive: tar -cvf /path/to/foo.tar /path/to/foo/ # To extract a .gz archive: tar -xzvf /path/to/foo.tgz # To create a .gz archive: tar -czvf /path/to/foo.tgz /path…
Cool, but on my Mac it makes flashing text..arggh.
Re: Ask HN: Best things in your bash_profile/aliases?
#248Earlier quoted context omitted.
Cool, but on my Mac it makes flashing text..arggh.
Add -s to curl, should fix it.
Re: Ask HN: Best things in your bash_profile/aliases?
#249Earlier quoted context omitted.
I like to put useful information in the prompt, but yours looks too cluttered for my liking. I particularly dislike the fact that most of the information of your prompt is nearly constant, thus very redundant. Putting the CWD inside the prompt is useless. You can just run pwd yourself if you are lost, but most of the time you already know where you are. Similarly for the username and the hostname. If you only deal wi…
That's all quite easy to change in the rprompt() function. I tried hard to factor everything out, so the prompt content specification itself is clear and obvious. Basically, for the reasons you mention, I dislike having that info set in PS1, but I find it considerably less distracting when right-justified. Having it visible, though, makes it easy to ascertain the execution context of commands in the terminal history…
Sure! the idea is that background jobs are very rare, and their existence should be clearly indicated in the prompt (in bright red). Usually, there are zero jobs and the prompt is silent about that.
Re: Ask HN: Best things in your bash_profile/aliases?
#250 vi()
{
emacs -nw --eval '(and (switch-to-buffer "foobar") (setq viper-mode t) (setq viper-inhibit-startup-message t) (setq viper-expert-level 1) (viper-mode))' "$@"
}
alias vim=vi
alias vim.tiny=vi
alias vim.basic=vi