Lesson 4: VI Editor in Unix

ByDr.-Ing. Erik Neitzel
Lesson 4: VI Editor in Unix

This lesson is meant to give you an alternative to the WordPress Theme Editor and plugins able to manipulate script file contents. The reason for this is simple: if you make a mistake using an editor that is part of the content management system itself, you will have shut yourself out. The only ways to save your installation are to restore your webspace from a backup or to re-edit your files using the operating system’s file editors of your web server.

In case you have a Unix based system running on the server, the odds are great that you can use VI as a remote editor. That way you do not have to transfer any locally created code files to the server. This brief article shows some basic step-by-step command sequences for you to get started with both Unix environments and VI.

Get connected to the web server using either Putty (Windows) or Terminal (OSX) and the SSH access parameters you either received from your hosting provider, your admin or yourself 🙂

Unix file handling commands

First of all you need to know how to create folders, since you probably want to organize the files you are about to create. If you wanted to create a folder within just the folder you’re currently in, use: mkdir folder-name

If you want to remove a directory you previously created, use rmdir folder-name to do so.

After creating a folder you can then switch to that folder using the change directory command “cd”: cd folder-name

You always get the full path of your current directories using the command pwd.

To create a file in the current directory use: touch file-name.php

Sometimes you need to copy existing files to another location. You can do this using the “cp” command: cp ./existing-file.php ./new-file.php, whereas “./” stands for the current folder and can also consist of several subfolders like ./sub1/sub2/sub3/file.php. Be aware, however, that those subfolders have to exist, they will not be created automatically. Use the commands mentioned above to do so.

If you simply want to move a file, use “mv” instead of cp. That way you can also rename files and folders: mv current-name-or-location new-name-or-location

To remove a created or copied file use the “rm” command: rm existing-file.php

If you lost track on the files within the current directory, use the list command like this: ls -la, which shows you details to all files within that folder.

VI file editing commands

Once you created a file within the directory you want, that file will be blank. You may want to fill it with fancy code, probably 🙂

Please go to the directory you created your file in using cd directory and ls -la to be sure that the file you seek is present and touch file.php if it is not.

Then type: vi file.php in order to open that file in an editor called VI.

The VI editor will show an empty editing space, since your file is still empty. Now please type letter [i] on your keyboard, which results in an “insert” label on the bottom left pane of VI. You can then type code, e.g. <?php echo “Hello world”; ?>

As soon as you’re done coding, please hit [ESC] followed by “:w” which results in “123 bytes written” on the bottom pane.

The Escape key gets VI in the command mode where you can use

  • :w to write (save)
  • :u to undo
  • :q to quit VI

Do not forget to type [i] again if you want to continue coding (assuming that you did not leave VI, if you did, just point Unix to VI file.php again).

I hope this brief introduction into Unix file handling and VI editing helps you get started.

The next lessons:

Dr.-Ing. Erik Neitzel