Wednesday, 13 July 2016

vi editor: Programmers' choice - Part 1

Before going any further, we would like to start with an editor.

Why an Editor ?
  1. Once you are in Unix/Linux, you will most likely deal with many files and you may need to write you own scripts to work with the OS.
  2. If you are a programmer, you might want to find a good editor for your source codes.

I will be describing one of the editors available for you in any UNIX/Linux distribution.
The editor is known as vi editor and for many programmer this editor suffices their needs.

So, let's start with this editor.

What's the hype about this one ?
Well, vi is a screen based text editor which runs on terminal. Let me tell you some features of this editor.
  • No need of installation. With all major UNIX/Linux/Mac OS distribution you get this one bundled.
  • Choosing this editor helps you write your source code in any UNIX/Linux distribution. Even if you are on Windows you can install this editor.
  • Less memory footprint.
  • Does exactly what you need.
  • If used properly this can help you with lot of typing.
  • Very much user friendly.
Well, that's all the features I like about vi. If you know some, please comment and I will add to this list.

Apart from these, you can directly take views from real time users here -
https://www.quora.com/Why-are-tools-like-Vim-and-Emacs-still-used-for-coding
https://www.quora.com/Why-do-some-developers-prefer-terminal-style-editors-like-vi-Vim-and-Emacs-instead-of-GUI-based-editors-like-Sublime-Text


Now you may wonder how to use this editor.
Well, it's pretty simple, just type vi in the terminal and it invokes the editor.

For Windows users, there are some more steps to go ahead.
  • Navigate to this page with any of your favourite browser, http://vim.sourceforge.net/download.php 
  • Move to this section PC: MS-DOS and MS-Windows
  • Install the editor
  • Add the installation path to the environment PATH variable
  • Open command prompt/Poweshell and type vim
That's it we are all set to use this beautiful editor.

To keep things simple, we'll start with an empty directory.
palash@palash-pc:~$ cd
palash@palash-pc:~$ mkdir vi-tut
palash@palash-pc:~$ cd vi-tut

If you are new to linux and do not know any command, you may find this difficult. Let me tell you the commands I used and what they do.

cd - Change directory. When we want to move to any directory, we use this command. When cd is entered to the command line without any argument, it moves to the home directory.
So, in the first line, we've moved to the home directory and in the third line, we've moved to a directory named vi-tut.

mkdir - Make a directory. If you want to create a directory, use this command and it will create an empty directory with the name specified as argument. If it finds a directory already present, it will throw an error. If you get any error, provide another name.

This is in short about the commands we've used in this article. We'll dive deeper in directory structure, naming conventions etc. etc. later. For now, these two command suffices more than enough.

We started with our home directory and created a directory named vi-tut. We are currently in this directory.
Let's start our journey. We will go through some very basic concepts of this editor and then will get our hands dirty.

Modes of vi editor

vi editor comes with keyboard-only concept. What I mean by this is that unlike other editors(like Notepad, Notepad++, GEdit, emacs etc) vi editor does not come with a menu to deal with file related tasks. Instead, it uses different key combinations for this purpose.

So, you might be thinking, if it uses different key combinations, don't they show up in the file content ?

Yes, you are thinking in a right direction. To handle this scenario, vi editor works on two different modes. First mode is to issue commands to the editor and the other mode is for edit purpose. So using the keyboard only we can deal with file contents.

Let's dive deeper on these two  modes.

Command Mode

This mode talks to the editor instead of printing the entered characters into the main content. It is the default when you open a file in vi editor.

Insert Mode

This mode is for dealing with the content of the actual file. When we type anything on the keyboard, the same goes to the content of the file.

All these seem to be very confusing at first. So, let's start our journey in the system.
[palash@palash-pc vi-tut]$ vi

This is command to invoke vi editor. The simplest of all...
So, this will open the editor and should see something like the following
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                          VIM - Vi IMproved                                                                            
~                                                                                                                                                                       
~                                                                           version 7.4.1910                                                                            
~                                                                       by Bram Moolenaar et al.                                                                        
~                                                             Vim is open source and freely distributable                                                               
~                                                                                                                                                                       
~                                                                    Help poor children in Uganda!                                                                      
~                                                            type  :help iccf<Enter>       for information                                                              
~                                                                                                                                                                       
~                                                            type  :q<Enter>               to exit                                                                      
~                                                            type  :help<Enter>  or  <F1>  for on-line help                                                             
~                                                            type  :help version7<Enter>   for version info                                                             
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
                                                                                                                                                      0,0-1         All

Note: The color combination my vary depending on your terminal.

So, what it says ?
It tells you that you have opened the vi editor and it is running the improved version of vi editor with some other information as well.

Simply type ZZ (note the capitals) and it will exit the editor.

Next we will try vi editor with an argument,
[palash@palash-pc vi-tut]$ vi myText.txt

This will open the editor once again but you should see some difference this time. Following is what we are seeing,
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
"myText.txt" [New File]                                                                                                                                                

So, this time you are seeing the editor with no banner and also the file name is mentioned in the bottom left corner.

