Welcome to the fifth part of this series of blog posts to introduce you to the wonderful world of SSH.
In this post we are going to be learning about a terminal text editor. I hear you ask “Well, what’s a terminal text editor?”, let’s start with that.
The window you are using to SSH to your vCluster (whether it be PuTTY for Windows, or Terminal.app for Mac) is known as a Terminal. This terminal can run a command to make it behave like a text editor, much like Notepad or TextEdit would be used.
An advantage of this is being able to quickly edit files that are already on your vCluster but need small changes. Without using a terminal text editor, you would have to download the file using FTP, then open it up in Notepad, change it, save, and re-upload it to your vCluster… That’s a very lengthy process for making small changes like adding a missing semicolon. This process can be shortened down to running a single command, editing then saving. Done. It has the potential to save you hours!
Having said that, it’s not all plain sailing. Using a terminal editor is a steep learning curve (depending on which editor you use), and its quite different to a normal text editor. But I think that the time saving benefits outweigh the slight awkwardness.
There are a couple of editors installed on the SSH server, but we will be using one called: vim.
First you need to connect to your vCluster, you know how to do this by now. Check out “SSH-ing to your vCluster” in case you get stuck. Then using previously learned skills of changing directories change to the directory that has the file you want to edit. If you’re not sure how to navigate directories have a look at “SSH Part 3“. Now we type:
The bit you need to change is obviously the file_name. And don’t forget the extension, like .php or .html. So for example, you could type:
and this will load up the vim editor with your index.php file loaded.
Using the Editor
Not too hard to open a file, heh? Now it gets a little tricky. When you first open this editor, it doesn’t just let you edit the file as if it were a Word Document, but it opens in ‘navigation’ mode. This mode allows you to navigate around the editor using the arrow keys, but more importantly it allows us to enter commands. For now, we don’t need to know the commands, but I will go over a couple a little bit later. First, we will go into ‘insert’ mode and edit our file.
To get into ‘insert’ mode, you just need to press the I key, that’s ‘Shift + I’. You should now notice the helpful prompt at the bottom to remind you that you are in insert mode.
Now, you can move around using the arrow keys, but you can also now type as if it were a regular Word Document or Notepad file. Its worth noting now, that you cannot use the mouse to do anything. Everything has to be done with the keyboard. You may think of this as a hindrance at the moment, but you will soon realise the potential it has when you don’t need to lift you hands from the keyboard, just be patient with it.
Right, now you have edited your file, and you want to save. Where is the floppy disk icon that we are all so accustomed to? As I have said, you cannot use the mouse for anything, so by process of elimination, you can assume we have to use the keyboard. This is where commands come in.
Commands are what drives this editor. To enter a command, you need to be in the ‘navigation’ mode, which can be done by pressing Escape (the ‘esc’ key). Now you should type a colon (:). You will notice that a colon appears at the bottom of the Terminal, and you can now type a command.
The first command to use is the save command. This is just the letter ‘w’, short for write. If you type a ‘w’ just after the colon, and press enter then the file will be saved! That was easy, wasn’t it.
Now you have saved, you can either go back into insert mode and do some more editing, or you can exit the editor. To exit, we use the command ‘q’, short for quit. Exactly the same as doing the save command, just type a colon then ‘q’ and it will quit.
However, if you have edited a file but want to quit without saving, then it has a go at you for ‘No write since last change’.
It gives you a hint as what to do, but it’s simple. Instead of just typing ‘q’ after the colon, add an exclamation mark (!) after it: ‘q!’. Then it quits without saving.
Using the write command, then the quit command one after another is a bit cumbersome, I want to only do one command to write and quit! That’s entirely possible, and simple. Guess what, you just combine the two, ‘wq’. Easy isn’t it?
One final tip, if you wish to create a new file then instead of typing
you replace the file_name with the name of the file you wish to create. Although it won’t actually create a file until you do the write command. So if you change your mind and quit without writing, no file will be created.
So, lets recap.
- Open vim with: vim file_name.ext
- Enter insert mode by pressing: i
- Return to navigation mode by pressing: escape
- Type a command by pressing: colon (:)
- Save command: w
- Quit command: q
- Save and quit in one go: wq
- Quit without saving: q!
Any questions? Leave a comment and I will clarify anything for you.