Showing posts with label Emacs. Show all posts
Showing posts with label Emacs. Show all posts

Thursday, 4 August 2011

Hey Emacs! (an excursion on Emacs' tab-key)

One on main difficulties on Emacs is how it manages tab-key. A user whom comes from VI/VIM or another editor feels disoriented because Emacs ignores a tab and mantain stubbornly the selected line in the same place.
A bug? No. It's a feature. Hard to understand, but a feature. This is because there's three ways to manage a key-tab event:

  1. a way to insert a TAB (\t) character
  2. a way to insert 4 (or more) spaces
  3. a command to indent selected line(s)

VIM and other editors mean tab-key as 1 or 2. Emacs, instead, binds tab-key whith the "indent according to mode" command. This means a more sophisticated (and elegant) tool, but also means to understand it.

Modes

Emacs works with Modes. A C file will be interpreted with the c-mode; a Java file with java-mode, ecc. Every mode has its own configuration. E.g., a C file could be indented according to GNU style, BSD style, Kerningan and Ritchie style and others (a full list is avaiable on wikipedia).
But is an advantages? Well, yes, when you understand how a mode works. If it's properly configured, it helps programmer as no other editor. You can detect if you forget a parenthesis becaus Emacs will indent wrong a line. It's usefull.

But my colleagues uses VIM

If a VIM users sends you a C file, all new lines you'll add will be indented using current Emacs mode and style. It's boring, but there's a solution. You can change Emacs configuration just for a file writing on top of it the famous "Hey Emacs" line. If you write on top

/* Hey Emacs! -*- mode: java; c-basic-offset:4; indent-tabs-mode: t;default-tab-width:4 -*- */


Emacs will understand:
  • it's reading a Java file (enable Java mode)
  • indent lenght is 4 spaces (c-basic-offset)
  • tabs are 4 spaces long
  • when idents, Emacs must use a tab character (indent-tabs-mode:t)

This is just an example: there's many other variables you can set. I think adding this line it's a small price to pay. Emacs is really good.
And, if you want, there's always VIM ;)

Update
There's a way to insert brutally a tab character: pressing META+i. But I suggest you to use it carefully.

Tuesday, 19 April 2011

VIM vs EMACS - again


«Two snakes facing each other!»


Looking to my blog statistics, I realized visitors come here mostly because they're looking to "vim vs emacs". It's comprensible: this is one of most (in)famouse computer "wars".
In last months I used a lot VIM, because I had some problems with Aquamacs Emacs. So, I decided to write another post to these two max-weight editors, maybe helping you to choose.

EMACS

I became an Emacs fan since 1999, when I first heard about "a free, open source, configurable, programmable editor". I gave Emacs many tries, just because it was Emacs. But, let's admit it, it hasn't a quiete learning curve. Emacs has more options and controls than an SR-71 and has it's own default settings. This is the "beauty" of Emacs: to discover that a simple function (one-tab if I type tab key, e.g.) needs to be configurated.

VIM

Usually, everybody tries VIM after some frustration with Emacs and find a bit confortable with it. Editing file in a modal program is a bit weird, but become quite fast. You can have problems with some "complex" tasks, such as search-and-replace. To do this in VIM you have to type

:s^$/text to find/text to replace/gc

where, last 'c' stands for "confirm".

Conclusion

When I have to write an iPhone App, I must use XCode. When I'm working on a win32 platform, Notepad++ is the choice. To develop a Java application, I prefer Netbeans (and some friends of mine use it also for PHP development). I admit Emacs or VIM are fundamental to a programmer, but to use them for huge programs could be a delirium. So, as general line-guide I say:

  1. VIM or Emacs are good to write small/medium scripts
  2. For large applications, I'll use a SDK (Netbeans? Eclipse? XCode?)
  3. Emacs with cua-mode is easier than VIM, but you could have troubles with other tasks
  4. Do not be ashamed to use a simpler editor as gEdit, Kate, Bluefish or JEdit. Use what you prefear. A text-editor is just a tool. Real Programmers write programs

And happy programming! :)

