Switching git branches faster on command line

I first started with an alias in my .bash_profile

The drawback was that although it is shorter, it doesn’t give you suggestions based on existing branches when pressing “tab” (same as unix commands work for folders/files). Git checkout command does.

So I created an alias to checkout develop.

But I wanted to use it for any branch…

Then I wrote a script (see below) and added another alias

The script does a git checkout for a first branch that matches the first script argument

Full example:

And here is the script – git-checkout-fuzzy.sh:

Enjoy and please let me now in the comments below if you find it useful and it works for you.

UPDATE: In IntelliJ it is probably better to set a keyboard shortcut for VCS > Git > Branches…

Switch windows with keyboard on Mac OS X

Very nice option to switch windows is Contexts app. It allows you to switch applications using Alt+Number. It provides a sidebar with all running apps and ability to replace Cmd+Tab including smart search through currently opened windows.

Switch with search to any window
Switch with search to any window

However I often prefer to use a keyboard shortcut that doesn’t change – for example Alt+x for XCode and Alt+c for Google Chrome. BetterTouchTool is very handy for this. Just create a keyboard shorcut, use Execute Terminal Command action and put configure it with similar command:

BetterTouchTool

XCode – Open file in current tab in external editor (MacVim)

I wanted to open the current file from XCode 5 in MacVim similarly as I do in IntelliJ. After some research I found following solution that works for me. If Assistant editor is opened, the file from the Standard Editor (on the left side) is opened regardless which editor is active.

First raised as a StackOverflow question.

Update: In case you were wondering how to start MacVim from XCode, you should maybe try XVim instead. It brings Vim mode right into XCode.

Kill all application windows

As I develop an application in GWT and have IntelliJ set up to open the app in IE when debugging/running it, I end up with quite a lot of opened IE windows. I use Chrome as my main browser so I don’t mind killing all the IE windows to cleanup. To make it easy I use batch file with this command:

 

Starting Vim (gVim) from IntelliJ

Ever wanted to use Vim editing features while inside IntelliJ IDEA? Here is how to do it… You can even jump to the very same line you are on in IntelliJ.

Go to Settings > External Tools, add a new tool and use something similar:

Program: c:\Program Files (x86)\Vim\vim71\gvim.exe
Parameters: "+call cursor($LineNumber$,$ColumnNumber$)" "$FilePath$"
Working directory: $FileDir$

Update: IdeaVim – Vim commands right in the IDE

Additionally I finally tried the IdeaVim plugin for IntelliJ and my experience so far is pretty good. The need to start gVim externally almost disappeared.

Update: Open file in IntelliJ from gVim (Windows)

Save file + Open current file on the same line in IntelliJ + Close file in VIM

map! <F3> <Esc>:update<CR>:silent exe "!openIntelliJ.bat c: --line " . line(".") . " %:p"<CR>:q<CR>

  • :map! command creates a key map that works in insert and command-line mode.
  • :update is similar to ":w" or ":write", but only writes when the buffer has been modified.
  • silent prevents Hit enter cmd window.
  • line(".") returns current line number (dot is for the current)
  • %:p returns full file path of the current file
  • IntelliJ commmand line uses following syntax: [IntelliJ Path] [path1 = path to the project that contains the desired file] --line[number] [path to the file to be opened]

Here is my openIntelliJ.bat – one of the advantages is that I have just one place to change when I upgrade to a newer version (giving the exe to path variable would an option too):

start "" "c:\Program Files (x86)\JetBrains\IntelliJ IDEA 12.1.6\bin\idea.exe" %1 %2 %3 %4

The second attribute for Windows “start” command is title, I don’t need it, so I supply empty string.

References:

Adhoc logging in Eclipse

Background and motivation

Very often it is more useful to log variables to console or file (using Log4J for example) instead of using breakpoints and manually step over. Especially when the execution goes through the interesting line multiple times debugging can be quite annoying.

When we are dealing with our code, it is pretty easy to add a log statement to the specific part. When we are dealing with 3rd party code we don’t have such options. In such a case we can achieve this using AOP (e.g. AspectJ or Spring AOP), however sometimes it is just too heavy solution when we need to do this one time only to find a small bug or so.

One day I came accross an interesting article 5 Tips for Debugging Java Code in Eclipse written by Abdullah Cetin CAVDAR on September 13, 2008. Fredrik Attebrant made an interesting comment that helped me to achieve what I all the time wanted:

“Trace points” can to some extent be done by entering code into the “condition” of a conditional breakpoint.

First do some printing, then make sure to return false if you want the execution to continue.

Example:
System.out.println(” foo has size: ” foo.getSize());
return false;

How to use adhoc logging in Eclipse

So here we go… we will use conditional breakpoints in Eclipse to enable us to log important stuff.

Create a breakpoint on the line you are interested in. Right click that breakpoint and select Breakpoint Properties…. Check “Enable Condition” checkbox and input your code into the box below. The breakpoint icon will show a question mark on that line meaning that it is a conditional breakpoint now.

Code sample

The trick here is to do the output and then return false so that execution is not suspended and you do not have to interact with the debugger as it is exactly what we want to avoid (see the option below the text box: Suspend when – condition is true).

Conditional Breakpoint Properties

To test it, we obviously have to run the program using Debug instead of Run because we need to enable breakpoints. The output should be similar to following:

Started
LOG - 0
LOG - 1
LOG - 2
LOG - 3
LOG - 4
LOG - 5
LOG - 6
LOG - 7
LOG - 8
LOG - 9
Finished

There is more to try…

And going further we can even implement conditions (hence conditional breakpoint), so that we would log only under specific circumstances.