Now, let's start typing some text on this file. To enter something in the file, we need to enter the Insert mode. To do so, type i. And you are in Insert mode.
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
-- INSERT --                                                                                                                                          0,1           All

Note the -- INSERT -- description on the bottom left corner and the Line Number, Character Number information in the bottom right corner.
Well, for some version of vi editor the -- INSERT -- banner does not show. In that case, you might be in a confused state. To overcome this confusion, hit Esc key twice which will definitely change to command mode.

Now type something in the file and hit Esc key once or twice. It will go back to Command Mode. This is the following what I've written in the file and went back to Command Mode
A quick brown fox jumps over the lazy dog.
A quick brown fox jumps over the lazy dog.
A quick brown fox jumps over the lazy dog.
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
"myText.txt" 3L, 129C                                                                                                                                                  

Now the text file "myText.txt" contains 3 Lines and 129 characters and all these information are available in the bottom of the editor. Now exit the editor. Following are the commands you can use to exit the editor.
ZZ - Saves the content displayed on the screen into the file and exits the editor
:wq - Saves the content displayed on the screen into the file and exits the editor.
:w - Saves the content displayed on the screen into the file but does not exit the editor
:q - Does not save the file and exit the editor. This command won't let you exit the editor if the content has changed or the file is in read only mode.
:q! - Does not save the changes and exits the editor.

Now, you might have already experienced the following, while typing ZZ into the command mode, nothing was written on the screen. But when you have entered :w or :wq or :q or :q! the characters entered are also displayed in the bottom left corner of the editor. If you have not noticed this, let me show you once more,
A quick brown fox jumps over the lazy dog.
A quick brown fox jumps over the lazy dog.
A quick brown fox jumps over the lazy dog
A quick brown fox jumps over the lazy dog.
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
~                                                                                                                                                                       
:wq

So, you see in the bottom left corner, :wq is also printed.

This is also known as command line mode (command mode where you can type commands).

Once you are done, you can open the file in read only mode using the following command,

[palash@palash-pc vi-tut]$ vi -R myText.txt 

This command will open the file in read only mode (note -R in the argument). So, you cannot change anything in this file. I will leave the experimental steps on the reader.

Or, you can use the following commands also to view the file content.

[palash@palash-pc vi-tut]$ more myText.txt 
A quick brown fox jumps over the lazy dog.
A quick brown fox jumps over the lazy dog.
A quick brown fox jumps over the lazy dog
A quick brown fox jumps over the lazy dog.

Now I want you to do one thing, create a new text file with name "quick-brown-fox.txt" and type this text "A quick brown fox jumps over the lazy dog." 50 times and save the content.
While saving the content, use different commands like ZZ, :wq, :w, :q one at a time.
To create a new file with the name, you should type in the terminal,
vi quick-brown-fox.txt

We'll use this file in our next article on vi editor. One humble request to you, please do not copy paste the content, instead write each line. I know, it will be difficult but it's worth the value. It will not only improve skill on vi editor, it will also improve your typing skill. So, no cheating please. You are also encouraged to try some other things or key combination in command mode.

Quick Recapitulation

From my personal experience, I can tell you that this may be one of the confusing articles you've ever read. So, give it some time, try to grasp the basics, take break, complete the task I've suggested, stay focused and have the appetite to learn something new.
To help you with this article, let me summarize what we tried here -

  1. Tried to get the basic commands for directory handling with cd and mkdir
  2. Knew about the very basic yet powerful editor in UNIX/Linux
  3. Knew about the installation procedure in Windows OS as well
  4. Knew about different modes of vi editor
  5. Tried the editor with or without arguments
  6. Knew how to appear in different modes of vi editor
  7. Tried to save the content with different commands
  8. Tried to read the file content using vi read only mode
  9. Tried to use another command to read the content of the file
That's all for today. We'll next look into some more commands of vi editor which will ease our lives.

vi editor: Programmers' choice - Part 2

So we are good to deal with file opening, inserting some text in the file and saving it in the vi editor. In this post, we'll be looking into some more details like how to move the cursor to some place, how to copy and paste some words or a whole line etc.

So, in our earlier discussions, we have created a file with 50 lines of text and we will be using the same file to demonstrate some other commands of vi editor.

Open the file using vi command. Remember all of this post works in Command Mode and not in Insert mode. So, be careful about what you are doing or you will end up by messing up everything.

Now going any further, let's print the line numbers in the file. To do that, simply write in the command mode,

:set number


So, in the bottom left corner, you can see, I've entered the command to show the line numbers. This is pretty useful when dealing with long files. Let's see what happens when we hit enter after typing the command,

Now you can see that in each line the corresponding line numbers are shown. Again, note that the color combination in your system may vary from mine based on the version of vi and the terminal profile.

Now, let's start with moving the cursor in any direction. First move to command mode and try the following.
Following are the required keystrokes to move the cursor.
h - moves the cursor to one character left
j - moves the cursor to the lower line
k - moves the cursor to the upper line
l - moves the cursor to one character right

So if you press the key combination in succession, you will return back to the same character you started with h k l j. Play with this combination couple of times and come back.

