Live data from Hacker News

Ask HN: Best things in your bash_profile/aliases?

news.ycombinator.com

101–110 of 288 posts

Re: Ask HN: Best things in your bash_profile/aliases?

#101
post #80

Can I add something from .vimrc? If you put the commands: set undofile set undodir=~/.vim/undodir You get infinite undo in your files. Now when you go into a file, delete or add some text and close your terminal down, the next time you enter vim you can (re|un)do previous operations. Has saved my ass more times than I care to mention over my career!

Which version of vim added support for this?

Re: Ask HN: Best things in your bash_profile/aliases?

#102
post #80

Can I add something from .vimrc? If you put the commands: set undofile set undodir=~/.vim/undodir You get infinite undo in your files. Now when you go into a file, delete or add some text and close your terminal down, the next time you enter vim you can (re|un)do previous operations. Has saved my ass more times than I care to mention over my career!

I can't believe I didn't know about this! Thanks so much :)

I don't want to sound hyperbolic but its kind of a game changer for text editing.

I understand why developers don't have it on by default in other text editors (space) but seeing as I don't fill my HDD with movies or games or any other large media like that, i've got all the space in the world.

Re: Ask HN: Best things in your bash_profile/aliases?

#103
post #101
post #80

Can I add something from .vimrc? If you put the commands: set undofile set undodir=~/.vim/undodir You get infinite undo in your files. Now when you go into a file, delete or add some text and close your terminal down, the next time you enter vim you can (re|un)do previous operations. Has saved my ass more times than I care to mention over my career!

Which version of vim added support for this?

7.3 I think - https://lwn.net/Articles/401002/

Re: Ask HN: Best things in your bash_profile/aliases?

#104
I use bash, but really like the RPROMPT idea from zsh, so mimick that as best as I can.

It turns out that bash has a PROMPT_COMMAND variable which gets called before writing out PS1, so I use this to write my own rprompt. The main idea is just to use tput to draw a thing on the right and restore the cursor position afterward.

As is, I have something like below right-justified at each prompt:

    user@host:pwd (git-branch)
It even degrades nicely if horizontal space starts getting cramped. Actually, the code is (I believe) just posix shell, so I define relevant functions in .profile and have .bashrc and .mkshrc use them as appropriate. Since it's a bit long, here are some pastebins that should be drop-in workable:

- profile: http://ix.io/1yiC/sh

- bashrc: http://ix.io/1yiA/bash

- mkshrc: http://ix.io/1yiB/ksh

There are a couple caveats. Bash clears the whole line when deleting characters, which causes the rprompt to be cleared as well. On the other hand, mksh works as expected. Also, if you enter really long commands into the prompt, they will eventually bump into and overwrite the rprompt, instead of clearing it wholesale. I've not found this too much of a problem though.

In the code above, the rprompt() function defines the actual contents of the prompt and performs the graceful degredation. The rprompt_draw() function does the actual drawing and prompt_draw() links these two together to be used by bash, mksh, etc.

Feel free to ask my any questions or hate on my code.

Re: Ask HN: Best things in your bash_profile/aliases?

#105
post #84

Not strictly an alias, but autojump has been a huge productivity booster for me. Installing it sets up an alias of `j` which guesses the best directory to cd to based on your cd history. So instead of having to type `cd ~/Projects/someproject` you could just type `j some` and it'll end up there. https://github.com/wting/autojump

I've been using z and it's great: https://github.com/rupa/z

Re: Ask HN: Best things in your bash_profile/aliases?

#106
Here is my lot

  # setting common options
  alias cp='cp -iv'
  alias mv='mv -iv'
  alias rmdir='rmdir -v'
  alias ln='ln -v'
  alias ls='ls --color=auto --human-readable --group-directories-first --classify'
  alias grep='grep --color=auto'

  # shortcuts
  alias e=vim
  alias music=ncmpcpp
  alias g='git status'
  alias gp='git push'

  # show pretty git diff
  alias gitdiff='git difftool -y -x "colordiff -y -W $COLUMNS" | less -R'
  # go to root git directory
  alias cdgit='cd $(git rev-parse --show-toplevel)'
  # view csv files in columns
  alias csv='column -s, -t'
  # activate python venv
  alias v='source ./venv/bin/activate'

  # show weather
  alias weather='curl wttr.in/Manchester'
  # get my calendar
  alias c='gcalcli --calendar  --monday calw'
  # alternate view of calendar
  alias agenda='gcalcli --calendar  agenda `date --iso-8601`T08 `date --iso-8601`T15'

  # rot13 some text
  alias rot13="tr '[A-Za-z]' '[N-ZA-Mn-za-m]'"

  # I always forget how to echo status code
  alias exitcode='echo $?'

  # make directory and enter it
  mdc() { mkdir -p "$@" && cd "$@"; }
  # create a file and make git track it
  ga() { touch "$@" && git add "$@" }
  # open a twitch stream in mpv
  twitch() { livestreamer --player "mpv -" "http://twitch.tv/$@" best }
  # set terminal window title
  title() { print -Pn "\e]2;$@\a" }
  # open a file with less or a directory with ls
  l() { if [ -f "$1" ]; then less "$1"; else ls "${1:-`pwd`}"; fi }

