VIM: Binding a shortcut

 All this time, I do not know how to create my own VIM Bindings. And yes, trying yourself is the only way you learn anything.

I want to explain what I have learnt on creating a vim shortcut, so you might get a good headstart.

The command I am going to explain is as below


map <C-b> :w\|!ruby spec/example_spec.rb <cr>

so

  • map: is like you are mapping <custome_key> <series of actions>
  • <C-b>: this stands for CTRL+b 

If you want to use Shift, Alt, Cmd(Mac), yes VIM have options for all of that.

```

S-...>		shift-key			*shift* *<S-*

<C-...>		control-key			*control* *ctrl* *<C-*

<M-...>		alt-key or meta-key		*meta* *alt* *<M-*

<A-...>		same as <M-...>			*<A-*

<D-...>		command-key (Macintosh only)	*<D-*
```
Vim documentation: intro (sourceforge.net)
  • Then :w\|!ruby spec/example_spec.rb<cr>
    • :w is save
    • \|: is like you are joining this command with with another action.
    • !ruby spec/example_spec.rb
      • ! is for invoking shell commands, for example give :!uptime, you will see what I am saying.
      • ruby spec/example_spec.rb, you know its ruby command executing the file.
      • <cr>: This carriage return equal to <Enter> key, with out this, your command will be loaded but to execute you have to press enter again. so adding <cr> at the end, will help executing your command.

Based on above understanding, lets say you have a python file like service.py, you want to execute it with a VIM binding, you can use below command
map <C-r> :w\|!python service.py <cr>
so on CTRL+r, I will be running my python program.

Hope it helps.
Thanks
Raja

0 comments:

Post a Comment