Text files must be constantly read, edited and changed on every VPS, usually after the installation of new packages. Learning to practice and learn with command line text editors can make a big difference in your experience and results with VPS.
If you have problems with your server, the only resource is the system console. You can only use them via a text editor. The only editor guaranteed to exist anywhere is Vi or his descendant Vim. In this tutorial we will learn Nano and Vim, two popular text editors that you can run from a VPS terminal.
A problem for most VPS newbies is that viThe only text editor that comes standard in any Linux distribution is neither WYSISWYG nor intuitive. If you expect commands from Windows Notepad or MacOS TextEdit to run, you'll get frustrated and disappointed in no time. For example, suppose you follow a script to install a widely used program like Nginx, and it contains a command like
sudo vim /etc/nginx/nginx.conf
If this is your first contact with vi, you will discover that
A) You can not enter in the editor (unless you press at random I on the keyboard) and that
B) You have no idea how to save the file and leave the editor (until you find out) : wq will do the trick).
Over time, an improvement in the vi called vimbecame very popular with power users. It's an almost strict superset of vi But with dozens of extra features, you can automate logs, plug-ins, tasks, work with multiple files simultaneously, using the internal language VimScript, or more formal Python, Ruby, Perl, or Tcl as scripting languages.
Vim It's hard to learn completely, but it's well worth the effort.
Problems that novices have with Vi / Vim is why another editor, Nanois so popular. The available commands are displayed as a menu on the screen, and when you start typing, text is filled out. No wonder many users have installed a "droplet", an "instance" or a "server" first, their VPS boxes will be installed Nano,
What we will treat
For Nano and Vi / Vim we will show how to:
- Install the editor under Debian / Ubuntu and Centos
- Explain the philosophical approach to editing
- Get help
- Create a new one or open an existing file
- Display important commands in the editor
- Save file and exit
- Debian / Ubuntu or Centos systems
- SSH capability in the server
- Since Vim is a superset of Vi, we will focus only on Vim
Installing the editors on Ubuntu 16.04 and Debian 9
vi should be present on all servers, but some can actually get fetched Vim, We will show installation commands on all systems as there may be differences between hosting providers and server versions.
Update and update the current version of Ubuntu / Debian first:
sudo apt update
sudo apt upgrade
Depending on the Debian version, the sudo Command may not be detected immediately after installing the server. If this is the case, just leave it out sudo from the commands.
Only on Ubuntu when you receive the following prompt:
Just hit the tab key on the keyboard and then press Enter.
You can then install updates if necessary:
sudo apt dist-upgrade
Then install Nano:
sudo apt install nano
We see that it is already installed on Ubuntu 16.04:
Let's install Vim now:
sudo apt install vim
It is also preinstalled.
Installing editors in Centos 7
The commands are:
sudo yum update
sudo yum install nano
sudo yum install vim
To start the editor
To start an editor, call it at the command prompt with its name. If you add a file name, two things can happen. If there is no file at this address, the editor creates an empty file. If the file exists, it will be opened in the editor.
When is Nano used?
This is the ideal case for using Nano: they are (fairly) new to VPS servers, but have only purchased one to run a particular program on one or more sites. So you browse around and find a good recipe for installing the software you want. You follow the example and reach a point where a configuration file needs to be changed. Nano is an ideal editor for those small, fast, and dirty jobs where you add one or two lines, such as changing the site name for Nginx, and so on, and then save and close the file.
Now switch to a neutral folder like / tmp and open a file called sometext.txt in it:
cd / tmp
We can immediately start typing new text or paste existing text into the clipboard. If Windows is a terminal window (for example, Kitty or Putty), insert text with a right mouse click. Under MacOS, a simple Cmd-V should work.
I have copied the above paragraph and I have the following:
The text is all in it, but you only see the end. Nano does not break text by default, as this is the expected behavior when editing configuration files. Press to switch to broken text Esc, release it and then press $:
That was soft wrapping, hard wraps are turned on or off Esc + L,
The normal way to issue commands in nano is the Ctrl key plus a letter called as ^ G – that is, press Ctrl and then press the pres button G Hold down the Ctrl key. In the menus nano uses capital letters G So you have to press the shift key and then probably G In fact, you do not have to press the Shift key at all.
Instead of Esc followed by a key press, you can also use the key Old with a simultaneous keystroke. So hard winding would mean pressing OldHold down the button and then press shiftHold down the button and then press l, In general, Old and Esc are called Metaschlüssel and – depending on your keyboard – another key can be assigned this role.
Help screen in Nano
Ctrl-G gives you the most important nano-help text on the screen:
Scroll down with combinations of ^ Y and ^ V or, if on a regular PC / Mac keyboard, just use PageUp and Page down Key. You can also scroll up and down using the cursor keys.
We will now examine keyboard shortcuts in more detail:
Commands starting with M, for example M- (Call up the meta-key, which, as we have already seen, can be Esc. Old or something else. So you can jump to the beginning of the paragraph by pressing Esc, release the pressure and then press shift and 9 at the same time. Or you can press Old, Press and hold shiftHold down both buttons and then press 9,
Press Ctrl-X to exit the help screen.
Save file With nano
Use to save files with nano Ctrl-O, The menu below changes to:
To save the file with the changes, just press Enter,
Press Ctrl-X leaves nano if the file is saved; If not, a confirmation will be requested Y.,
In the main menu we see that Ctrl-W will find 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 "normal" mode. This means that when you press keys on the keyboard no text is entered, but interpreted as interactive commands. To move the cursor, press the buttons H. j. k, and l, Press jFor example, moves the cursor one line below it. Continue to press j until you see the second screen in vimtutor:
and then continue learning from there.
Run the following command
and you get this window:
With Vim you can easily create or open a file:
Three operating modes in Vim
Vim has several modes, but these three are the most important:
- NORMAL MODE – cursor, moving through text, text buffer, text manipulation, etc.
- INSERT MODE – insert text: tap and edit.
- VISUAL MODE – 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 enter text, press I in normal mode to ignite it Insert Mode. You will see the word – INSERT – You can enter text in the lower left corner of the window.
In insert mode, I copied the above paragraph and right-clicked – the text appeared in the Vim editor.
The command mode in Vim
You enter commands by typing a colon. For example, enter : H For help, the following help screen is displayed:
kind : q to return to the screen with text.
Type the following to exit the file and exit the editor:
To stop vim without saving the file, type:
Here are the basic commands:
- save up: : w
- save and close: : wq
- Exit: : q
- Makes: ! (Example : w! : q!)
- Copy: y
- copy a line: yy
- Insert: p
- Cut: d
- to cut a line: dd
If you enter a number before the command, this command will be executed so many times. That is, if w is a command to move the cursor to the beginning of the next word, 15w Moves the cursor 15 words to the right.
VISUAL MODE In Vim
Tap V In normal mode, the visual mode is activated.
Note the label VISUAL LINE in the lower left corner of the terminal window.
In this mode you press keys like j and k Moves the cursor up and down, but also marks a white text block. We pressed there V, whole lines with cursor are marked. Press v In this case, only the word is displayed in the lower left corner VISUAL,
Now press to delete text d (Delete) and press y (tear) to copy the marked area. Then move the cursor to the desired insertion position. to press p will then paste after the cursor while P will insert before.
Press c to change text … and to get more commands, you should spend more time learning Vim than you expected!
What can you do next?
We showed the basic editing features of two near-universal text editors, Nano and Vi or Vim. They may not be your cup of tea, but you should at least learn enough about Vi / Vim to read, edit, and then save a file. Other famous editors we can not live up to here are Emacs, Micro, NE, and there are many others to choose from for your VPS!
Dusko Savic is a technical writer and programmer.