7:49 PM Key mapping inside VI editor |
Its very strange and delightful to see that when you press F9 in side vi editor, a backup copy of the file is created or when you press F8, the xml file is formatted in readable form. It is always useful when we map certain keys usually function keys inside VI editor to make our work easy. In this very article, we shall learn how to map keyboard keys inside vi editor to execute certain commands. The basic syntax to create mapping is:
If you want to know what all functions the keys are currently mapped to, you can use ":map" comand inside vi editor. It will tell you the mapping in escape mode. To check mapping in insert mode, we use ":map!" For Eg. The function key F9 is mapped to call the function takeBackup() to create a backup of the current file with currrent system time. The function is explained in detail below
To display current system time in VI editor.
Examples: 1. Lets say we want to map a fucntion key to call a fucntion that takes a backup copy of the file currently open in VI editor. The command for the same is;
2. We want to print the current system time in VI editor, if we press F2 key. The command for the same is:
3. If you want to create mapping where in insert mode, we press certain key, it will type some text to buffer.
So, whenever you press F3 in insert mode it will type "Shankar Kumar Bhagat" at the current cursor position.
If you want to keep the mapping active only for current VI session, these commands inside vi editor are enough.
If you want to make the key mapping permanent in vi editor for all sessions, you have to save the mapping in a file in your home directory.
While adding the key mapping into a file we dont need to add ":" at the beginning of the command.
To remove the key mapping, we use unmap command :unmap <F2> To remove mapping for the key F2 in normal mode
add The search string will not be echoed when using this mapping.
takeBackupCopy() function
:echo "Source Filename: ".expand("%:p")
You can assign the source file name and target filename to variables using let, So, the complete function takeBackupCopy() function is as below:
|
|
Related blogs
You may also like to see:
[2015-01-18] | [Open System-Linux] |
The JAR archiving tool in Linux |
[2014-06-05] | [Open System-Linux] |
SCRIPT command: Save your Linux sessions automatically |
[2016-10-12] | [Open System-Linux] |
Remove Control M characters from a script or any file in Linux/AIX |
[2017-08-13] | [Open System-Linux] |
Perl trick to remove Ctrl - M charater from a file |
[2014-09-24] | [Open System-Linux] |
Set, unset, display shell behaviour options : SHOPT command in Linux |
Total comments: 0 | |