Overview
This article explains how to create and edit a file via SSH using two popular text editors.
- nano — Best choice for beginners.
- vim — More features than nano, but also more difficult to use. vim is a good choice for advanced users.
If you're not comfortable with editing files in a terminal, you can instead create and edit files via FTP.
Using nano to create and edit a file
nano is easier to use than vim, so it's a good choice for newer users. The steps below show you how to create and edit a file using nano.
- Log in to your server via SSH.
- Navigate to the directory you want to create or edit a file.
- Type in nano followed by the name of the file. For example, if you wish to create (or edit) a new file name index.html, run the following:
[server]$ nano index.html
- A new file opens named index.html.
- Start typing your data into the file.
- To save the file, hold down the Ctrl key and press the letter O: (Ctrl + O).
- The bottom of the prompt asks you to confirm the name of the file, which is already set as index.html.
- Press the Enter key on your keyboard to save.
- Press Ctrl + X to close nano and return to your shell.
Using vim to create and edit a file
As stated above, vim includes more features that allow you to edit files but it is also more difficult to learn. vim is only recommended for advanced users with prior experience working in the Linux shell.
- Log in to your server via SSH.
- Navigate to the directory you want to create or edit a file.
- Type in vim followed by the name of the file. For example, if you wish to create (or edit) a new file named index.html, run the following:
[server]$ vim index.html
- The vim editor may appear confusing at first because you cannot type into the file yet.
- Press the letter i on your keyboard to enter INSERT mode in vim.
- The bottom left displays the word -- INSERT --. This confirms you have switched to editing mode.
- Start typing into the file.
- When finished editing the file, press the ESC key. This takes you out of INSERT mode and -- INSERT -- disappears from the bottom left of your terminal.
- To save, type in a colon (:) followed by wq. For example:
:wq
- The characters :wq appear on the bottom left.
- Press the Enter key on your keyboard to save.
Changing the name of a file
To change the name of a file, use the mv command. For example, to change the file name from file1.txt to file2.txt, enter the following:
[server]$ mv file1.txt file2.txt