Command Line Text Editors

The Breakdown

[jdhore]: Depending who you are, how you work and what OS (and what generation) you’re coming from, you will either love or hate Command Line Text Editors. I (jdhore) personally love them because even if your GUI breaks in *nix or you’re SSHing into a remote server, you can always have the same tool at your fingertips and in my opinion, when you’re editing text, you should never be taking your hands off the keyboard. Sure, many GUI Text Editors have keyboard shortcuts, but they’re not always sane or complete like in a text editor that was soley designed around keyboard-only operation.

[cmack]: All very good points, and let’s face it: even if you’re a GUI-loving Linux/Unix user, you’ve undoubtedly found yourself at the console quite a number of times anyway, editing some config file so some aspect of your GUI can function properly.  At their very hearts, *nix-based OSes are driven almost entirely by apps and tools that rely heavily on text-based configuration files, which is part of what makes them so attractive to those inspired to write code for a hobby or living in the first place.  You’re definitely going to have need for a text editor at some point, and like just about every other aspect of *nix, there are a myriad different ways to address that need.  Here are a couple of the big hitters:

THE APP

Nano

jdhore –

First things first I suppose, i’m going to preface this “review” with the fact that I don’t use nano anymore. I’ll be talking about what I use now in the second app in this category.

As linux users, we all need to edit text files, be it source code, configuration files, quick todo’s that are a waste to open up a full word processor for and so on. Most new linux users have probably heard about the editor wars of Vi/Vim versus Emacs, but both of these type of editors are quite difficult to use if you’re new to linux (“C-x C-c? What does that mean?” “I’M TYPING IN VI AND NOTHING’S HAPPENING!” “:exit, :quit, :goaway, Ctrl+Q…oh, :q, dammit”).

This is where nano comes in. Sure it doesn’t have the massive feature-set of Vim or Emacs, but its simple, it has just enough features to get you through the day such as syntax hilighting and line wrapping and find & replace and jump-to-line.

Nano also shows a bit of help by default as soon as you open the editor. This help doesn’t mention every nano feature and how to use it, but it tells you about the basics of how to write files, read files, exit, search and a bit more.

The reason i have stopped using nano and have switched to vim myself is that while nano is a great simple editor, it hits a wall that…When you need to do some hardcore editing, it falls down because it just doesn’t have the featureset. It is a great text editor however and it is the default command-line text editor in almost every mainstream linux distribution (Debian, Ubuntu, Archlinux, Gentoo and others) so you can usually expect to find it on your system.

cmack -

Nano is my personal favorite for its sheer simplicity and ease of use.  I don’t lurk at the command line level any more than I have to, due to the fact that curiosity inevitably leads me to making changes to files that should not be done.  Why?  To see why that file header reads ** DO NOT CHANGE THIS **, of course! :D

Nano makes it easy to flip around through a text file with the same basic navigation that many users have come to expect of such a thing.  When you hit an arrow key, you expect your cursor to move in that direction.  Hit a letter, and you expect a letter to be inserted at the cursor.  Dead-simple.  Other features like searching and file operations are denoted in Nano’s menu at the bottom of the “window”, helping the user to keep from getting too lost in the changes they’ve made, or pointing the way to the door when you’re done.

While Nano does technically support syntax highlighting, there are some common sets missing that some might find to be a bit of a downer.  Rest assured, it’s not all that difficult to add more!  This site will show you how to add support for PHP, CSS and XML.

It’s the default CLi text editor in most *nix installs these days for very good reason: it works, works well, and doesn’t complicate the process with features and capabilities you probably don’t need in the first place.  It’s as simple and reliable as Windows’ venerable Notepad, and that’s why Nano absolutely takes the top slot in its field, in my README.

Another App

Vim

jdhore -

Ah, Vim. I could probably go on for hours with how amazing Vim is, but I’ll try to keep it reasonably concise. For starters, I’m not going to go into why Vim is or is not better than Emacs. I personally prefer Vim so that’s what I use, if you prefer Emacs, great. I don’t want any flamewars here, kthxbye.

Vim comes from the “classic” editor Vi. I call Vi classic because it was written over 40 years ago now (in 1970) and was one of the first editors on unix-based systems. One of the upsides, therefore, of learning Vim and, by association, Vi is that you will basically have the same editor everywhere. As i mentioned above, nano is the default on most mainstream distros, but all these distros have at least Vi as well. Vi is also on just about every environment imaginable. I have Vi on my smartphone, Vi on my router, Vi in the shell when i totally break my linux system, Vi on Solaris at work and on every old-school unix-like system available. No matter what system you’re on, you probably have some sort of Vi or Vim installed.