Wednesday, 2 March 2011

The endless VIM vs EMACS debate

Many programmers prefer using VIM instead of EMACS. They claims it's better because it's faster, smaller and easier to configure.
What to say about?
In these days I wrote a small iPhone framework in python. It requires a TAB-separated-Table as input. I used Aquamacs to write this input file and my program crashed. Why? Aquamans inserted some tab characters, I don't know why.
I rewrite that input with VIM and all worked well.
Another "life case": I tried to install lua-mode on a Windows 2000 workstation and on my MacBook Pro. I failed in both cases. I think after two tries, a system is not enough user friendly, so i give up.
Confused? It's normal. VIM has a easier learning curve. It's boring to write tons of code, but it could be more usefull than EMACS. EMACS is good if you accept an "evolutionary model", where you write day-by-day your .emacs file.

And if you are tired, there's always JEdit :)

Wednesday, 3 November 2010

Why EMACS is still important

«Hey you! Is that a VIM session?!»

Talking about EMACS in 2010 may seems stupid. But GNU's official text editor still maintains its charm, despite nearly three decades. Born from Richard M. Stallman's keyboard, EMACS is the editor of choice of different programmers scattered around the world, thanks to its main feature (it's programmable). I decided to try EMACS after a fight of years.
If you use EMACS enable immediatly the cua-mode The cua-mode sets the "copy-and-paste" and other controls with the keyboard shortcuts control-v, control-x, z-control, control-c, making more "human" the user interface and avoiding you to spend two hours on manual to understand what is a "region", the "yanc" command and the "kill" command. Using this "trick", I finally started using EMACS for real and not just for fun.

EMACS vs XCode vs Netbeans vs Eclipse vs...
EMACS can compete against an IDE?" No. It can't, at least not right now. It 's true EMACS is a text editor with a LISP engine then theoretically you can turn it into a IDE, but I've never seen anything like it. And I'm not interested to see it. The "universal solution" (or "silver bullet") does not exist. If I'm working a small project in C / C, a bash script or a python script, then I'll probably use EMACS; but to develop an iPhone App, a java servlet or a 3D engine I'll use a IDE to do object's methods analysis, to compile the project with ANT, to upload it to SVN, etc..

Why should I use EMACS?
Just for fun. To learn LISP. But the main reason is because EMACS is on any operating system. Well, if you're writing a shell script on a SSH session, then VIM is enough. But when you need to write some Python scripts, with C code, then EMACS is the choice. It's everywhere, has a huge library of macros, will work on Mac OS X, Windows and GNU / Linux and ALL EMACS macros (including "search and replace in selection "," Indent Code","open a shell","Open SSH session to server","Send a email to", ecc.) work on ALL systems which run EMACS . If you are a programmer, probably you will find difficult to use the C mode. Well, you have to configure the c-major-mode to work as you want. At the end of this post, I'll show my personal .emacs file.

EMACS isn't good as VIM
If you talk just about personal preferences, I don't say anything: everyone has his preferences. I was a VIM user, but I finally passed to EMACS after some days of XCode. Why? XCode indents wrong my Objective-C code if I forget some parenthesis (using Objective-C it happens very often). I felt the lack of this advanced feature and I realized that Emacs can do the same, changed my feelings on it.
There's some things I would like to see in next EMACS release: first, set the default use of cua-mode, because in 2010 just two or three nerds are comfortable with "kill and yank". Second, set UTF-8 as default encoding. Third, a modern configuration interface (the "Customization Macro" was useful in the '80, but now is a bit obscure).

My .emacs file
Somebody has a very large .emacs file, filled with his own macros. I just want to use the c-style called BSD, with tab character as indentation method and four spaces as tab width.

;C programming style
(setq c-default-style "bsd"
         c-basic-offset 4)
;for c-mode, use tabs instead of spaces.
;Set tab width as 4 spaces
(setq-default c-basic-offset 4
                  tab-width 4
                  indent-tabs-mode t)

Last advice
Take a look at the Emacs Wiki: I found it very useful.
Happy hacking :)