LowEndBox - Cheap VPS, Hosting and Dedicated Server Deals

How to Edit Files From Your Linux VPS Terminal

There is a constant need to read, edit and change text files on any VPS, mostly after the installation of new packages. Learning, practicing and gaining proficiency with command line text editors can mean a world of difference in your experience and results with VPS.

If your server runs into a trouble, the only resource will be access to system console and the only way to use it will be through a text editor. About the only editor that is guaranteed to exist everywhere is Vi or its descendant, Vim. In this tutorial, we will study nano and Vim, two popular text editors that you can run from a VPS terminal.

Vi Editor

A problem for most newcomers to VPS is that vi, the only text editor that is by default present on any Linux distribution, is neither WYSISWYG nor intuitive. If you expect it to obey commands from Windows Notepad or MacOS TextEdit editors, you’ll be frustrated and disappointed in no time. Let’s say you are following a script to install some widely used program, say Nginx, and that it contains a command such as

sudo vim /etc/nginx/nginx.conf

If this is your first contact with vi, you will discover that

A) you cannot type into the editor (unless you by chance press i on the keyboard) and that

B) you have no idea how to save the file and leave the editor (until you find out that :wq will do the trick).

Vim Editor

In time, an enhancement of vi called vim, became very popular with power users. It is an almost strict superset of vi but with dozens of additional features sucs as protocols, plugins, tasks automation, working with several files at once, using its internal language VimScript or more formal Python, Ruby, Perl, or Tcl as scripting languages, and so on.

Vim is difficult to learn completely, but well worth the effort.

Nano Editor

Problems that newcomers have with Vi / Vim is the reason why another editor, nano, is so popular. It will show the available commands as a menu on screen, and when you start typing, you will see text filling in. No wonder the first thing many users do after installing a “droplet”, an “instance” or a “server” on their VPS boxes, will be to install nano.

What We Are Going To Cover

For nano and Vi / Vim we shall demonstrate how to:

  • Install the editor on Debian / Ubuntu and Centos
  • Explain the philosophical approach to editing
  • Get help
  • Create new or open an existing file
  • Show important commands within the editor
  • Save file and exit

Prerequisites

  • Debian / Ubuntu or Centos systems
  • Ability to SSH into the server
  • Since Vim is a superset of Vi, we are going to concentrate on Vim only

Installing Editors on Ubuntu 16.04 and Debian 9

Vi should be present on all servers, however, on some it may actually envoke Vim. We are showing commands for installation on all systems as there may be differences amongst hosting providers and server versions provided.

First update and upgrade the current version of Ubuntu / Debian:

sudo apt update
sudo apt upgrade

Depending on the version of Debian, the sudo command may not be recognized immediately after the server is installed. If that is the case, just omit sudo from the commands.

On Ubuntu only, if you get the following prompt:

just press Tab on the keyboard and then Enter.

You may then install 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:

Let’s now install Vim:

sudo apt install vim

It comes preinstalled as well.

Installing Editors on Centos 7

The commands are:

sudo yum update
sudo yum install nano
sudo yum install vim

How to Start the Editor

To start an editor, you invoke it by its name at the command prompt. If you add a file name, two things can happen. If there is no file at that address, the editor will create an empty file, and if the file exists, it will be opened in the editor.

Nano

When To Use Nano

This is the ideal case for using nano: you are (fairly) new to VPS servers, but you have just acquired one in order to run one specific program on it, or a couple of sites. So you snoop around and find a good recipe for installing your software of choice, you follow along and you reach a point when a config file needs to be changed. Nano is an ideal editor for such small, quick and dirty jobs, where you add a line or two, say, tweak site name for Nginx and so on, then save and close the file.

Now move to a neutral folder such as /tmp and open a file called sometext.txt in it:

cd /tmp
nano sometext.txt

We can start typing new text right away or we can paste a text we already have in clipboard. If it’s a terminal window under Windows (such as Kitty or Putty), you will paste text with the right click of the mouse and if it’s under MacOS, a simple Cmd-V should do.

I have copied the above paragraph and this is what I’ve got:

The text is all in there but you see only its very end. Nano by default does not wrap text as that is the expected behaviour when you edit configuration files. To change to wrapped text, press Esc, release it, and then press $:

That was soft wrapping, hard wraps are turned on or off with Esc + L.

The normal way to issue commands in nano is through Ctrl key plus a letter, which is denoted as ^G – meaning press Ctrl and then pres key g while still holding Ctrl. In the menus, nano uses upper-case G so it seems you have to press the Shift key and then g but actually, you do not have to press Shift at all.