Now let's try these commands as prefix,
move the cursor to the middle of a line and try the following commands
5h 5j 5k 5l
or
3h 3k 3l 3j
or,
10h 10j 10k 10l
or
<number>h <number>j <number>k <number>l
This time you will see that instead of moving a character, the cursor is moving the specified number of characters. For example 5h will move the cursors 5 characters left and if there are not enough characters, it will move to the first character. Similar goes with j k and l.

Now, this all about moving by characters, we can also move by words. Here is how we do that.
w - Moves the cursor forward one word
b - Moves the cursor backwards one word. If the cursor is in between of a word, it moves to the starting of the word.
e - Moves the cursor to the end of the word.

You can also add number prefix with this commands to move the cursor to a specified amount of words. So, 5w will move the cursor to 5 words forward and so on.

Now, if you have noticed that vi editor is quite case sensitive (remember we used ZZ to save our file and not zz). So, if you try W B E instead of w b e, you will notice that, these commands will ignore punctuation. Here also you can add number prefix to move forward or backward more than one word in a line.
Now these are the shortcuts for moving through characters and words. Next we'll check out shortcuts for moving through lines.

$ - will move the cursor to the end of the line
0 - will move the cursor to the beginning of the line

With lots of moving around, you may forget where you  started or where you are. So, you need the information to be shown. You have three options to do that,

  1. Already discussed about :set number command, you can use this command
  2. You can use Ctrl + g combination to get the information in the bottom left corner
  3. Use :set ruler command to show the current position of cursor in the bottom right corner
Now we'll check where the file is really long and we need to jump around from here to there. So, we'll be needing larger file. So, I would request the reader to again work on their typing skill and write the same content to make it 150 lines of same text, "A quick brown fox jumps over the lazy dog.".

Too much of typing, right ?
Yes I know. But it will help you in future. Even I am typing all these. So why can't you ?

Take a break, have some coffee, may be walk around for some time and come back once fresh. Type another 100 lines and come back. In the mean time let me write on the next part of this series.

Happy typing...

vi editor: Programmers' choice - Part 3

Now we have a pretty large file containing 150 lines and 6450 characters. It will surely go beyond a single screen to display the file. At this time, this may be a difficult job to read the file.

We can try h j k l combination to read the file but it is indeed pretty awkward to do so.

To save us from this manual and irritating job, vi has blessed us with some more cool commands which we will try here. Again remember all of this post works in Command Mode and not in Insert mode.

First, open the file in vi editor and use :set number to show the line numbers.

Let's start to jump on a particular line in this large text file.
G - moves to the last line of the file. No matter how long the file is, it will do its job pretty quickly.

Once you are in the last line, you may need to go to the beginning of the file or anywhere in between. So, let's try this one by one.

1G - moves the cursor to the first of the file
20G - moves the cursor to the first character of line  number 20
30G - moves the cursor to the first character of line  number 30

So, you get the idea, <number>G will move the cursor to line number <number>. While this is very handy, we can do the same using the following command as well.

:1
:20
:30

So :<number> will move the cursor to the beginning of line number <number>.
Now what if you've used a number that is larger than the total number of lines in the file (for our file we can try 160, 170, 1000 etc.).

Actually this will take you to the beginning of the last line.

So, in our case 160G, 170G, 100G, G, :160, :170, :1000 yields the same result.

Next we want to move through line. Following are some commands that will help you.

- (hyphen) - moves the cursor to the beginning of the previous line.
+ - moves the cursor to the beginning of the next line.

In many cases, you may not know the exact position of a particular portion of text and you also don't want to scroll through line by line or you are reading a large file and you want to move through screens. These are pretty common scenario when you are reading a log file or reading a beautiful long story in a text file. In these scenarios, you simply want to go to the next screen or previous screen.

vi comes with four commands

Ctrl-f scrolls down one screen
Ctrl-b scrolls up one screen
Ctrl-u scrolls up a half a screen
Ctrl-d scrolls down a half a screen

This one really needs some screenshots to understand. Let me show you.
Initially I am at the beginning of first line and my screen contains 41 lines of text at once.


Now I am entering Ctrl + f 

Now my screen contains lines 40 to line 80. This screen contains two lines from the previous one to have some trail of text.

Other combination should be tried by you on your own. Also note that, the lines on the screen and scroll amount depends on. So, whatever, I am getting, may not be same for you.

Also, all these four combination works with a number prefix and by now you may have guessed what it does for you.

Now, on the same screen you may need to move around anywhere While in this case vi helps a little bit.
While you always have the option to use <number>G combination, vi also helps with the following three commands,

H moves the cursor to the top line of the screen.
M moves the cursor to the middle line of the screen.
L moves the cursor to the last line of the screen.

Now pretty uncommon scenario but still you may need to scroll just a single line of the file. Following are two commands that you can use,

Ctrl+y - forward scrolls one line
Ctrl+e - backward scrolls one line

These were pretty common basic cursor movement commands. Get a good hold of them. Because, if you are on UNIX/Linux, pretty common that you'll be using vi editor and these commands will save you a lot of time.

That was all the basic commands used for cursor movement and you need to practice for a while and you will make it to a level where you can use these commands efficiently to ease your work. In the next article, we'll start with insert mode.

What and why UNIX ?