There is a constant need to read, edit and edit text files on any VPS, mainly after installing new packages. Learning, practicing and acquiring skills with command line text editors can mean a world of difference in your experience and results with VPS.
If your server encounters a problem, the only resource will be access to the system console and the only way to use it will be via a text editor. Vi or his descendant, Vim, are the only ones to have the guarantee to exist everywhere. In this tutorial, we will study nano and Vim, two popular text editors that you can run from a VPS terminal.
A problem for most newcomers in VPS is that vi, the only text editor present by default on all Linux distributions, is neither WYSISWYG nor intuitive. If you expect it to obey the commands of Windows Notepad or MacOS TextEdit editors, you will be frustrated and disappointed in no time. Let's say you follow a script to install a widely used program, says Nginx, and that it contains a command such as
sudo vim /etc/nginx/nginx.conf
If this is your first contact with viyou will discover that
A) you can not type in the editor (unless you accidentally tap I on the keyboard) and that
B) you do not know how to save the file and leave the editor (until you know that : wq will do the trick).
Over time, an improvement in vi called vim, has become very popular with users with power. It's a super-tight set of vi but with dozens of additional features, such as protocols, plugins, task automation, working with multiple files at once, using its VimScript internal language, or more formal Python, Ruby, Perl, or Tcl protocols, and so on.
Vim is hard to learn completely, but it is worth it.
The problems that newcomers have with Vi / Vim are why another publisher, nano, is so popular. It will display the commands available as a menu on the screen and, when you start typing, you will see text fill up. It is therefore not surprising that many users do their first tasks after installing a "droplet", an "instance" or a "server" on. their VPS boxes, will install nano.
What we are going to cover
For nano and Vi / Vim, we will show how:
- Install the editor on Debian / Ubuntu and Centos
- Explain the philosophical approach to publishing
- Get help
- Create a new file or open an existing file
- View important orders in the editor
- Save the file and exit
- Debian / Ubuntu or Centos systems
- SSH capability on the server
- Since Vim is a superset of Vi, we will focus on Vim only.
Installing Publishers on Ubuntu 16.04 and Debian 9
Vi should be present on all servers, however, on some, it can actually envy Vim. We show commands for installation on all systems because there may be differences between the hosting providers and the server versions provided.
First update and upgrade of the current version of Ubuntu / Debian:
sudo apt update sudo apt upgrade
Depending on the version of Debian, the sudo This command may not be recognized immediately after the server installation. If this is the case, simply omit sudo some orders.
On Ubuntu only, if you get the following prompt:
just press the Tab key on the keyboard, then press Enter.
You can then install the updates, if any:
sudo apt dist-upgrade
Then install Nano:
sudo apt install nano
We see that it is already installed on Ubuntu 16.04:
Now, let's install Vim:
sudo apt install vim
It is also preinstalled.
Publisher installation on Centos 7
sudo yum update sudo yum install nano sudo yum installs vim
How to start the editor
To start an editor, you invoke it by name at the command prompt. If you add a file name, two things can happen. If there is no file at this address, the editor creates an empty file and, if it exists, it is opened in the editor.
When to use Nano
This is the ideal case for using nano: you are (relatively) new to VPS servers, but you have just acquired one to be able to run a specific program on it, or a few sites. So, you search and find a good recipe to install the software of your choice, you follow and you reach a time when a configuration file needs to be changed. Nano is an ideal editor for such small, quick, dirty jobs, in which you add a line or two, for example, change the site name for Nginx, etc., and then save and close the file.
Now move to a neutral folder such as / tmp and open a file called sometext.txt in:
cd / tmp nano sometext.txt
We can start typing the new text immediately or paste a text that we already have on the clipboard. If it is a Windows terminal window (such as Kitty or Putty), you will paste text with the right mouse click and if it is under MacOS, a simple Cmd- V should suffice.
I've copied the paragraph above and here's what I've got:
The text is all in but you only see its end. By default, Nano does not package text because it is the expected behavior when you edit configuration files. To switch to wrapped text, press Esc, release it, then press $:
It was a flexible packaging, the rigid packaging is activated or deactivated with Esc + The.
The usual way to issue nano commands is to use the Ctrl key plus a letter, indicated by ^ G – meaning to press Ctrl then the key close g while holding Ctrl. In menus, nano uses capital letters g so it looks like you have to press the shift key and then g but in reality, you do not have to press Shift at all.
Instead of Esc followed by a touch, you can also use the key Alt by simultaneously pressing a key. If difficult, we should press Alt, holding it, then pressing shift, holding it, then pressing l. In general, Alt and Esc are called meta keys and – depending on your keyboard – this role can be assigned to another key.
Help screen in nano
Ctrl-G will show you the main text of the nano help:
Scroll with combinations of ^ Y and ^ V or, if you are using a normal PC / Mac keyboard, simply use PageUp and Footer key. You can also scroll with the cursor keys, from top to bottom.
We will now study a little more keyboard combinations:
Commands starting with M, for example, M- (, call to press the Meta key which, as we have already seen, can be Esc, Alt or something else. So you can go to the beginning of the paragraph by pressing Esc, release the pressure, then press shift and 9 at the same time. Or you can press Alt, press and hold, press shift, hold down both keys and press 9.
to push on Ctrl-X leave the screen help.
Save a file with nano
To save a file with nano, use Ctrl-O. The menu at the bottom becomes:
To save the file with changes, simply press Enter.
Pressing Ctrl-X leave nano if the file is saved; otherwise, you will be asked to confirm Y.
In the main menu, we see that Ctrl-W find the text, _Ctrl-_ is for the replacement operation and so on.
Learn Vim with Vimtutor
The best way to learn Vim is to start a special version of Vim called vimtutor:
You are immediately in Vim and in its "normal" mode. This means that the keyboard keys will not enter text but will be interpreted as interactive commands. To move the cursor, press the keys h, j, k, and l. Pressing jfor example, move the cursor one line below. Continue to press j until you see the second screen vimtutor:
and then proceed from there.
Start the next command
and you will get this window:
It's easy to create or open a file with Vim:
Three modes of operation in Vim
Vim has several modes of operation, but these three are the most important:
- NORMAL MODE – sliders, moving text, text buffers, text manipulation, etc.
- INSERT MODE – text insertion: input and editing.
- VISUAL MODE – a mode in which larger blocks of text can be defined, cut, pasted, etc.
press Esc twice to return to normal mode.
INSERT MODE In Vim
If you want to type text, press I in normal mode to turn on the Insert fashion. You will see the word – INSERT – in the lower left corner of the window and you can enter text.
In insert mode, I copied the paragraph above and clicked with the right mouse button – the text appeared in the Vim editor.
The command mode in Vim
You start issuing commands by typing two dots. For example, type : h for help, the following help screen will appear:
Type : q to return to the screen with the text.
To exit the file and exit the editor, enter:
To exit vim without saving the file, enter:
Here are the basic commands:
- save: : w
- save and leave: : wq
- exit: : q
- Obligate: ! (Example : w! : q!)
- copy: there
- copy a line: yy
- paste: p
- Cut: re
- to cut a line: not a word
By typing a number in front of the command, this command will be executed as many times as necessary. It means that if w is a command to move the cursor to the beginning of the next word, 15w will move the cursor 15 words to the right.
VISUAL MODE In Vim
Typing V in normal mode will activate the visual mode.
Note the label VISUAL LINE in the lower left corner of the terminal window.
In this mode, pressing keys such as j and k will move the cursor up and down, but will also mark a block of white text. Since we pressed V, the entire lines with the cursor will be marked. press v to allow the visual selection of text based on characters, in which case the lower left corner will only show the word VISUAL.
Now, to delete some text, press re (delete) and press there (copy) to copy the marked region. Then move the cursor to the desired paste location; pressing p will then stick after the cursor, while P will stick in front of her.
press c to change text … and for the following commands, be sure to devote more time than expected to learning Vim!
What can you do next?
We showed the basic editing capabilities of two almost universal text editors, nano and vi or vim. They may not be your cup of tea at all, but you should at least know enough about Vi / Vim to read a file, edit it and save it. Other famous publishers for which we have no room for maneuver include Emacs, Micro, NE, and many more to choose from for your VPS!
Dusko Savic is a technical writer and programmer.