Showing posts with label text editors. Show all posts
Showing posts with label text editors. Show all posts

Sunday, 30 March 2008

An easy introduction to Vim

If you're only looking to use Ubuntu as an alternative to Windows (which many people are, and that's fine), then the text editors we've already mentioned (gEdit, Kate, nano) are more than sufficient for your needs. They're more than adequate for things like editing configuration files and creating web pages using HTML.

However, if you're really into Linux and would like to learn more, it's a very good idea to take the time to learn at least one of the two most powerful editors available: Vim or Emacs. However, Vim comes installed on Ubuntu by default whereas Emacs doesn't, so it's a more logical choice to start with (but if you're interested, by all means try Emacs as well).


The version of Vim that comes preinstalled with Ubuntu isn't the full version, but a cut-down version called vim-tiny. To get the full version, you need to enter the following:
sudo apt-get install vim-full
The full version of Vim will be downloaded and installed for you.

Now, Vim may seem rather idiosyncratic at first, but once you begin to get used to it you'll start to see why it's so highly regarded. Unlike something like Notepad, Vim has several modes you have to switch between to carry out different tasks. However, you also don't need to lift your hands from the keyboard very often, making it faster to use.

Now, here's where you'd expect to see a Vim tutorial from me, right? Well, I'm not going to do that because there's a great tutorial already included with it! Just open the terminal and enter the following:
vimtutor
You'll have the opportunity to learn about Vim at your own pace. Now, it is quite a lot to take in (I haven't managed to complete it yet, and I've made several attempts!), but it's worthwhile. At first, it might seem like an uphill struggle, but persist with it and you'll soon get used to Vim's way of doing things. Once you've completed a few of the exercises, you'll never want to go back to nano again!

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!