How to edit text files from the command line
Although you can edit text files in cPanel (if your account includes cPanel), it is often quicker and easier to do so from the command line. This article describes how to use the Nano and Vim editors to edit text files from the command line.
The Nano editor is probably easier for beginners to use initially. On the other hand, the Vim editor is in more widespread use, and has a long list of features. Try both editors, and use whichever one you feel more comfortable with.
If this is your first time working in the command line environment, you may want to learn some basic Linux commands before you proceed with this article.Table of Contents
- Using the Nano editor
- Using the Vim editor
- More Information
Using the Nano editor
Editing files with the Nano text editor is easy. To open a file in Nano, type the following command at the command line:
Copynano filename
Replace filename with the name of the file that you want to edit.
If the specified file does not exist, Nano creates it.
To edit the file, just start typing your changes. To navigate around the file, you can use the arrow keys on your keyboard. If the file’s contents are too long to fit on the screen, you can press Ctrl+V to move forward a page, and Ctrl+Y to move back a page.
When you are ready to save your changes, press Ctrl+O, verify the filename, and then press ENTER. To exit Nano, press Ctrl+X.
You can access Nano’s online help at any time by pressing Ctrl-G.
Using the Vim editor
To open a file for editing in Vim, type the following command at the command line:
Copyvi filename
The vi command is a link to vim, so you might be wondering why you write vi instead of vim. The Vi editor was among the first text editors available for Unix computers. Vim, also known as “Vi Improved,” is a clone of Vi with a tonne of extra capabilities. If desired, you can also type vim to launch the Vim editor; however, vi is shorter and accomplishes the same task.
Vim’s file editing interface is not as user-friendly as Nano’s. Vim opens in normal mode, so you can not just start typing your edits. Nothing you type on the keyboard in regular mode modifies the text; instead, it interprets it as a possible command.
To make changes to the text, you must enter insert mode. To do this, type i. Note that the status line at the bottom of the screen changes to —INSERT—. You can now make changes to the file. To navigate around the file while you are in insert mode, use the arrow keys and Page Up/Page Down keys.
To return to normal mode, press ESC. Note that the —INSERT— status line at the bottom of the screen goes blank. Now you can type commands to save your changes, search for text, and so on.
To write your changes without exiting Vim, type :w and then press ENTER. To exit Vim, type :q and then press ENTER. To write your changes and exit Vim at the same time, type :wq and then press ENTER.
There is an excellent tutorial for Vim available from the command line. To start the tutorial, type vimtutor at the command line.