Linux: Text Editing
-
Given that everything is configured in Linux by way of text files, it should be of little surprise that text editing is a major topic for Linux users. Text editing is something that you will do over and over again, probably every few minutes while working on Linux. So getting comfortable with working with text files is pretty important.
There are many tools that we can use for editing text on any system today. On Linux the most popular ones include vi, pico, emacs, joe and nano. The oldest is vi and it is extremely light and is included on effectively every UNIX, and therefore Linux, server ever deployed. No matter how old or new the server is that you will be working on, no matter what distribution, no matter how stripped down the installation may be we expect vi to be there. We also have the advantage that vi is available even if we work on a system that is not Linux but is FreeBSD, Dragonfly, NetBSD, AIX, HP-UX, Solaris, IRIX, TrueUNIX, Mac OSX, OpenBSD and more. You can even add vi to Windows, if you like. Because of this, it is common and recommended to learn vi and to often use it for all text editing tasks because when you need it, vi is always going to be there.
No one really likes vi. The syntax for using it is archaic and weird and there is really nothing else like it around. But vi integrates with many UNIX utilities and you need to know it well so using it all of the time is often the best way to accomplish this. Many UNIX jobs expect you to work in vi for everything from interview to production systems. So there is little avoiding it in a Linux or general UNIX career.
The second popular editor still being used today is nano which has essentially replaced joe and pico as the "easy" text editor of choice on Linux systems. Emacs is very difficult to use, much like vi, and has its own following but is heavier than vi and not nearly as often included on systems so lacks the reasoning for why vi is learned so often. If you want to explore other command line text editing options, nano is likely what you will want to explore. But once you have learned vi it really is not that bad and knowing it is very important and should not be skipped, not even at this stage.
Using vi is often scary at first. It gives no clues as to how to use itself and leaves you more or less stranded in your document until you figure things out.
The first thing that everyone needs with vi is a good command reference: https://commands.com/vi/commands
Learning a few basic commands are generally enough to get around in vi. Most importantly is learning the idea of there being two modes in vi, a normal mode and an insert mode. We start vi using a command like:
vi afileiwanttoedit
If the file exists, it will open it for editing. If the file does not exist it will create it when we save it so no need to worry about the file existing before running the command.
Once we get into vi, we are in the normal mode and cannot begin entering text yet. In order to start editing we need to enter insertion mode. We can do this most easily by entering the command "i".
Now we can enter text like you would expect. Just start typing.
To do anything useful, other than entering text, we need to exit insertion mode. We do this by pressing "Esc". From here we can move the cursor and other functions.
The quick shortcut to save and exit is hitting "ZZ". To exit without saving you hit ":q!".
There are many possible commands to learn, but they are best learned as needed, from a cheat sheet.
Recommended Exercises
Make some text files with vi. Edit them with vi. Make changes. Delete lines, text move around, etc. Learn to save quickly and easily as well as leaving without saving.
- Use the colon to move directly to a line number.
- Use the slash to search for text in the file.
- Use the :noset command to turn on and off seeing invisible characters and line numbers.
-
If I may butt in here and show off I've talked about the text editor war on my blog (and here) before:
http://tonyshowoff.com/articles/vi-vs-emacs-nope-theyre-both-terrible-and-obsolete/ (link to MangoLassi conversation in post)
In all seriousness, because the post is a little over the top, if you want to be a sysadmin with Linux, Unix, BSD, old SunOS machines from the stone ages, etc you really need to know how to use vi. I know how to use vi, I despise it, but it's necessary from time to time. Often there are easier, less terrible editors available, but not always, and sometimes other editors aren't capable of completing the task for whatever reason (vipw for example).
My personal recommendations are nano or pico or FreeBSD's formerly popular "ee", I use these pretty much all the time, except, again, vi does come up.