bash function: md.view
One of my favorite bash functions is md.view, which is available in my dotfiles repository.
# test markdown files, probably a better way to test for programs.
function md.view {
local i=true
type -p markdown &> /dev/null || i=false
if $i ; then
local output="/tmp/md.view-$(date +%F).html";
markdown $1 > "$output";
google-chrome "$output"; # xdg-open would open default browser after next line executes
rm "$output";
else
echo "markdown is not installed"
fi
}
There's not really anything particularly cool about this function. It checks for markdown and reminds me to install it if I haven't yet. It doesn't do this for Google Chrome because that's the first application I install on new machines. Usually, you'd use xdg-open to choose a browser, but this can cause problems with the remove command at the end of the function (depending on how the browser creates its process).
Anyway, this is really useful when I'm creating README files using markdown for my github repositories. For instance, I was just updating a README to refer to *_32.png images. The problem is that * and _ are special tokens in markdown. I'd never attempted to escape one after the other, which is often broken in parsers. With this function in my extended .bash\functions, I can quickly check output with:
$ md.view README.md
I know, it's such a simple thing to have as a favorite. Sometimes really simple things have a huge impact, especially if you type out README files regularly
‘Upgrading’ couchdb on Ubuntu 12.04
If you've installed couchdb from the Ubuntu repositories, you're likely running 1.0.1. If you decide to build and install couchdb from source, you may have some issues with the server starting after a reboot.
The trick, as outlined here is to perform the following:
sudo mv /etc/init.d/couchdb /etc/init.d/couchdb.1.0.1.donotuse sudo ln -sf /usr/local/etc/init.d/couchdb /etc/init.d/couchdb sudo update-rc.d couchdb defaults
This assumes you've configured the source with --prefix=/usr/local (which is the default).
Re-linking the init script should allow you to start/stop/restart the server as expected.
Ubuntu: Open With… Wireshark
Reading through Practical Packet Analysis, I ran local captures to follow along until Chapter 5. Instead of trying to simulate an improperly-dissected packet, I downloaded the captures which accompany the book. In Ubuntu 11.10, I didn't see any way to easily associate .pcap files with Wireshark. In previous versions of Ubuntu, the 'add' button was available in the 'Open with...' dialog, but in 11.10 it is grayed out. Clicking the option to choose another application or find an available application online didn't work. Bummer.
To get wireshark to show up in the 'Open With...' dialog's choice of applications, I followed the instructions on AskUbuntu.com. The instructions are for vim, so I've modified them for wireshark.
Save the following as ~/.local/share/applications/wireshark.desktop:
[Desktop Entry] Encoding=UTF-8 Name=Wireshark Comment=Wireshark packet capturing Exec=wireshark %u Terminal=false Type=Application Icon=/usr/share/pixmaps/hi48-app-wireshark.png Categories=Application;Utility; StartupNotify=true MimeType=application/octet-stream NoDisplay=false
For more information about .desktop files, check out the Desktop File Specification.
dotfiles backup using GitHub
I was recently looking for a solution to backup my configuration files (bash, vim, etc) using GitHub. After some looking around, I've compiled a pretty nice project for myself.
First, this script checks dependencies. My dependencies are git, ruby, vim, tree, rake, gem, bundle, and trash. You could check out the code and add any number of dependencies here. Rubygems and bundler are required because the script later installs all gems listed in Gemfile.
Next, the script copies ~/.bashrc to ~/.bashrc.local. This allows you to keep your current bash configuration as a 'local-only' config that doesn't get copied or committed to github.
The script, as I copied most of bootstrap.sh and the rakefile from @gf3, expects the repository to be cloned to ~/.dotfiles. From there, it calls rake.
Rake looks at every file in ~/.dotfiles and copies the corresponding file relatively from ~/ to, essentially, ~/dotfiles-backup/`date`. I recommend first running the backup to make sure your files are properly backed up.
rake backup
The script then calls 'bundle install' to install all gems. It then copies all files from ~/.dotfiles to replace those relative files that were previously backed up from ~/.
The post-install displays a message to remind you to edit .gitconfig and .hgrc.
Because I've done some copying and compiling, these are relative close to the three projects in the README for right now.
Here is an excerpt from the README:
Bash
$ tree ~/.bash
/home/jim/.bash
├── aliases
├── completions
├── completion_scripts
│ └── git_completion
├── config
├── functions
├── paths
└── prompt
The above files are loaded by .bashrc. The files are pretty self-explanatory, other than prompt which colorizes the bash prompt with tweaks for git.
Cool Aliases
- cd : pushd
- bd : popd
- cd.. | .. : back one directory
- cd... | ... : back two directories
- ^ up to five directories
- rm : trash
- undopush
- ip
- GET | HEAD | POST | PUT | DELETE | TRACE | OPTIONS
Config
- sets editor to vim
- sets English/UTF-8
- sets manpager
- sets commands to ignore in history
- sets noclobber (e.g. prevents
cat > IMPORTANT_FILEmistakes ) - sets nocaseglob (e.g.
ls ~/.B*will list contents of~/.bash)
Functions
The two functions, md and c may not seem like much, but they simplify some commands. For example:
$ md projects; git clone git@github.com:jimschubert/dotfiles.git && cd dotfiles
In the above line, md will create the projects directory and cd into it.
c stands for 'code' and works like this:
jim at computer in ~
$ pwd
/home/jim
jim at computer in ~
$ c dotfiles
~/projects/dotfiles ~
jim at computer in ~/projects/dotfiles on master
$
You can change it to whatever shortcut and issue reload, which is also an alias from this setup.
Screenshot
Notice the color scheme and github branch notifications created by ~/.bash/prompt.
Mercurial and Git in one repository
I have a personal project hosted at bitbucket using Mercurial as the version control system. Since I started that project, I've been using GitHub for everything. I just found hg-git instructions on github.com. It is pretty awesome.
For my purposes, it allows me to maintain the project on two remote servers, one running Mercurial, the other running Git.
To see how it works, first install hg-git:
$ sudo easy_install hg-git
Then, edit your ~/.hgrc settings, adding to the [extensions] section:
[extensions] hgext.bookmarks = hggit =
Also, be sure your ~/.hgrc contains a valid email address:
[ui] username = Jim Schubert <james.schubert@gmail.com>
Now, you can create a repository on github.com and push your Mercurial commits:
$ cd ~/projects/project_name $ hg bookmark -r default master $ hg push git+ssh://git@github.com/username/project_name.git $ hg push
If you're only planning on using Mercurial to push changes to github or some other Git host, you can add that path to ~/.hgrc:
[paths] default-push = git+ssh://git@github.com/username/project_name.git
Running additional Google Chrome profiles in Linux
Google Chrome supports running multiple profiles.
In newer versions of Google Chrome, you'll be able to enable profiles by navigating to chrome://flags and enabling Multiple Profiles. Restart the browser, and you'll have a switcher and multiple profiles which can be activated in different windows. This allows you to sync to different gmail accounts. For instance, if you have a personal account and a work or school account, each with different bookmarks... you can run two or three different profiles and have access to all of your data.
If you're using an older version of Chrome or Chromium and you don't have the Multiple Profiles option, you can use the switch
--user-data-dir
when opening the browser and the profile for your session will be pulled from the given folder instead of the Default folder. The folder specified should be created automatically. To be safe, I like to create the folder before providing it as an argument.
As explained here, the default directory is located at:
Google Chrome: ~/.config/google-chrome/Default Chromium: ~/.config/chromium/Default
Open a terminal and create a new directory for your second profile. For instance:
mkdir ~/.config/google-chrome/work
Then, open Google Chrome with the switch:
google-chrome --user-data-dir='~/.config/google-chrome/work' &
If this is a profile you'll be using often, you can also create a menu entry under Applications -> Internet ('Chrome|Work', maybe?).
Linux tip: Alias ‘cd’ in bash shell
A while ago, I stumbled across an excellent article at O'Reilly with tips for your bash shell.
I wanted to expand on the "pushd/popd" section a little.
First, check your ~/.bashrc file for the following lines:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
This checks that the ~/.bash_aliases file exists, and if it does, it loads it using the '.' builtin. If you aren't familiar with the source/. builtin, here is an excerpt from the documentation:
. (a period)
. filename [arguments]Read and execute commands from the filename argument in the current shell context. If filename does not contain a slash, the PATH variable is used to find filename. When Bash is not in posix mode, the current directory is searched if filename is not found in $PATH. If any arguments are supplied, they become the positional parameters when filename is executed. Otherwise the positional parameters are unchanged. The return status is the exit status of the last command executed, or zero if no commands are executed. If filename is not found, or cannot be read, the return status is non-zero. This builtin is equivalent to source.
Now, create or edit ~/.bash_aliases with the following content:
alias cd="pushd" alias bd="popd" alias cd..="cd .." alias cd...="cd ../.." alias cd....="cd ../.." alias cd.....="cd ../../.." alias cd......="cd ../../../.."
This aliases cd to push the result onto the directory stack (pushd). Then, we introduce a bd command to pop the last directory off the stack (popd).
If you're like me, you get tired of typing cd ../../../.., which is still faster than typing cd /home/jim/projects/al [TAB]/src or something similar. So, the last lines alias quite a few levels of directory changes so you type cd.. followed by the number of directories you want to navigate back.
Here are some examples:
jim@schubert:~$ cd projects/newtabredirect/ ~/projects/newtabredirect ~ jim@schubert:~/projects/newtabredirect$ cd ~/projects/select-actions/ ~/projects/select-actions ~/projects/newtabredirect ~ jim@schubert:~/projects/select-actions$ cd ~/Documents/blogs/ ~/Documents/blogs ~/projects/select-actions ~/projects/newtabredirect ~ jim@schubert:~/Documents/blogs$ bd ~/projects/select-actions ~/projects/newtabredirect ~ jim@schubert:~/projects/select-actions$ cd.... ~ ~/projects/select-actions ~/projects/newtabredirect ~ jim@schubert:~$ pwd /home/jim

