Tuesday, 31 May 2016

Working with Standard Input, Output & Error in Linux

Every process in Linux is provided with three open files( usually called file descriptor). These files are the standard input, output and error files. By default :
  • Standard Input is the keyboard, abstracted as a file to make it easier to write shell scripts.
  • Standard Output is the shell window or the terminal from which the script runs, abstracted as a file to again make writing scripts & program easier
  • Standard error is the same as standard output:the shell window or terminal from which the script runs.
A file descriptor is simply a number that refers to an open file. By default , file descriptor 0 (zero) refers to the standard input & often abbreviated as stdin. File descriptor 1 refers to standard output (stdout) and file descriptor 2 refers to standard error (stderr). These numbers are important when you need to access a particular file , especially when you want to redirect these files to the other locations, File descriptors numbers go up from zero.

Redirecting Standard Output

Syntax to redirect the output of a command to a file.
Command_options_and_arguments > output_file
Example :
linuxtechi@localhost:~$ cat /proc/cpuinfo > command.txt
We can see the data that would have gone to the screen with more command :
linuxtechi@localhost:~$ more command.txt 
processor         : 0
vendor_id         : GenuineIntel
cpu family        : 6
model             : 37
model name        : Intel(R) Core(TM) i3 CPU       M 370  @ 2.40GHz
stepping          : 5
microcode         : 0x616
cpu MHz           : 0.000
cache size        : 6144 KB
physical id       : 0
siblings          : 2
core id           : 0
cpu cores         : 2
apicid            : 0
initial apicid    : 0
fpu               : yes
fpu_exception     : yes
cpuid level       : 5
wp                : yes
The > operator tells the shell to redirect the output of the command to the given file. If the file exists , the deletes the old contents of the file and replaces it with the output of the command.

Redirecting a Command’s Input :

Syntax to redirect the input of a command to come from a file.
# Command_options_and_arguments < input_file
Use the < operator to redirect the input for a command , example is shown below :
linuxtechi@localhost:~$ wc -l < command.txt
52
In this example , the input to the ‘wc‘ command comes from the file named command.txt. The shell sends the contents of the file command.txt as a standard input for the wc command.
Note : We can also combine both redirections with following syntax :
# command_options_and_agruments < input_file > output_file.

Redirecting Standard Error :

In addition to redirecting the standard input and output for a script or a command, we can also redirect standard error. Even though standard error by defaults goes to the same place as the standard output – the shell window or terminal. There are good reasons why stdout and stderr are treated separately. The main reason is that we can redirect the output of a command or commands to a file but you have no way of knowing whether an error occurred. Separating stderr from stdout allows the error message to appear on your screen while output still goes to a file.
Syntax to redirect stderr from a command to a file.
# command_options_and_agruments 2> output_file.
The 2 in 2> refers to the file descriptor 2, the descriptor number for stderr.
Example:
linuxtechi@localhost:~$ lsash /usr/bin 2> commands-error.txt
linuxtechi@localhost:~$ cat commands-error.txt
No command 'lsash' found, did you mean:
Command 'sash' from package 'sash' (universe)
lsash: command not found

Redirecting both Standard Ouput & Standard Error.

Use 2>&1 Syntax to redirect standard error to the same location as standard output .
Example:1
linuxtechi@localhost:~$ ls /usr/bin > command.txt 2>&1
Above Command has three parts.
  • ls /usr/bin is the command run
  • > command.txt redirects the output of the ls command
  • 2>&1 sends the output of the file descriptor 2, stderr , to the same location as the file descriptor 1, stdout.
Example: 2
linuxtechi@localhost:~$ ls /usr2222/bin > command.txt 2>&1
linuxtechi@localhost:~$ more command.txt
ls: cannot access /usr2222/bin: No such file or directory
Note that above example assumes that your system doesn’t have directory names “/usr2222/bin”

Redirecting Both stderr & stdout at Once

linuxtechi@localhost:~$ ls /usr2222/bin &> command.txt
linuxtechi@localhost:~$ more command.txt
ls: cannot access /usr2222/bin: No such file or directory
In the above command ls is the command , /usr2222/bin is the argument to the ‘ls‘ command and ‘&> command.txt‘ redirect both stdout and stderr to a file named command.txt.