Now, I explained what Vi is, now for Vim. Vim is sort of a fork of Vi. It doesn’t use any of the actual Vi code since for a long time, Vi was closed-source, but it takes many of the Vi ways of doing things. The biggest advantages to Vim over Vi is that Vim is absolutely bursting with features, Vi, not so much. Vi has about the same (bit less) featureset as nano. Vim adds so much more like syntax highlighting (for over 500 different types of files!), scripting, multiple windows, multiple tabs, code completion, a great help system, seamless editing over network protocols, editing inside of archives, mouse interaction, multiple-level undo and so much more.

Vi and Vim are both what are called “modal editors”. Normally you are in command mode where you can enter commands to manipulate files, text in open files, windows, move through files efficiently and much more. There is also insert mode which is the mode you’re in when you just want to type text and have it show up in the file. In Vim, there’s also Visual Mode that allows you to select blocks of text and act on it in really really cool ways. This is a bit difficult to get used to, but once you’ve been using Vim for a month or two, it kind of becomes like second nature.

Just to show the power of vim, i’m going to give an example of something I used Vim for earlier this weekend (as of this writing) that nano-like editors couldn’t have even hoped to do. I was working on a project and I was replacing lines containing a SVN ID that hadn’t been used in 3 years with something called a PACKAGE_STRING in about 250 files. Here’s what I was able to do in Vim:

1. vim **/*.c – recursively open every C file in this project with Vim.
2. :bufdo %s/\”\$Id.*/PACKAGE_STRING\,/gc – On every open file, replace the entire line containing a SVN ID with PACKAGE_STRING, and ask me for every one (I did that just to prevent any possible unforseen screw-ups).
3. :wall – write all changes of all files to disk.
4. :q – Quit Vim.

So in under 5 minutes I was able to replace SVN ID’s in over 250 files with Vim. I can’t imagine how long this would’ve taken me in nano or some similar editor. Probably hours.

Now, to be completely clear here, Vim is not easy to learn and you will never learn all there is to know about Vim.  Heck, I was watching a talk by the single Vim developer and even he doesn’t know all the features in Vim. You also can’t learn Vim in the typical way, in my opinion, if you’re used to a simple editor like Nano or Notepad or Gedit because all of Vim’s great features are sort of hidden away. The way I think you have to learn is to be shown the basics and the *REALLY* cool stuff Vim can do by someone, and then learn as you go from there. It is well worth it and the time you spend learning how to efficiently use Vim.  It’ll hand back to you probably at least ten-fold when you’re using it.

Vim has many, many additional and extremely cool features i didn’t mention here as well, but if I mentioned all the cool stuff in Vim, this review would go on for a lot longer than it has so far.

In my personal opinion Vim is amazing and better than pretty much every text editor on the planet…If you take the time to learn it.

Oh, I should also note that there are some pretty good screencasts on Vimeo (hehe, VIMeo) that are good for new and intermediate Vim users alike since…I can’t really teach all of you vim. Here’s the link: Vim Screencasts

cmack -

Vi/Vim are the kind of editors that really mess with my day, but it’s certainly not for lack of ability or usefulness.  Not by a long shot.  I’ve seen what Vim in particular can go, and it is indeed an amazing thing to behold.  Imagine a text editor that you can literally program on-the-fly (as jdhore demonstrates above), to perform incredibly-complex operations on not just a single open file, but across many many files stored in different locations across your hard drives, including accessible drives on other systems!  Now that is one hell of a powerhouse of an editor, right there!  The find/replace functionality jdhore demonstrated is but one of many possibilities.  Vim has become far more than a simple text editor, and indeed, could almost be considered a coding environment in its own right.  So much so, in fact, that we had a difficult time trying to lock it down into a single category!

Between its massively-flexible syntax highlighting supporting hundreds of code languages, its ready acceptance of regular-expressions, and its ability to influence files that aren’t technically even opened in the currently-running instance of the app, an experienced user could very easily write, debug and maintain an entire website or application using nothing but Vim.

For all of its power though, there is a major caveat on which jdhore touched: it’s a modal editor, which means that by default your key inputs don’t directly affect the contents of the file you’re currently viewing.  Instead, you’re plopped into sort of a command line within a command line.  Vim is standing by for the user to input one of the many many keyboard commands available.  The problem here is that there is no clue given to the new user whatsoever about what’s about to happen when they assume they can just start typing away into the file they’ve opened.  This is a mistake I have made oh so many times myself, getting myself so entangled within the commands I’ve unwittingly initiated by trying to modify text, that I’m forced to kill the app and start again from-scratch.
There’s another problem too.  Its official documentation is enough to get you started, but nowhere near enough to help you come to terms with its true capabilities.  There are, however, tons of websites that offer a great deal of help with this app.  Again, see jdhore’s entry above for a prime example.

Vim absolutely shines as an incredible example of what can happen when an app is placed in the hands of the developers who use it most.  Unfortunately, that’s part of why it isn’t THE app for us in this category.  Don’t get me wrong: Vim works well as a text editor, but it steps so far outside simple text editing that it’s a bit like dropping a nuke on an anthill.  It’s truly powerful, but -in this context, at least- truly overkill.

, , ,

No Comments