Instead of Esc followed by a key press, you can also use key Alt with a simultaneous key press. So hard wrap would mean pressing 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 – some other key may be assigned that role.

Help Screen in nano

Ctrl-G will get you main nano help text on screen:

Scroll down with combinations of ^Y and ^V or, if on a normal PC / Mac keyboard, just use PageUp and PageDown keys. You can also scroll with cursor keys, up and down.

We’ll now study keyboard combinations a bit more:

Commands starting with M, for instance, M-(, call for pressing the Meta key, which, as we have seen already, can be Esc, Alt or something else. So you can go to the beginning of the paragraph by pressing Esc, release pressure, then press on Shift and 9 at the same time. Or, you can press Alt, keep the key pressed, press on Shift, hold both keys pressed and then press on 9.

Press on Ctrl-X to leave the help screen.

Saving File With nano

To save file with nano, use Ctrl-O. The menu at the bottom changes to:

To save the file with changes, just press Enter.

Pressing on Ctrl-X will leave nano if the file is saved; if not, it will ask for confirmation with Y.

From the main menu, we see that Ctrl-W will find text, _Ctrl-_ is for the replace operation and so on.

Vim Editor

Learning Vim With Vimtutor

The best way to learn Vim is to start a special version of Vim called vimtutor:

vimtutor

You are in Vim right away, and in its “normal” mode. It means pressing keys on the keyboard will not enter text but will be interpreted as interactive commands. To move cursor press on keys h, j, k, and l. Pressing j, for instance, will move cursor one row below. Keep pressing j until you see the second screen in vimtutor:

and then proceed with learning from there.

Running Vim

Run the following command

vim

and you’ll get this window:

It’s easy to create or open a file with Vim:

vim anothertext.txt

Three Modes of Operation in Vim

Vim has several modes of operations but these three are the most important:

  • NORMAL MODE – cursors, moving through text, text buffers, text manipulation etc.
  • INSERT MODE – inserting text: typing and editing.
  • VISUAL MODE – mode in which larger blocks of text can be defined, cut out, pasted and so on.

Press Esc twice to return to the normal mode.

INSERT MODE In Vim

If you want to type text, press i in the normal mode to ignite the Insert mode. You’ll see the word — INSERT — in the lower left corner of the window and you’ll be able to enter text.

In insert mode, I copied the above paragraph and right clicked on the mouse – the text appeared in Vim editor.

The Command Mode in Vim

You start issuing commands by typing a colon. For example, type :h for help and the following help screen will show up:

Type :q to get back to the screen with text.

To leave the file and exit the editor, enter:

:x[return]

To quit vim without saving the file, enter:

:q![return]

Here are the basic commands:

  • save: :w
  • save and exit: :wq
  • exit: :q
  • force: ! (example :w! :q!)
  • copy: y
  • copy a line: yy
  • paste: p
  • cut: d
  • cut a line: dd

Typing a number in front of command will execute that command that number of times. That means that if w is a command to move cursor to the beginning of the next word, 15w will move cursor 15 words to the right.

VISUAL MODE In Vim

Typing V in normal mode will turn on the Visual mode.

Note the label VISUAL LINE in the lower left corner of terminal window.

In this mode pressing keys such as j and k will move cursor up and down but will also mark a white block of text. Since we have pressed V, entire lines with cursor will be marked. Press v to enable character- based visual selection of text, in which case, the lower left corner will show only the word VISUAL.

Now, to delete text, press d (delete) and press y (yank) to copy the marked region. Then move the cursor to the desired paste location; pressing p will then paste after the cursor, while P will paste before it.

Press c to change text… and for further commands be sure to devote more time to learning Vim than you expected!

What Can You Do Next

We have shown basic editing capabilities of two almost universally present text editors, nano and Vi or Vim. They may not be your cup of tea at all, but you should at least learn enough of Vi / Vim to read a file, edit and then save it. Other famous editors we have no space to do justice here are Emacs, Micro, NE, and there are many others to choose from for your VPS!

Dusko Savic is a technical writer and programmer.

duskosavic.com

Kaylee

No Comments

    Leave a Reply

    Some notes on commenting on LowEndBox:

    • Do not use LowEndBox for support issues. Go to your hosting provider and issue a ticket there. Coming here saying "my VPS is down, what do I do?!" will only have your comments removed.
    • Akismet is used for spam detection. Some comments may be held temporarily for manual approval.
    • Use <pre>...</pre> to quote the output from your terminal/console, or consider using a pastebin service.

    Your email address will not be published. Required fields are marked *