Appending To Files

Use the ‘>>’ operator to redirect the output of a command , but append to the file , if it exists. The syntax is given below :
# Command >> file_to_append.
Example:
linuxtechi@localhost:~$ uptime >> sysload.txt
linuxtechi@localhost:~$ uptime >> sysload.txt
linuxtechi@localhost:~$ uptime >> sysload.txt
linuxtechi@localhost:~$ more sysload.txt
11:49:17 up 1:22, 3 users, load average: 0.28, 0.12, 0.11
11:49:28 up 1:22, 3 users, load average: 0.40, 0.15, 0.12
11:49:36 up 1:23, 3 users, load average: 0.33, 0.14, 0.12

Truncating Files :

We can use a shorthand syntax for truncating files by omitting the command before > operator . The Syntax is given below :
# > file_name
We can also use an alternate format with a colon :
# : > file_name
Both of these command-less command will create the file if it does not exist and truncate the file to zero bytes if the file does exist.
linuxtechi@localhost:~$ ls /usr/bin > command.txt
linuxtechi@localhost:~$ ls -l command.txt
-rw-rw-r-- 1 linuxtechi linuxtechi 19713 Dec 2 12:18 command.txt
linuxtechi@localhost:~$ > command.txt
linuxtechi@localhost:~$ ls -l command.txt
-rw-rw-r-- 1 linuxtechi linuxtechi 0 Dec 2 12:18 command.txt

Sending Ouput to Nowhere Fast

There are some scenarios where you not only want to redirect the output of a command , you want to throw the output away. You can do this by redirecting a command’s output to the null file “/dev/null” The null file consumes all output sent to it , as if /dev/null is a black hole star.
linuxtechi@localhost:~$ ls /usr/bin > /dev/null
Note : The file /dev/null is often called a bit bucket.

Sunday, 29 May 2016

Command Line Arguments in Linux Shell Scripting

Overview :

Command line arguments (also known as positional parameters) are the arguments specified at the command prompt with a command or script to be executed. The locations at the command prompt of the arguments as well as the location of the command, or the script itself, are stored in corresponding variables. These variables are special shell variables. Below picture will help you understand them.
command-line-arguments
command-line-shell-variables
Let’s create a shell script with name “command_line_agruments.sh”, it will show the command line argruments that were supplied and count number of agruments, value of first argument and Process ID (PID) of the Script.
linuxtechi@localhost:~$ cat command_line_agruments.sh
command-line-agruments
Assign Executable permissions to the Script
linuxtechi@localhost:~$ chmod +x command_line_agruments.sh
Now execute the scripts with command line argruments
linuxtechi@localhost:~$ ./command_line_agruments.sh Linux AIX HPUX VMware
There are 4 arguments specified at the command line.
The arguments supplied are: Linux AIX HPUX VMware
The first argument is: Linux
The PID of the script is: 16316
Shifting Command Line Arguments
The shift command is used to move command line arguments one position to the left. During this move, the first argument is lost. “command_line_agruments.sh” script below uses the shift command:
linuxtechi@localhost:~$ cat command_line_agruments.sh
command-line-agrument-shift
Now Execute the Script again.
linuxtechi@localhost:~$ ./command_line_agruments.sh Linux AIX HPUX VMware
There are 4 arguments specified at the command line
The arguments supplied are: Linux AIX HPUX VMware
The first argument is: Linux
The Process ID of the script is: 16369
The new first argument after the first shift is: AIX
The new first argument after the second shift is: HPUX
linuxtechi@localhost:~$
Multiple shifts in a single attempt may be performed by furnishing the desired number of shifts to the shift command as an argument.

Vim Tutorial

1. Introduction

This tutorial has been written for both vi and vim. It starts with real basics, such as cursor navigation and ends with more advanced techniques like merging files.
For every section of this tutorial there is a short video with hints to help you understand how vim / vi works. Even that I have divided this tutorial into parts from novice to the expert user, there is plenty more what vim can do to make your work with vim editor easier and more efficient.
However completing this tutorial you will give sufficient knowledge about vim / vi and its features for your daily tasks.

