Creating and editing a file via SSH

Overview

There are a few different ways to create and edit a file when logged into a DreamHost server via SSH. This article explains how to use two 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 editing files in a terminal, view the following article:

This article only gives you an example of how to create and edit a file using SSH. When following these instructions, make sure to choose your actual file and edit it with whatever contents you need.

Using 'nano' to create and edit a file

nano is easier to use than vim so it's a good choice for newer users. Follow the steps below to create and edit a file using nano.

Creating or editing a file using 'nano'

  1. Log into your server via SSH.
  2. Navigate to the directory location you want to create the file, or edit an existing file.
  3. 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:
    nano 01.png
  4. Start typing your data into the file.
    nano 02.png
  5. When you're ready 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:
  6. Press the Enter key on your keyboard to save.
  7. When finished, 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 to edit files, but is also more difficult to learn. vim is only recommended for advanced users with prior experience working in the Linux shell.

Using 'vim' to create and edit a file

  1. Log into your server via SSH.
  2. Navigate to the directory location you wish to create the file, or edit an existing file.
  3. Type in vim followed by the name of the file. For example, if you wish to create (or edit) a new file named test.html, run the following:
    [server]$ vim test.html
    The vim editor may appear confusing at first because you cannot type into the file yet:
    vim 01.png
  4. Press the letter i on your keyboard to enter INSERT mode in vim.
    It now shows -- INSERT -- on the bottom left:
    vim 02.png
  5. Start typing into the file.
  6. 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.
  7. To save the file, type in a colon followed by wq. For example:
    :wq
    The characters :wq appear on the bottom left:
    vim 03.png
  8. 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, this changes a file named file1.txt to file2.txt.

[server]$ mv file1.txt file2.txt

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?