Editing Text On The Linux Command Line

Linux Command LineServer administrators will, at some point, have to edit text files on their server. Most Linux application configurations are handled via text files, and making configuration changes is done by changing the content of those files. The naive way to edit files on your server would be to FTP into the server, download the file to your local machine, edit it there, and then upload it to the server, overwriting the original. If you edit a file once in a blue moon, that’s just about justifiable, but if you regularly edit files, it pays to familiarize yourself with the command-line editing tools you’ll find on any Linux server.

If you know how to use command-line text editors in Linux, making configuration changes is simply a matter of SSHing into the server, launching an editor with the file, and making your changes.

If the idea of editing text in a command-line application doesn’t appeal to you, you’re not alone. However, some of the most sophisticated text editing tools ever devised come as standard or can be installed from the package manager.

Let’s have a look at two options: Vim and Nano — they’re both Linux text editors, but they’re very different in philosophy and capability.

2 Ways to Edit Files From The Linux Command line

The easiest way to open a text file is to navigate to the directory it lives in using the “cd” command, and then type the name of the editor (in lowercase) followed by the name of the file. Tab completion is your friend.

Text Editing With Vim

It’s important to know about Vim because you’ll find it installed on every Linux server you come into contact with, and because it doesn’t work in the same way as the text editors you’ve used before.

Vim-related frustrations are a major cause of cursing for novice server admins, so I’ll get the most glaring issue with Vim out of the way first. It’s a modal editor. That means Vim has several different modes in which the keys on your keyboard will do different things. Insert mode is used to enter text into a file (buffer really, but the difference doesn’t matter at the moment). Perplexingly for many, Vim doesn’t start in Insert mode, so attempting to type in Vim when you first open it won’t have the desired result. It may do nothing at all or it may pop up cryptic messages at the bottom of the screen.

Vim starts in Normal mode, in which key presses are interpreted as commands, including navigation, search, and editing commands. The command to enter Insert mode is triggered by pressing “i” while Vim is in Normal mode. Then you can enter text as you would normally.

To get out of Insert mode, press “escape,” which will return you to Normal mode.

To save a file you’ve made changes to, ensure you’re in Normal mode, and enter the following.

:w

When you want to quit Vim – which may be right after you’ve started it, enter “:q” while in Normal mode.

Vim is an extraordinarily powerful text editor, but that power is accompanied by a learning curve that’s almost vertical. If you want to understand why so many developers and system administrators love Vim, take a look at this article.

That said, if you never want to look at Vim again, Nano may be the Linux command line editor for you.

Command Line Edits With Nano

Nano is a simpler text editor that works more-or-less like the text editors you’re familiar with from Windows or OS X (which we have to call MacOS now, I suppose). When you start Nano, you’re free to enter text as you would in any other editor. Keys do what you would expect them to.

The only difficulty with using Nano might be caused by the keyboard shortcuts for saving and exiting the program. The shortcuts are helpfully displayed at the bottom of the screen when you start Nano, but you should be aware that the “^” symbol that precedes them stands for the “control” key on your keyboard.

To save a file, press:

Ctrl-o

To exit, hit:

Ctrl-x

That’s the bare minimum you need to edit and save text files on your Linux server. Both Nano and Vim reward deeper investigation if you want to become truly effective as a command line warrior, so I’d encourage you to take a look at their manual pages and the online documentation for each.

What About Emacs?

A note to emacs fans: I’m one of you. This article was written in Emacs (Spacemacs) with Evil Mode turned on. (Orgmode was my gateway). I chose to focus on Vim and Nano because I wanted to keep it short, and because both are found in the standard installation of many Linux distributions.

Matthew Davis is a technical writer and Linux geek for Future Hosting.

Dedicated Server Special

Take advantage of our Double RAM offer on the E3-1230v2 4 x 3.30GHz+HT server! Only $134.95 per month. Managed and Unmanaged options available at checkout.

GET STARTED