2. Vim Novice level ( Vim Basics )

2.1. Moving cursor around

 move vim text editor cursor left with h, move vim text editor cursor right with l, move vim text editor cursor up with k ,move vim text editor  cursor down with j
In vim you can move cursor around with following keys h, l, k, j which is left, right, up and down respectively.
You can move cursor around also with arrow keys, however this is possible only if they are available.
Vim was designed for all kinds of terminals where arrow keys may not be available for you. Moreover, once you get used to using vim with h, l, k, j you will move more quickly than using arrow keys.
Open some text file and try use above keys now:
vim yourfile.txt
NOTE: You do not have to create a file prior to execution of the above command. If the file does not exists it will be created. If the file does exists it will be opened.
Vim Novice level summary:At this point of this vim tutorial you have learned:
Moving around with cursor:
h key = LEFT, l key = RIGHT, k key = UP, j key = DOWN

2.2. Exiting from vim

At the moment, you know how to open a file with vim and how to navigate vim cursor around. What you now need to know is how to exit from vim editor without saving changes. Vim editor has two modes:
  • command mode
  • editing mode
Command mode is the one where we can instruct vim editor to exit to the command line ( shell ). To do that we need press ESC and type :q!. Play video and watch bottom of the video screen for entered commands.
Vim Novice level summary:At this point, of this vim tutorial you have learned:
  • Moving around with cursor:
    h key = LEFT, l key = RIGHT, k key = UP, j key = DOWN
  • Exiting vim editor without saving:
    press ESC to get into command mode, enter :q! to exit.

2.3. Character Deletion

In this section you will learn how to delete characters in a vim command mode. To delete character in vim, you need to position cursor on the character you are intent to delete and press x. Remember you need to be in a command mode! If unsure press ESC key.
Vim Novice level summary:At this point of this vim tutorial you have learned:
  • Moving around with cursor:
    h key = LEFT, l key = RIGHT, k key = UP, j key = DOWN
  • Exiting vim editor without saving:
    press ESC to get into command mode, enter :q! to exit.
  • Deleting characters in vim command mode:
    delete character with x key

2.4. Inserting Text

Well, you have already learned couple things about vim! Now its time to insert some text. In order to insert some text you need to switch vim into an editing mode. To get into editing mode press, i when in a vim command mode. -- INSERT -- key word at the bottom will indicate that you are in an inserting mode. See video for demonstration. When you become familiar with i ( insert mode ) use instead i letter a ( append ) and see what it does !

Vim Novice level summary:At this point, of this vim tutorial you have learned:
  • Moving around with cursor:
    h key = LEFT, l key = RIGHT, k key = UP, j key = DOWN
  • Exiting vim editor without saving:
    press ESC to get into command mode, enter :q! to exit.
  • Deleting characters in vim command mode:
    delete with x key
  • Inserting / appending text:
    Press i or a in command mode and type

2.5. Saving edited file

Now you know, have to move cursor, delete characters insert text and exit file without changes with :q! vim command. Exiting vim editor without changes does not always produce desired outcome. To save changes for file you are editing use :wq command in vim command mode. There is also quicker way to do it, which is by key strokes SHIFT+zz . Watch video for demonstration of both :wq and SHIFT+zz commands. Remember you need to be in vim's command mode to execute those commands !

Vim Novice level summary:At this point, of this vim tutorial you have learned:
  • Moving around with cursor:
    h key = LEFT, l key = RIGHT, k key = UP, j key = DOWN
  • Exiting vim editor without saving:
    press ESC to get into command mode, enter :q! to exit.
  • Deleting characters in vim command mode:
    delete with x key
  • Inserting / appending text:
    Press i or a in command mode and type
  • Saving changes and exit:
    in command mode :wq or SHIFT+zz

2.6. VIM novice level Summary

  • Moving around with cursor:
    h key = LEFT, l key = RIGHT, k key = UP, j key = DOWN
  • Exiting vim editor without saving:
    press ESC to get into command mode, enter :q! to exit.
  • Deleting characters in vim command mode:
    delete with x key
  • Inserting / appending text:
    Press i or a in command mode and type
  • Saving changes and exit:
    in command mode :wq or SHIFT+zz

