Monday, 28 January 2008

Text editors

If there is one thing Linux has in spades, probably even more than desktops, it's text editors. I just entered apt-cache search "text editor" and was greeted with a staggeringly long list. Now, not all of these were text editors, some just had the phrase "text editor" in the description, but there's still a hell of a lot of them.
There's a good reason for this. Linux uses text-based files for configuration purposes. For instance, the software repositories you are subscribed to are stored in a text file. Although you can add new ones using a graphical application, it's often easier to use a text editor as you often need to do little more than add a line to the file, and separately import the signing key. Adept or Synaptic can take a while to load, whereas a text editor doesn't.
Text editors can be divided into two types:
  • Command line text editors work within the command line, and are usually started by entering their name.
  • Graphical text editors open in their own window, just like any other graphical application, and can normally be started from the menu.
Probably the two best-known text editors within Linux are vi and Emacs. Both are extremely complex and powerful applications which are very useful once you have gotten to grips with them, but have something of a learning curve to them.

vi is traditionally a command-line editor, although now it provides various GUI's (graphical user interfaces). It's small and fast, but very powerful. The commands take some getting used to, though, as they are radically different to their equivalents in similar applications in Windows (such as Notepad). Nowadays, the original vi is rarely used as it has been mostly supplanted by newer versions, the best-known of which is Vim, but these are often still referred to as vi.

Emacs takes longer to load, but is more powerful and customisable than vi. It's virtually an operating system in its own right - you can apparently use it to browse the web, read e-mail, even play Pong or Tetris!

But don't worry - you don't need to start using one of these text editors yet. Ubuntu comes with vim-tiny, which is a cut-down version of Vim, as well as gEdit and nano. Kubuntu has Kate instead of gEdit, but is otherwise the same.

gEdit and Kate are both graphical applications, and will be easy to get to grips with if you're already used to applications like Notepad. I find Kate in particular is a great application for programming, better than Notepad by a long shot. When you're writing code in many languages it will adjust the colour of the text to help make it more readable (I know it can do this with HTML, CSS and JavaScript, but it didn't seem to do so with Java).

However, I find that for editing configuration files it's best to use a command-line text editor as if you're making changes to the system you'll need to use sudo. Nano is great for this, as it's a nice straightforward text editor. To launch it, you just need to give the name and specify the path to the document (and include sudo if necessary). For example, to edit your /etc/apt/sources.list, enter the following:
sudo nano /etc/apt/sources.list
And nano will open up the document with administrative access. Once you've finished editing it, you can press Ctrl-O to Write Out (ie to save it). Or you can just press Ctrl-X to exit and if you have unsaved changes you'll be prompted to save them. It's pretty self-explanatory, but you can press Ctrl-G to get help.

You could use Kate or gEdit to do this by putting sudo kate instead of sudo nano, but they will take longer to load, whereas nano will get started almost immediately. It's therefore ideal for editing configuration files.

You'll no doubt be editing many text files as you begin to get more confident with Ubuntu, so take the time to become familiar with at least nano and Kate/gEdit. As long as you can use one command-line editor and one graphical editor, that should be sufficient. But if you find yourself using text editors a lot, more powerful options such as Vim and Emacs will become more worthwhile.

In future posts I will generally be using nano when a text editor is called for to edit a configuration file, but don't feel you have to use this. If you prefer something else, that's great, after all, they all do the same thing!

No comments: