News for hackers. Stuff that matters.
Re:Unix Tricks by curiousHacker on Thursday July 03, 2014 @03:51AM (#7981996)
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.

Re:Unix Tricks by Tiksi on Thursday July 03, 2014 @04:08AM (#7982031)
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.
Re:Unix Tricks by gknoy on Thursday July 03, 2014 @01:06PM (#7984280)
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?

Re:Unix Tricks by Flenser on Friday July 04, 2014 @04:28AM (#7987857)

   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.
Re:Unix Tricks by mtford on Thursday July 03, 2014 @05:50AM (#7982228)
Many thanks for this! This has been a thorn in my side for a while but never got round to doing anything about it...
Re:Unix Tricks by grimgrin on Thursday July 03, 2014 @09:59AM (#7983072)
Don't you want HISTCONTROL=ignoreboth ?
Re:Unix Tricks by diggan on Thursday July 03, 2014 @05:56AM (#7982241)

    > 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.
Re:Unix Tricks by vram22 on Thursday July 03, 2014 @09:54AM (#7983032)
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'.

Re:Unix Tricks by dredmorbius on Thursday July 03, 2014 @10:35AM (#7983267)
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 ;-)

Re:Unix Tricks by jbert on Thursday July 03, 2014 @01:28PM (#7984452)
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 :-)

Re:Unix Tricks by vram22 on Thursday July 03, 2014 @03:27PM (#7985123)
Cool :)
Re:Unix Tricks by diggan on Thursday July 03, 2014 @10:26AM (#7983213)
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.

Re:Unix Tricks by ejstronge on Friday July 04, 2014 @09:39AM (#7988669)
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/

Re:Unix Tricks by Zenst on Friday July 04, 2014 @06:39AM (#7988161)
Yes works with ksh, been using it for decades, very handy.
Re:Unix Tricks by ibotty on Thursday July 03, 2014 @03:54AM (#7982004)
> 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.

Re:Unix Tricks by Spiritus on Thursday July 03, 2014 @07:20AM (#7982427)
>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\] $"
Re:Unix Tricks by tambourine_man on Thursday July 03, 2014 @10:33AM (#7983261)
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

 }
Re:Unix Tricks by gknoy on Thursday July 03, 2014 @01:11PM (#7984318)
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

Re:Unix Tricks by staticshock on Thursday July 03, 2014 @02:29PM (#7984792)
Re:Unix Tricks by bcoughlan on Friday July 04, 2014 @12:15PM (#7989331)
Try not escaping around the triangles and branch symbols.
Re:Unix Tricks by raju on Thursday July 03, 2014 @07:48AM (#7982494)
This. Right Here. For the longest time I could not figure out why my Ctrl-r (history) was so messed up.
Re:Unix Tricks by JoeAcchino on Thursday July 03, 2014 @04:15AM (#7982045)
> 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.
Re:Unix Tricks by antocv on Thursday July 03, 2014 @12:09PM (#7983904)
> 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.

Re:Unix Tricks by mitchty on Thursday July 03, 2014 @11:38PM (#7987221)
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.
Re:Unix Tricks by eddieh on Friday July 04, 2014 @03:14PM (#7989920)
I switched to zsh for a few hours once, but it broke TRAMP in Emacs. There is probably a work around...
Re:Unix Tricks by antocv on Friday July 04, 2014 @03:26AM (#7987738)
Can you suggest an intro guide to zsh for former bashers?
Re:Unix Tricks by toddkaufmann on Friday July 04, 2014 @02:42PM (#7989803)
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.

Re:Unix Tricks by naner on Friday July 04, 2014 @12:40PM (#7989435)
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.

Re:Unix Tricks by jdong on Thursday July 03, 2014 @05:06PM (#7985830)
You should just move to zsh that does this natively and in a clean manner.