Tuesday, April 13, 2010

Eclipse - code formatting for Beginner!

First things first! Eclipse is a Open source IDE, an Integrated Development Environment for development using languages like C, C++, PHP (both dynamic and static) for different platforms like mobile, embedded, thick and thin servers!

Two Myths broken!
1. It doesn't come with the development kit unlike Visual studio!
2. It is not for just Java.

So, with the SDK pre-installed on your machine, run your copy of eclipse that you can download for free at the eclipse project website.

This post will hence discuss about the Eclipse for J2SE although most features are same in J2EE version too!

One most important issue most developers like me spend time on is not on the code-logic but the alignment of the code! :)

So here's what you hate when you write your code in Eclipse - The alignment of the braces!

class HelloWorld{
   public static void main(String[] args){
       System.out.println("Hello World");
   }
}

which we always like to see as,


class HelloWorld
{
   public static void main(String[] args)
   {
       System.out.println("Hello World");
   }
}


Eclipse says YES to you! Go to the preferences dialog (Cmd + , in the mac) and search for 'formatter' in the filter text-box or browse your way through Java > Code Style > Formatter.




There you have it! Click edit and you can change EVERY aspect of code formatting from indentation to how your comments appear in your code window!

You might be interested in setting your braces as shown below: (The changes your make are immediately reflected on the preview pane on the right. This comes real handy!)

Also for beginners, this might be useful!

Unlike Visual studio, the code that you type in Eclipse is not auto formatted when you type the code, letting the choice to be yours!

If you want a section formatted properly, after typing in a hurry, you can at anytime select just the section  and hit cmd+shift+f to format it or without a select, if you perform the same command, the entire code is formatted.

You can also correct indentations alone with cmd + i. You can find these under the source menu in Eclipse!

Sunday, April 11, 2010

Some handy vi args!

Say you compiled a program called test.cpp and the compiler spitted the error
"test.cpp:97: error: ‘itoa’ was not declared in this scope
at you!

Mostly, you do one of the following!
Open the file and 
a) Scroll through it!  (See line numbers with :set number  or   :set nu )
b) Jump to line 97 which you can achieve by 
i) 97G   (vi command itself)
ii) :97 enter  (ex editor command inside vi)

You can achieve this easily with the command line args itself (those that are supplied when you open vi. The file name itself is one such arg)!

vi +97 test.cpp will open file with cursor at the 97th line!

Now what if you are like me and are bad with numbers? Can't remember 97?

vi +/itoa test.cpp will perform a pattern matching and will open the file at the first match. You can then use n or N to skim through the matches until you land on your line! 


Bonus tip! :) 

Ever wished vi has alt+tab (on windows/linux) or cmd+tab (on mac) to switch between two files? It has it too! :D

Just open two or more files with all the names as args to the command!

vi file1 file2 file3 file4 file5

You will be editing file1 initially!
Check which file you are editing with ex command :args or :ar 
Vi will print
[file1] file2 file3 file4 file5 
showing the current file in brackets and the entire list that you supplied!

Move around the list with :n and :N. To jump to the first file on list use :rewind or :rew and :last for the last file!

Ooops! Now to issue of concern alt+tab! 
Once you have at least once switched between the 2 files that you need to edit alternatively, use :e # to switch between files!
or simply ^^ (Ctrl + caret)
What if you have find that you need to edit file1 and file7 alternatively but forgot to open file7??? Wait! Don't quit vi!
When you are editing file1 just call in file7 with :e file7 (e for edit) and there you have it! Now use ^^!  

As mentioned in an earlier post, you can use the clipboard to move text around the files!

Though the y (yank) and p(put/paste) commands are handy you can't easily do that for multiple lines!
use :line1,line2ya  c for copying/yanking multiple lines (where line1 = starting line number; line2 = ending line number; cn = a-z one letter clipboard name)

Switch to your file you want now without exiting vi and

then :pu c will put that clipboard c's content into the new file!
(A word of caution! The switching between files can happen only if the file you are editing is saved)

Now that's why vi is a Very Interactive editor!

Saturday, April 3, 2010

vi has a clipboard too!


The very interactive(vi) editor has all that your GUI based editor has! Actually, it has more! Adding to all the basics, here are some key-taps that can help u tap the power of VI.

When you hit 'd' based delete commands like 'dd', 'nd' ... or 'y' based yank commands all that's is deleted or yanked is stored in a buffer. (Ssh! Deleting with d is not delete, it's actually 'cut'. Try 'p'-ing to paste after 'd' :) )

So where does it actually get stored? It's not one but 2 buffers!

Deleted lines are stored in a numbered buffer, named from 1 to 9. (stack like. recent entry is 1, oldest being 9)
You can yank/delete into alphabetically named buffers too (a-z).

"(double quote) is used to access this a-z buffer.

" is the simple syntax

Eg: "c5dd will delete/cut 5 lines in to buffer named 'c'

(simply 5dd will not cut into the buffer)

Interesting part is "C3dd will append your now-deleted 3 lines to your previously present contents of buffer 'c'. (Note the upper case.) You can't do this even in MS word!!!

Paste the buffer using "cp     ( "c for accessing buffer and p for paste/put )


Same works for yanking too!

To access the numbered, deletion buffer, use "np where n is number 0-9 similar to a-z.

Ain't that something!!!!