Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You know what I hate? My history isn't aggregated in real time across all my Mac OS X terminal windows.

You know what else I hate? Typing in long commands in the Mac OS X terminal and then them wrapping weirdly. Especially when I hit the up arrow to go back in my terminal history.



Here you go, just throw it in your .bashrc:

  shopt -s histappend
  export HISTSIZE=100000
  export HISTFILESIZE=100000
  export HISTCONTROL=ignoredups:erasedups
  export PROMPT_COMMAND="history -a;history -c;history -r;$PROMPT_COMMAND"
I wish I could give credit to where I originally found this but it was ages ago.


I've been using `history -a`, but what do `history -c` and `history -r` do?

It looks like it clears your shell's history, and then re-populates it from the .bash_history file?


   history -a  # append history lines from this session to the history file.
   
   #History file may contain history from other terminals not in this one so:

   history -c # clear [in-memory] history list deleting all of the entries.
   history -r # read the history file and append the contents to the history list instead.
I've heard that -n can be problematic which is why -c then -r is used.


Many thanks for this! This has been a thorn in my side for a while but never got round to doing anything about it...


Don't you want HISTCONTROL=ignoreboth ?


    > You know what else I hate? Typing in long commands in the Mac OS X terminal and then them wrapping weirdly.
Yeah, keeping ctrl pressed in and pressing x followed by e (CTRL + x e) will open up the current line in $EDITOR and when you edit and save, replaces the current line with what you entered in your editor. Really a killer feature.


In Linux with bash (and probably other shells that are bash-compatible), you can use vi to edit any of the commands in your command history, and then execute the edited version.

Do this at the command prompt, or once in your ~/.bash_profile to make it permanent:

set -o vi

After that, you can search for any of the commands in your history, edit it, and then execute the edited command, by doing this:

At the prompt, type Esc once to get into vi command mode. Then you can press the k key repeatedly to scroll up through the command history, or (often easier) use the ? (search backward) vi command to search for a pattern to find a specific command. Once found, press v to edit it in a temp file. Then when you save and quit, the edited command gets executed.

The same technique works with emacs as the editor instead of vi, if you don't give the 'set -o vi' command, because the default editor for command line history is emacs. Also, if you have run 'set -o vi', you can switch the editor for commands back to emacs with 'set -o emacs'.


Or emacs.

The 'set -o <editor>' bit sets the readline editing environment to be similar to vi. It can be set to emacs.

C-xC-e (edit-and-execute-command) invokes the editor specified by $VISUAL, $EDITOR, or emacs, in that order. You could set it to scrivner if you wanted to (though I'm not sure that would necessarily work on exit).

I've tested with VISUAL set to nedit, from which I then changed it to uptime. Now I get loadavg when I want to edit my command line ;-)


If you like that, you can put:

set editing-mode vi

in your ~/.inputrc (instead) and all GNU libreadline using programs will give you vi keys instead of emacs :-)


Cool :)


Yes, that is also a solution. However, you cannot use vim so if you're used to vim, it might feel limited without certain commands. You're also missing eventual plugins.

It's better than nothing though.


I don't think this is the case, though I may be misunderstanding what you wrote.

At the shell, it's true that you're limited to vi-style controls (so no text objects like ci").

However, once you press 'v' in normal mode you can drop into either vi or vim - this only depends on what $EDITOR is set to[1].

[1] http://blog.sanctum.geek.nz/vi-mode-in-bash/


Yes works with ksh, been using it for decades, very handy.


> My history isn't aggregated in real time across all my Mac OS X terminal windows.

zsh has `setopt inc_append_history` and `setopt share_history`. i am sure bash has something similar.


>You know what else I hate? Typing in long commands in the Mac OS X terminal and then them wrapping weirdly.

Are you escaping your color codes in PS1 correctly?

Bad:

    PS1="\033[1;32m \w \033[m $"
Good (notice the extra \[ and \]):

    PS1="\[\033[1;32m\] \w \[\033[m\] $"


Mine seems escaped as hell, but still gets wrapped. Anything I'm missing?

edit: couldn't get UTF-8 branch symbol to be properly displayed here, but this is how it looks:

http://i.imgur.com/zuq24gV.png?1

  function fancyPrompt {

   local bgBlue="\[\033[48;5;31m\]"
   local fgBlue="\[\033[38;5;31m\]"

   local fgWhite="\[\033[38;5;231m\]"

   local bgDarkBlue="\[\033[48;5;24m\]"
   local fgDarkBlue="\[\033[38;5;24m\]"

   local bgDarkGray="\[\033[48;5;237m\]"
   local bgLightGray="\[\033[48;5;245m\]"
   local fgLightGray="\[\033[38;5;245m\]"
   local colorClear="\[\033[0m"

   local branch
   local branch_symbol="\[\] "

   if branch=$( { git symbolic-ref --quiet HEAD || git rev-parse --short HEAD; } 2>/dev/null ); then
   	branch=${branch##*/}
   	export PS1="${bgBlue}${fgWhite}\h${colorClear}${fgBlue}${bgDarkBlue}\[\] ${fgWhite}\w${bgLightGray}${fgDarkBlue}\[\] ${fgWhite}${branch_symbol}${branch}${fgLightGray}${bgDarkGray}\[\] ${colorClear}"
   else
   	export PS1="${bgBlue}${fgWhite}\h${colorClear}${fgBlue}${bgDarkBlue}\[\] ${fgWhite}\w${bgDarkGray}${fgDarkBlue}\[\] ${colorClear}"
   fi

 }


That's quite pretty. How do you get the triangles?

I've been loving a prompt which color codes git branches (which, if I understand right, would be built-in if I used zsh instead of bash) [0], though I have to edit the last line in order to get my history appendation working as well.

0: https://gist.github.com/tobiassjosten/828432



Try not escaping around the triangles and branch symbols.


This. Right Here. For the longest time I could not figure out why my Ctrl-r (history) was so messed up.


> You know what else I hate? Typing in long commands in the Mac OS X terminal and then them wrapping weirdly.

I solved this by adding the following line in my ~/.bashrc

  shopt -s checkwinsize
Never tried on Mac OS X though, so I don't know if it works.


> Typing in long commands in the Mac OS X terminal and then them wrapping weirdly.

For all people who dont use Mac OS X:

This happens because your PS1 is wrongly set and bash cant calculate correctly the length left of your line. Try it out, by going back to default with no colors and crap and see how long it goes.

For mac os x users. The above wont help, dont even try it.


I found the solution to bash problems was to move to zsh. I did that almost 10 years ago and haven't regretted the decision.


I switched to zsh for a few hours once, but it broke TRAMP in Emacs. There is probably a work around...


Can you suggest an intro guide to zsh for former bashers?


I prefer to keep mine separate, but I always use specific terminals / screen sessions for (mostly) the same type of work (admin, specific projects, web dev, etc.) so each have their own history, as well as keeping a per-directory record.

So far over 130000 command lines (with timestamp & cwd) for past 2.5 years, just on my laptop.


You know what I hate? My history isn't aggregated in real time across all my Mac OS X terminal windows.

I used to feel this way. Then one day I figured out how to enable this in Bash. Resulted in a confusing mess. Turns out you most likely want terminal sessions to be distinct until you end them.


You should just move to zsh that does this natively and in a clean manner.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: