Vim – An Advance Text Editor

You may already get tired of the nano text editor. It’s simple but not efficient nor sufficient. So we are introducing Vim – Vi Improve.

Vim is a powerful and highly customisable text editor. It’s based on and an improvement of Vi, another text editor included in Raspbian. The trade off for power is of course, the cost to learn. So I didn’t put this as a basic level of the tutorial, but as an advance.

To install vim, like all other packages:

  • sudo apt-get install vim

To start:

  • (sudo) vim <filename>

Like nano, if file exist, vim will open that file, otherwise a new file will be created.

Directories are also a special file in Linux. However nano cannot open them but vim can. you can try to open a directory and see what happens. It acts as a document tree!

Now open a blank file and we’ll go though some basic of vim:

  • vim ~/test

At the bottom we can see the name of file and it’s a new file, then two numbers 0,0-1, that’s the position of the cursor (line number 0, Position in the line 0). We ignore the All at the moment.

Vim as two modes, command mode and insert mode. The default is command mode which treat all keyboard input as commands. To enter insert mode to make direct changes, we need to switch to insert mode first by hitting i.

Now INSERT should be appear at the lower left and all inputs are actually recording.  Exit insert mode by hitting esc, and the INSERT would disappear, means vim is switched back to command mode.

To save the file and exit vim, enter :wq under command mode.

Here are more commands that are often used, all command are used in command mode only and they are case sensitive:

Enter Insert Mode:

i – insert before the cursor

a – append, insert after the cursor

I – insert at the start of the line

A – Append, Insert after the end of the line

o – start a new line below and insert

O – start a new line above and insert

s – delete the cursor character and insert

S – delete the line and insert

r – replace cursor character

R – replace mode (insert mode but replace)

Move the Cursor and Navigate:

In insert mode, arrows are the only way to navigate, but there are a lot more ways in command mode apart from the arrows. Arrows can be repeated for a number of times (jump) if there’s a number before it, eg. 3h – move 3 characters to the left.

h – left arrow

j – up arrow

k – down arrow

l – right arrow

gg – go to start of the file

G – go to end of the file

<Line Number> G – go to line

0 (zero) – go to start of line

$ – go to end of line

% – find match bracket

. (dot) – repeat last command

Cut, Copy, Paste and Delete:

In vim, all deletes act as cut, saved to a buffer and can be paste later. Most of these commands can be repeated for a number of times if there’s a number before the command, eg 12x – delete next 12 characters, 3Y – copy next 3 lines.

x – delete (cut) the character under the cursor

X – delete (cut) the character before the cursor

d<h/l> – delete (cut) the character under/before the cursor, equivalent to x/X.

d<j/k> – delete (cut) the line above/below.

dd – delete (cut) the current line

y<h/l> – yank (copy) a character before/under the cursor

y<j/k> – yank (copy) the line above/below

Y – yank (copy) the current line

p – paste after the cursor (or new line below)

P – paster before the cursor (or now line above)

Undo and Redo:

u – undo

U – undo current line only

ctrl + r – redo

Find and Replace:

Find and Replace are also case sensitive. If a special character need to be enter, have a \ before it, eg. to find all !=, enter /\!\= instead of /!=

/<string> – find <string> (from top to bottom)

?<string> – find <string> in reverse order (from bottom to top)

n – next match

N – previous match

:s/<old>/<new>/g – substitute  <old> with <new> in the current line

:%s/<old>/<new>/g – substitute  <old> with <new> in the whole file

Open, Save, Quit:

These commands can be combined. Start with : (column).

:w – save file

:w <filename> – save as <filename>

:o/:e <filename> – open/new file

:q – quit (exit) vim

! – override warnings (ignore all warnings, normally used with :q! – quit without saving)

:! <command> – execute command using system shell

:n – next file (if open multiple files in shell)

:N – previous file

:help – help file

Here’s a cheatsheet for all commands from GitHub: Vim Cheat Sheet

 

Apart from these powerful commands, vim setting file also make an important role:

  • vim ~/.vimrc

As a beginner, there lines are recommended:

  • syntax on
  • set nocompatible
  • tabstop=4
  • shiftwidth=4
  • set autoindent
  • set smartindent

