Working with raw text files on Linux – the Vi command

There will be times when you will need to edit the raw text files. You can do so by utilising the raw text file that comes along built-in with all mac and Linux operating systems. How much you can pull off with this editor is absolutely based on how much effort you are willing to put and putting into it. No worries, let us start with small.

Create your raw text file

Let’s create a new text file name “Bora”. The way would do it is downright simple: vi Bora.

Now you will be inside the text editor with a blank file and the name of the file at the end of the line.

This vi editor has some sort of non-intuitive concept which allows the user to be either in input or editing mode. This is probably done so in a way to keep the layer of security tight. The Insert mode allows you to insert and edit text. The Command mode allows you to work with command like copy, paste, cut, delete line, characters etc as well as saving the file. By default, you will enter with the command mode. You can change that by pressing ‘i’ on the keyboard to get into the insert mode. Now that you are in the insert mode, you can write whatever you want here. You can straight write some code here as well and hit the enter at the end of these lines. You can also use the backspace key if you made any error.

Saving your file

Note that no other keys will work here such as of window notepad as sort. When you are done, you can press the ESC key to exit the insert mode and get back to the command mode. Now to save your new file and quit it, you simply type ZZ, has to be uppercase, and hit the enter key. This will save the file and exit from the Vi editor. In case, if you have to get back to this file and edit some stuff, you can always do so by running the same way you created this file. Continuing with our example, if we wanted to edit our Bora file, we would do -vi Bora.

One of the two major things you want to remember is to keep track of the non-intuitive mode you are in. Make sure to press the I key to get into the insert mode and ESC key to get into the command mode. Don’t worry about the mistakes for the first few times. You will get used to it.

Vi command set is very old-school and still works because it is quite powerful. The following list of commands can make you aware of the power of the Vi commands.

Command for navigation

h is used to move the cursor to the left and j to move the cursor one line to the down. k moves the cursor one line up, while w moves the cursor one word to the right. l moves the cursor one character to the right and b moves the cursor one word to the left. 0 moves cursor to the beginning of line and $ will move the cursor to the end of the line.

To insert to the left of the current position, press i. R is used for setting the changes for the current character and dd deletes current line. D deletes the segment of current line to the right of the cursor while x deletes the current character. Use :w to save your file and :q!  means Quit vi but hec, do not save! With ZZ you can save the file and quite the vi editor.

These may seem too much to wrap your head around but honestly this is nothing crazy and nobody expects you to remember this unless you are sitting for an exam of some sort of techy exam. Most of this, as for usage, will stick around over time and usage. Even though, this is quite easy and one of the important aspects of command line mastery, lot of Linux users will simply look for modern editor like Vim, joe or emacs or more graphical editor like gVim or gedit.