3. Vim Operators and Motions

3.1. Deleting Words

Let's see if we can also make vim delete words instead of deleting characters one by one! Vim has for deleting words very intuitive command dw which means I guess Delete Word. Let's tryto delete some words. In order to do so navigate cursor at the beginning of the word you want to delete and enter command dw in a command mode.

Vim Operators and Motions summary:At this point of this vim tutorial you have learned:
  • Deleting words:
    delete word with dw command

3.2. Delete to the end of the line

In fact, the vim editor in the previous example did delete all characters from your cursor to the first space. That is the reason that you need to navigate to the beginning of the word you want to delete. With command d$ you can instruct vim to delete all characters to the end of the line. Place you cursor anywhere in the text and enter d$ command. Remember you need to bein a vim command mode.
From now onwards we will refer to d as an operator and to w or $ as a motion. There are many other operators and motions, for example you may try what d operator with e motion does.

Vim Operators and Motions summary:At this point of this vim tutorial you have learned:
  • Deleting words:
    delete word with d operator and w or e motion
  • Deleting to the end of the line:
    delete to the end of the line with d operator and $ motion

3.3. Motions and count number

When in a command mode and entering motion key e your cursor will be placed to the end of the word. When entering motion key w your cursor will be placed at the beginning of the following word. To do some magic trick you can use count number before motion key, which will multiply your action. For example, to jump to the end of the 8th word you will enter count 8 and motion e thus: 8e . Try it and also try to play with w motion and count number. If you want to navigate to the end of the line it would be silly to count words and then enter count and motion key-strokes. To navigate to the end of the line use $ motion and to navigate vim to the beginning of the line enter 0 motion.
Vim Operators and Motions summary:At this point, of this vim tutorial you have learned:
  • Deleting words:
    delete word with d operator and w or e motion
  • Deleting to the end of the line:
    delete to the end of the line with d operator and $ motion
  • Using operators, motions and counts:
    beginning of the line 0, end of the line $, end of the 2nd word 2e beginning of the 4th word 4w

3.4. Deleting multiple words

No you have seen how to jump over multiple words by using vim's motions with combination of counts. Let's combine this acquired knowledge with operator like d ( delete ). For example, to delete 4 words we can use command d4w. It is very easy to remember this just take first characters from words "Delete 4 Words" and you get your command d4w.
Vim Operators and Motions summary:At this point, of this vim tutorial you have learned:
  • Deleting words:
    delete word with d operator and w or e motion
  • Deleting to the end of the line:
    delete to the end of the line with d operator and $ motion
  • Using operators, motions and counts:
    beginning of the line 0, end of the line $, end of the 2nd word 2e beginning of the 4th word 4w
  • Deleting multiple words:
    to delete 3 words you would use d3w

3.5. Deleting lines

Vim is also capable to delete lines. To delete single line you can use dd command. If your intention is to delete multiple lines add count number in front of the actual command. For example, to delete 100 you can use command 100dd. Remember that the first line which will be deleted, is the one with your cursor on it.!
Vim Operators and Motions summary:At this point, of this vim tutorial you have learned:
  • Deleting words:
    delete word with d operator and w or e motion
  • Deleting to the end of the line:
    delete to the end of the line with d operator and $ motion
  • Using operators, motions and counts:
    beginning of the line 0, end of the line $, end of the 2nd word 2e beginning of the 4th word 4w
  • Deleting multiple words:
    to delete 3 words you would use d3w
  • Deleting lines:
    to delete single line dd, delete n lines ndd

3.6. Vim undo command

You have already learned how to delete characters, words as well as whole lines. With all this processing power you are very likely to make a mistake. Let's say you wanted to delete just 2 lines not 4. Simple help comes in form of vim's undo command u. Once you have made a mistake go to command mode and press u to undo your previous changes.
Vim Operators and Motions summary:At this point of this vim tutorial you have learned:
  • Deleting words:
    delete word with d operator and w or e motion
  • Deleting to the end of the line:
    delete to the end of the line with d operator and $ motion
  • Using operators, motions and counts:
    beginning of the line 0, end of the line $, end of the 2nd word 2e beginning of the 4th word 4w
  • Deleting multiple words:
    to delete 3 words you would use d3w
  • Deleting lines:
    to delete single line dd, delete n lines ndd
  • Undo changes:
    undo changes with u