If you prefer line number:

  • set number

If you want to use your mouse pointer to navigate:

  • set mouse=a

Save and exit, and open vim again to see changes.

You may seek more vimrc command to build your personal vim.

Last but not least, vim provides many useful plugins that are awaiting for you to discover!

OpenVPN – Securely Access Home Network from Anywhere

OpenVPN was the first project I achieved on my Raspberry Pi. It is, actually, the reason why I bought my Pi. This is also the first tutorial on server application.

First, we need to enter root user and install OpenVPN server:

  • sudo su
  • apt-get install openvpn

The reason for using root user instead of sudo is, there are some command we can’t use sudo, e.g. sudo cd /etc would not work.

Then we need to copy EasyRSA sample file to /etc/openvpn:

  • cp -r /usr/share/doc/openvpn/examples/easy-rsa /etc/openvpn/easy-rsa

And edit setting parameter of EasyRSA:

  • cd /etc/openvpn/easy-rsa
  • nano vars

There are a few lines we need to change:

  • export EASY_RSA=”`pwd` /etc/openvpn/easy-rsa”
  • export KEY_SIZE=1024 2048
  • export CA_EXPIRE=3650 730
  • export KEY_EXPIRE=3650 365

The first line is to specify where the keys are stored. Then the length of the key, 2048 bits are a lot safer than initial 1024 bits. The third and fourth lines are the expiration date from issue, initial it’s 3650 days (10 years) but shorter is safer. These two lines can be changed to your preferences.

The next few lines need to be changed to your own information:

  • export KEY_COUNTRY
  • export KEY_PROVINCE
  • export KEY_CITY
  • export KEY_ORG
  • export KEY_EMAIL
  • export KEY_OU (can be blank)

Save and exit nano.

Then we need to configure OpenVPN config file. Download the files below and copy them to /etc/openvpn:

  • Server Config
  • Client Config

We need to edit config files to tell OpenVPN some settings and where all the keys are:

  • nano server.conf client.conf

For server.conf, replace the <Server Name> to your own server name. This can be whatever you like but we need it next step.

For client.conf, replace the <Server Address> to your static IP address or domain name.

If you like to know all the lines are about, visit https://openvpn.net/index.php/open-source/documentation/howto.html#server. If you decide to change the protocol or port number, remember to update both config files.

Save and exit nano.

Next we need to generate some keys. Here are two scripts I use for one click generate.

  • OpenVPN Server Key
  • OpenVPN Client Key

Download the two files and copy them to /etc/openvpn/easy-rsa, then change the owner and permission:

  • chown root:root build-server build-client
  • chmod 755 build-server build client

Then we can run it and generate the keys in one click:

  • ./build-server <Server Name>
  • ./build-client <Client Name> <Password>

Note we need one client key per client. The same key can be used on multiple devices. Challenge Password must be blank.

Then we need to copy the client file to your client device and connect.  The config file ends with ovpn. This can be done via scp, ftp or email. Or, I made a website for downloading config files.

OpenVPN client for can be downloaded at:

Then open the ovpn file on the client and enter your passphase to connect.

Now you should be able to connect to the VPN but you cannot do anything on the internet, because we need to forward VPN connection to ethernet.

We need to enable forwarding in the system first:

  • nano /etc/sysctl.conf

Find this line and uncomment it (remove the hash at the beginning):

  • net.ipv4.ip_forward=1

Save, exit and refresh the settings:

  • sysctl -p /etc/sysctl.conf

Next, we need to specify the forward rules:

  • nano /etc/openvpn-firewall.sh

Add this line in, replace the interface (eth0 for cable and wlan0 for wifi) and IP address:

  • iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o <interface> -j SNAT --to-source <pi's IP address>

Then set to load this config on network card:

  • nano /etc/network/interfaces

And add this line to the appropriate interface with indent (add a tab or 4 spaces before the line):

  • pre-up /etc/firewall-network.sh

Restart network card (or reboot pi) and openvpn should now work:

  • sudo ifdown <interface>
  • sudo ifup <interface>

That’s done! Now you may try to connect from internet (eg. mobile data) before go out and access your home network from anywhere!