Re: Ask HN: Best things in your bash_profile/aliases?

#107

I use bash, but really like the RPROMPT idea from zsh, so mimick that as best as I can. It turns out that bash has a PROMPT_COMMAND variable which gets called before writing out PS1, so I use this to write my own rprompt. The main idea is just to use tput to draw a thing on the right and restore the cursor position afterward. As is, I have something like below right-justified at each prompt: user@host:pwd (git-branch…

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 with two or three servers, you can color code your prompt symbol to those, and obtain a much shorter and cuter prompt.

Useful information that is still not in your prompt:

- status of the last run command: $?

- number of jobs in the background, if any

I have this, with some colors thrown in:

    PS1='$(if [ \j != "0" ] ; then echo "[\j]"; fi)$?\$ '

Re: Ask HN: Best things in your bash_profile/aliases?

#108
I like the ix.io pastebin, since it has a nice rest interface and the url is concise. I have a wrapper function that lets you run

    ix []
and will sumbit the content of the supplied path, or stdin if no path is given.

Here's function at said pastebin (http://ix.io/1yiG/sh), and in raw form below:

    ix() {
    	curl=$(command -v curl)
    	[ -f "$HOME/.netrc" ] && curl="${curl} --netrc"
    
    	while getopts ":d:i:n:e:" opt; do
    		case "${opt}" in
    			d) ${curl} --request DELETE "ix.io/${OPTARG}"; return;;
    			i) curl="${curl} --request PUT"; id="${OPTARG}";;
    			n) curl="${curl} --form read:1=${OPTARG}";;
    			e) curl="${curl} --form ext:1=${OPTARG}";;
    			:) error "Expected argument: -${OPTARG}"; return 1;;
    			*) echo 'Usage: ix [-d ID] [-i ID] [-n N] []';
    				return;;
    		esac
    	done
    	shift $((OPTIND - 1))
    	unset -v OPTIND OPTERR
    
    	if [ -t 0 ]; then
    		filename="$1"; shift
    		if [ "$filename" ]; then
    			${curl} --form f:1=@"$filename" "${@}" "ix.io/${id}"
    			return
    		fi
    		echo "^C to cancel, ^D to send."
    	fi
    
    	${curl} --form f:1='

Re: Ask HN: Best things in your bash_profile/aliases?

#109

I use bash, but really like the RPROMPT idea from zsh, so mimick that as best as I can. It turns out that bash has a PROMPT_COMMAND variable which gets called before writing out PS1, so I use this to write my own rprompt. The main idea is just to use tput to draw a thing on the right and restore the cursor position afterward. As is, I have something like below right-justified at each prompt: user@host:pwd (git-branch…

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 buffer.

The status of last run command I have on my PS1. If it exits 0, then it's just a simple

    $
otherwise, it's:

    errno$
I do like your idea of showing current background jobs, but I find myself relying more on tmux windows than background jobs, so I suspect that number would be mostly constant for myself anyway.

Re: Ask HN: Best things in your bash_profile/aliases?

#110

A well crafted $CDPATH helps me jump from project to project and from directory to directory within a project without having to type many ../: CDPATH=./:~/:~/Documents/:[...] I often grep/ag stuff only to think afterwards that I should have done that from Vim, so I have this to rerun the previous command and feed its output to Vim's quickfix: vimq() { vim -q Speaking of Vim, this one lets me open an arbitrary file fr…

(It works, but..) What are all those backslashes for?

I use them to escape the first character of the command name, which has the effect of using the actual command and not an alias. In looking for a link to give you some background I found out that this is limited to interactive shells so those backslashes are just me not understanding what I do.
Post reply on HN