3.7. VIM Operators and Motions summary

  • Deleting words:
    delete word with d operator and w or e motion
  • Deleting to the end of the line:
    delete to the end of the line with d operator and $ motion
  • Using operators, motions and counts:
    beginning of the line 0, end of the line $, end of the 2nd word 2e beginning of the 4th word 4w
  • Deleting multiple words:
    to delete 3 words you would use d3w
  • Deleting lines:
    to delete single line dd, delete n lines ndd
  • Undo changes:
    undo changes with u

4. Vim apprentice user level

4.1. Paste command

Congratulations, you have reached apprentice user level in using vim text editor! Let's continue this vim tutorial with paste command. When deleting lines, words or characters with d command, vim actually saves them into cache memory for later use. Having said that, you can also think of d command as cut. To use your cache memory in this concept you can use p ( paste/put)command. Try delete line with dd command, move you cursor where you intent to insert this and hit key p to paste it.
Vim apprentice user summary:At this point of this vim tutorial you have learned:
  • Paste command:
    paste your cache memory with p command

4.2. VIM replace operator

Replacing characters with vim editor is a very easy task. Simply get vim to the editing mode, remove character and insert replacement character. Well, this approach is logical but, there is a way to do it faster with r ( replace ) operator. Move you cursor on the character you would like to replace first enter r command followed by character which should be there instead.rt command will replace current character with t.
Vim apprentice user summary:At this point of this vim tutorial you have learned:
  • Paste command:
    paste your cache memory with p command
  • Replace characters:
    rt replace current character with t

4.3. VIM change operator

Vim's replace operator allows us to replace single o multiple characters with a single character type. To multiple characters you can use a change operator. If you would like to change a single word you can use command ce.
Vim apprentice user summary:At this point of this vim tutorial you have learned:
  • Paste command:
    paste your cache memory with p command
  • Replace characters:
    rt replace current character with t
  • Change characters:
    ce to change single word, c$ to change to the end of the line

4.4. VIM apprentice user summary

  • Paste command:
    paste your cache memory with p command
  • Replace characters:
    rt replace current character with t
  • Change characters:
    ce to change single word, c$ to change to the end of the line

5. Vim Experienced user level

5.1. VIM advanced navigation

In many cases you will deal with large files which can contain thousands of lines. To navigate vim cursor to line 3500 with j key will take you whole morning including your lunch break. Fortunately vim developers equipped vim with g an operator. This is how you use it. To get to the end of the file hit G ( SHIFT+g ), to get to the beginning of the file use gg command, to navigate you cursor on n line use nG. If you are unsure how many lines your file has, you can use a CTRL+g key combination.

Vim experienced user summary:At this point of this vim tutorial you have learned:
  • Advanced Navigation:
    end of the file G, beginning of the file gg or 1G, to get on line n use nG
    instruct vim display file information CTRL+g

5.2. Searching text with vim

Navigating with vim's g operator is easy, but what if you do not know what line you need to edit? In this case vim allows you to search for a given keyword or phrase. You can use / or ? characters to search for keywords or phrases. /edit will search for a key word edit in forward direction. In case you use ?edit vim will search for key word edit in backward direction. You text may include more keywords, to search all of them keep pressing n key ( next ) or for backward search use N ( SHIFT+n )key.
Vim experienced user summary:At this point, of this vim tutorial you have learned:
  • Advanced Navigation:
    end of the file G, beginning of the file gg or 1G, to get on line n use nG
    instruct vim display file information CTRL+g
  • Search text with vim:
    search forward /, search backward ?, next search n , previous search N

5.3. Vim Substitution

