Home » 2014 » December » 22 » Key mapping inside VI editor

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:

cmd attribute lhs rhs

  • cmd           :map, :map!, :unmap, :unmap!,
  • attribute     <silent>, <buffer>, <script>
  • lhs            The key that we will use for mapping
  • rhs           The command or action that the key will execute

  • To list all the currently set mapping inside vi editor:

:map
:map!

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.
 <F9>   :call takeBackup() <CR>   

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


<F2> :echo 'Current system time:'. strftime('%c') <CR>  

To display current system time in VI editor.

  • To map a key to certain command or action:

:map {lhs} {rhs} ---NORMAL MODE
:MAP! {lhs} {rhs}  --Insert or command mode.


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;

:map <F9> :call takeBackup() <CR>

2. We want to print the current system time in VI editor, if we press F2 key. The command for the same is:

:map <F2> :echo 'Current system time:'. strftime('%c') <CR>

3. If you want to create mapping where in insert mode, we press certain key, it will type some text to buffer.
The command to achieve the same is:

:map! <F3> Shankar Kumar Bhagat <CR>

 

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.
Once you exit from the vi editor, setting of key mapping is lost.

 

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.
The file could be $HOME/.vimrc or $HOME/_vimrc or $VIM/vimrc.


So when we place the same commands in the .vimrc file, the mapping becomes permanent.
Also, you can place the function to be called in the mapping in the same .vimrc file



For eg.
We are placing below lines in .vimrc file and save the file:

map <F3> :call TestFunction() <CR>

fun! TestFunction()
         echo 'Hello world from test fuction'
endfun


Now whenever we press the function key F3 inside Vi editor, we get the string 'Hello world from test fuction'  and this is permanent.
Even if you quit from Vi editor, logout the session, login again and open a VI editor, the key mapping will persist.

While adding the key mapping into a file we dont need to add ":" at the beginning of the command.


 

  • To remove key mapping in VI editor.

To remove the key mapping, we use unmap command

:unmap <F2> To remove mapping for the key F2 in normal mode
:unmap! <F2>  To remove mapping for the key F2 in command mode or insert mode


To define a mapping which will not be echoed on the command line,

add
"<silent>" as the first argument. 
Example: >
        :map <silent> h /searchkey <CR>
So when we press the key 'h' it will search for the 'searchkey' in the vi editor.

The search string will not be echoed when using this mapping. 



Messages from the executed command are still given though.  To shut them up too, add a ":silent" in the executed command: >
        :map <silent> h :exe ":silent normal /seaarchkey\r"<CR>


takeBackupCopy() function

  • To get the file name of a file which is open in vi editor, use below command inside VI editor:

:echo "Source Filename: ".expand("%:p")

  • To create a target file name with current time stamp, use below command:

:echo "Target filename: ".expand("%:p").".".strftime("%Y%m%d%H%M")

  • To execute a command as string use system command.

:echo 'Copying '.system("cp -pv " .expand("%:p")." ". expand("%:p").".".strftime("%Y%m%d%H%M"))

You can assign the source file name and target filename to variables using let, So, the complete function takeBackupCopy() function is as below:

fun! takeBackupCopy()
         let srcfile=expand("%:p")
         let targetfile=expand("%:p").".".strftime("%Y%m%d%H%M")
         let cpcmd=system("cp -pv ".srcfile. " ".targetfile)
         echo "Copying :".cpcmd
endfun

 

 

 

 
 

Category: Open System-Linux | Views: 1396 | Added by: shanky | Tags: Use function keys inside vi editor, Vi, key mapping in Unix, map keys inside vi editor, key mapping in vi editor, map, key mapping in Linux | Rating: 0.0/0

Related blogs


You may also like to see:


[2014-10-24][Open System-Linux]
HEXDUMP command in Linux to display the file content in decimal, hexadecimal, octal format
[2015-07-12][Open System-Linux]
EGREP or extended grep in Linux to search patterns
[2015-06-03][Open System-Linux]
STAT command : check file or filesystem statistics
[2015-06-16][Open System-Linux]
Alien command : convert files from one form to another, install binary packages
[2015-03-18][Open System-Linux]
What is runlevel in Linux?

Total comments: 0
ComForm">
avatar