Knowledgebase

How to view and edit files via SSH

There are different commands/tools for viewing the contents and editing files through SSH: cat, less, vi, vim, pico, nano and others. One of the most widely used and complex command-line text editors is Vim. It's installed on all HostKnox servers. To edit a file with vim, browse to the directory of the file and execute a command like this:

vim example.txt

This will open the file example.txt that's in the current working directory. In the command just replace the name of the file with the one that you want to edit or view (don't forget the file extension). For beginners Vim is more difficult to use. It offers a great range of options and possibilities, but you have to spend some time at least to learn the more basic things. There's a tutorial that you can open and use from your hosting account via SSH. For more information check out the article on how to use the Vim tutorial via SSH.

In this article however we'll concentrate on another simpler solution which you can use right away without having to spend much time to learn about it. This can be done with the commad-line text editors nano and pico. They are installed on HostKnox servers. Although they are different text editors they look and are used in the same way. That's why the two terms are often used interchangeably. They are text editors that are very easy to use, and that you can use to view and change the contents of the files on your hosting account. To start it, after you connect to your account via SSH use the command nano or pico followed by the name of the file (including the file extension). For example:

nano example.txt

or

pico example.txt

will open the file example.txt that's in the current working directory. Once inside the file you can use the arrow keys on your keyboard to navigate through the file. The up and down keys are for changing the lines and the left and right for moving the cursor within a line. If a line is longer than the screen, it ends with the symbol $. To see the rest of the line just move the cursor over the symbol. You can delete content with the delete and backspace keys on your keyboard. To insert content just move the cursor to the desired spot within the file and type whatever you want. You don't have to execute any additional commands to do that.

At the bottom of the screen you'll see the keys that you can use to perform different actions. They all involve the Ctrl key on your keyboard plus another one. They are listed in the following way: ^W, ^K, ^U. The symbol ^ stands for the Ctrl key. This means that to perform the particular action you have to press and hold the Ctrl key and then press the other key. Each combination is followed by a short description of what it does. For example: ^W Where Is; this means that you can use Ctrl plus W to search for content within the file. Just press and hold the Ctrl key and then the W key; a search bar will appear in which you have to type the word(s) that you want to find. After you type the content in the search bar press Enter on your keyboard. If you do it again right after the first search (Ctrl plus W), you'll search for another occurrence of the same word(s). Other key combinations are Ctrl plus K for cutting text and Ctrl plus U for pasting that text at the spot where the cursor is; Ctrl plus O to overwrite the file and save the changes that you've made so far.

After you exit the editor (Ctrl plus X), if you made any changes you'll be asked whether you want to save them, and whether you want to save the file as the same file, or with a different name.

If you want just to view a file and be certain that you don't make any changes by mistake, you can use the -v option. For example:

nano -v example.txt

or

pico -v example.txt

will open the file example.txt that's in the current working directory in view-only mode.

Was this answer helpful?

 Print this Article

Also Read