Vim can replace given a pattern keyword throughout whole text. Vim also gives you an option to replace your pattern within certain range. This process is called substitution. To replacefirst occurrence of keyword bash with perl in your document you can use command :s/bash/perl/. If there is a more then one keyword of bash on the same line you can use command :s/bash/perl/g. This command can be used to replace keyword bash with perl in whole document: :%s/bash/perl/g . There may be a situation where you need to substitute keyword in certain line range only. In this case you replace % with line range. :20,40s/bash/perl/g this will substitute bash for perl only between lines 20 and 40.
Vim experienced user summary:At this point, of this vim tutorial you have learned:
  • Advanced Navigation:
    end of the file G, beginning of the file gg or 1G, to get on line n use nG
    instruct vim display file information CTRL+g
  • Search text with vim:
    search forward /, search backward ?, next search n , previous search N
  • Substitution :
    first occurrence single line :s/bash/perl/
    all occurrences single line :s/bash/perl/g
    first occurrence between line range: :23,100s/bash/perl/
    all occurrences between line range: :23,100s/bash/perl/g
    first occurrence in whole text: :%s/bash/perl/
    all occurrences whole text: :%s/bash/perl/g all occurrences whole text plus confirmation: :%s/bash/perl/gc

5.4. VIM experienced user summary

  • Advanced Navigation:
    end of the file G, beginning of the file gg or 1G, to get on line n use nG
    instruct vim display file information CTRL+g
  • Search text with vim:
    search forward /, search backward ?, next search n , previous search N
  • Vim Substitution :
    first occurrence single line :s/bash/perl/
    all occurrences single line :s/bash/perl/g
    first occurrence between line range: :23,100s/bash/perl/
    all occurrences between line range: :23,100s/bash/perl/g
    first occurrence in whole text: :%s/bash/perl/
    all occurrences whole text: :%s/bash/perl/g

6. Vim Veteran user level

6.1. Execute external commands on shell

Vim editor also allows you to execute external commands while editing files. : followed by the ! allows you to do that trick. :!date will execute date command on the shell.
Vim Veteran user summary:At this point, of this vim tutorial you have learned:
  • Execute external commands on shell from vim:
    :!ls will execute ls command on your shell

6.2. More options to write a file

For some reason, you may need to save your current progress on editing file to some different file, perhaps as a backup, or you just want to save current progress without quitting a vimeditor. :w command is used to save file without quitting vim. If you would like to save your current progress to some different file, for example, temp.txt you will do it with command :w temp.txt.
Vim Veteran user summary:At this point of, this vim tutorial you have learned:
  • Execute external commands on shell from vim:
    :!ls will execute ls command on your shell
  • Writing to files advanced:
    :w saves current file without quit, :w bash.sh writes to file bash.sh

6.3. Save selected text to the file

Let's say that you are in the situation where you have no mouse and no other windows available, and you need to selected text from currently opened file and save it to another file. This situation often happens with remote ssh / telnet logins. In this case you would use a v operator and highlight text with navigation keys, followed by :w to save selected text to different file.
Vim Veteran user summary:At this point, of this vim tutorial you have learned:
  • Execute external commands on shell from vim:
    :!ls will execute ls command on your shell
  • Writing to files advanced::w saves current file without quit, :w bash.sh whites to file bash.sh
  • Highlight text and save to different file:highlight text with v operator and save ti with :w <yourfile>

6.4. Retrieve content from file

Vim also allows you to append / merge content of one file into your currently opened file. For this you can use a r operator. :r file.txt will retrieve content of the file.txt and insert it to your currently opened file. The place where the text will be inserted depends on you cursor position.
Vim Veteran user summary:At this point, of this vim tutorial you have learned:
  • Execute external commands on shell from vim:
    :!ls will execute ls command on your shell
  • Writing to files advanced::w saves current file without quit, :w bash.sh whites to file bash.sh
  • Highlight text and save to different file:highlight text with v operator and save it with :w <yourfile>
  • Retrieve text from different file::r <yourfile> will retrieve content of other file

6.5. VIM veteran user summary

  • Execute external commands on shell from vim:
    :!ls will execute ls command on your shell
  • Writing to files advanced::w saves current file without quit, :w bash.sh whites to file bash.sh
  • Highlight text and save to different file:highlight text with v operator and save it with :w <yourfile>
  • Retrieve text from different file::r <yourfile> will retrieve content of <yourfile> file

