Editing text files from the command line
You can edit text files in your web hosting control panel, but it is often quicker and easier to do so from the command line. Learn how here!
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.
Note
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.
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:
nano filename
Replacefilename with the name of the file that you want to edit.
Note
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:
vi filename
Note
If you are wondering why you type vi instead of vim , the vi command is a link to vim. One of the earliest text editors on Unix systems was the Vi editor. Vim ("Vi Improved") is a Vi clone that has many more features than the original. You can type vim to start the Vim editor if you want, but vi is shorter and does the same thing.
Editing files with Vim is not as intuitive as with Nano. You can't just start typing your changes, because Vim starts innormal mode. In normal mode, anything you type on the keyboard is interpreted as a potential command, not changes to the text.
To make changes to the text, you must enterinsert 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.
Tip
There is an excellent tutorial for Vim available from the command line. To start the tutorial, type
vimtutor at the command line.
More Information
This article is a very basic introduction to using the Nano and Vim text editors. Both of these editors, and Vim in particular, have many more features and customizations available:
-
To view the online documentation for the Nano text editor, please visit http://www.nano-editor.org/docs.php.
-
To view the online documentation for the Vim text editor, please visit http://www.vim.org/docs.php.
Related Articles
Updated 3 days ago