Encrypting text files using VI editor

While creating text files using the “vi” editor we can make the files secure by encrypting the files with our own encryption key. Any encrypted file will be unreadable unless the coorect key is not known.

Let us say we have a file by the name temp:

$ cat temp This is test file to be used for encryption using vi editor in linux. We should not be able to read the contents of the file untill we do not know the encryption key.

To encrypt this file, so that no one with out the encryption keys is able to read or write from the file, open the file using vi editor

$ vi temp

Now go to command mode by typing “esc”

Then type :X and press enter.

Note the “X” is in upper case

VI will now prompt for an encryption key as shown below.

Enter any key, and press enter

It will prompt for confirmation of the key, enter the same key again .

To find out whether the file is encrypted or not we can use the command file

$file temp temp: Vim encrypted file data

Thus indicating that it is an encrypted file. If we try to read from the file using cat

$cat temp

The output will be junk value. If we try to open the file using “vi” editor, it will prompt us for the encryption key.

In case we entered the wrong key, the file will open but with garbage values in it, and it will be in read only mode.

Only on entering the correct key, the file be visible in the original form

To remove the encryption, open the file using “vi” editor by entering the correct key and go to the command mode. Use the command

set key

And press enter. It will prompt for a value of key, hit enter, do not enter any value for the key.

The encryption should have been removed now and we can edit the file as usual way.

Category: Linux | Comments Off on Encrypting text files using VI editor

Searching and highlighting in vi editor

In this post we will look into at the ways in which we can search in a program using the VI editor.

The searching always happens in the command mode so if you want to search for a string or a pattern in the program, first step is to go to the command mode by pressing “esc”

Let us take a simple hello world program as an example.
#includemain() { printf(“Hello world”); printf(“world”); }

Let us say we come across the string printf once and want to search all the occurrences of printf after the current cursor position, then just move the cursor on printf and press “*”. The cursor will automatically move to the next occurrence of printf and on each press of “*” it will move to the next occurrence of printf.

To search backwards, that is behind the current cursor position place the cursor on printf and press “#”. The cursor will move to the previous occurence of the search string. The second way of searching is
Go to command mode and type /

any thing that follows the “/” will be taken as the search string.

Then keep pressing “n” to move to the next occurrences of the search string. If you want to move back in the document, that is search for occurrences of the string before the current cursor position use the shift “n” , i.e “N” .
To see all the matching strings at the same time we can highlight all the matched strings by setting the option hlsearch i.e.

:set hlsearch

All the items which matched the previous search string will get highlighted. To remove the highlight run the command

:nohlsearch

To make the seach case insensitive we need to set the option “ignorecase” i.e

set ignorecase

The search can be made case insensitive by adding \c to the search string, i.e

/\c/

The search can also be restrcted to specific lines. To restict the search only to a single line we need to use \%numl along with the search string. To restrict the search for strintg printf to 20th line we need to use

/\%20l\printf/

To restinct the search to lines after 20th line

/\>%20l\printf/

To restinct the search to lines between 20th line and 30th lines

/\>%20l\%

Category: Linux | Comments Off on Searching and highlighting in vi editor

Enabling mouse in vi editor in linux

While using the “vi” editor, one of the common problmes that we face is the inability to the mouse to move around in the file. Here is simple way to enable to usage of mouse in vi editor.

Open the vi editor in the command mode (Press “esc” to enter command mode) run the command

set mouse=a

Now you should be able to click any where in the document and the cursor will move to that location.

To enable the mouse every time “vi” is launched we need to open the file

/etc/vim/vimrc

And uncomment the line

“set mouse=a”

Note: un-commenting is done by removing the ‘ ” ‘ at the beginning of the line.

Save and close the file. From the next time any file is opened using “vi” editor the mouse will be enabled by default.

Category: Linux | Comments Off on Enabling mouse in vi editor in linux