7. Vim Expert user level

7.1. Using open operator

Switching vim into an editing mode with i (insert) or a (append) operator is easy. However, sometimes you may find it easier to use an o operator. o operator inserts new line below yourcursor and switch vim into the editing mode. O (SHIFT+o) operator inserts new line above your cursor and switch vim into editing mode.
Vim Veteran user summary:At this point, of this vim tutorial you have learned:
  • Using o operator:
    :o insert line bellow you cursor, O inserts line above your cursor

7.2. Copy and Paste

Previously, we have seen how we can use d ( delete ) operator to delete line and paste it to another location within file. This time we will do the same but instead of deleting line, wewill copy it and paste it. The principle is the same as with a d operator but in this case we will use y ( yank ) operator.
Vim Veteran user summary:At this point, of this vim tutorial you have learned:
  • Using o operator:
    :o insert line bellow you cursor, O inserts line above your cursor
  • Copy and paste:
    yank line with y and paste it with p

7.3. Customize vim's environment

Vim is extended version of its older brother vi and this part of the tutorial applies only to vim. Every time you start up vim it reads a .vimrc file from you home directory. In .vimrc file you can set up a background, font color and many other different settings which are beyond this tutorial.

Vim Veteran user summary:At this point, of this vim tutorial you have learned:
  • Using o operator:
    :o insert line bellow you cursor, O inserts line above your cursor
  • Copy and paste:
    yank line with y and paste it with p
  • Customize vim's environment:
    edit ~/.vimrc file to customize vim's environment

Congratulations !
You are now expert in using vim text editor and your expert level feedback on this vim tutorial is much appreciated: lubos(at)linuxconfig.org

8. Vim Tutorial Summary

8.1. VIM novice level Summary

  • Moving around with cursor:
    h key = LEFT, l key = RIGHT, k key = UP, j key = DOWN
  • Exiting vim editor without saving:
    press ESC to get into command mode, enter :q! to exit.
  • Deleting characters in vim command mode:
    delete with x key
  • Inserting / appending text:
    Press i or a in command mode and type
  • Saving changes and exit:
    in command mode :wq or SHIFT+zz

8.2. VIM Operators and Motions summary

  • Deleting words:
    delete word with d operator and w or e motion
  • Deleting to the end of the line:
    delete to the end of the line with d operator and $ motion
  • Using operators, motions and counts:
    beginning of the line 0, end of the line $, end of the 2nd word 2e beginning of the 4th word 4w
  • Deleting multiple words:
    to delete 3 words you would use d3w
  • Deleting lines:
    to delete single line dd, delete n lines ndd
  • Undo changes:
    undo changes with u

8.3. VIM apprentice user summary

  • Paste command:
    paste your cache memory with p command
  • Replace characters:
    rt replace current character with t
  • Change characters:
    ce to change single word, c$ to change to the end of the line

8.4. VIM experienced user summary

  • Advanced Navigation:
    end of the file G, beginning of the file gg or 1G, to get on line n use nG
    instruct vim display file information CTRL+g
  • Search text with vim:
    search forward /, search backward ?, next search n , previous search N
  • Vim Substitution :
    first occurrence single line :s/bash/perl/
    all occurrences single line :s/bash/perl/g
    first occurrence between line range: :23,100s/bash/perl/
    all occurrences between line range: :23,100s/bash/perl/g
    first occurrence in whole text: :%s/bash/perl/
    all occurrences whole text: :%s/bash/perl/g

8.5. VIM veteran user summary

  • Execute external commands on shell from vim:
    :!ls will execute ls command on your shell
  • Writing to files advanced::w saves current file without quit, :w bash.sh whites to file bash.sh
  • Highlight text and save to different file:highlight text with v operator and save it with :w <yourfile>
  • Retrieve text from different file::r <yourfile> will retrieve content of <yourfile> file

8.6. VIM expert user summary

  • Using o operator:
    :o insert line bellow you cursor, O inserts line above your cursor
  • Copy and paste:
    yank line with y and paste it with p