I Prefer Jim Developer James Schubert shares his code and his thoughts.

18Jan/12Off

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.

github:jimschubert/dotfiles

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_FILE mistakes )
  • 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.

flattr this!

29Oct/11Off

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

flattr this!

Tagged as: , No Comments
14Aug/11Off

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?).

flattr this!

23Jul/11Off

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

flattr this!

Tagged as: , No Comments