Author Topic: vim key sequence binding  (Read 1530 times)

photometer

  • Community Member
  • Posts: 61
  • Hero Points: 0
vim key sequence binding
« on: December 14, 2020, 10:33:53 PM »
I would like to replace the data between 2 commas. I am using vim emulation.
i.e. hello world, this is , a test.
Hence in vim, if I put my cursor on word 'this' and type T,ct,    it will select the text between the 2 commas and now I can replace it with new text.
How do I bind this key sequence to a key (e.g. Alt-C) ?

Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 6875
  • Hero Points: 530
Re: vim key sequence binding
« Reply #1 on: December 15, 2020, 02:42:26 PM »
You would have to write a Slick-C macro to bind a key macro to a key like Alt+C.

You can use "qa" to record and name the key macro "a" and then play it back with "@a". This is the easiest method for sure.

Another non-Vim way would be to record a Slick-C macro (Ctrl+F11 by default). You have to do this in insert mode since it won't record command mode actions. I point this out for completeness even though it won't record command mode actions. When you use this method, you can name your recorded Slick-C macro (like mac1) and then bind it to Alt+C or any key.  When you bind the command to a key, you can bind it for insert mode (the default), command mode, and/or visual mode.


photometer

  • Community Member
  • Posts: 61
  • Hero Points: 0
Re: vim key sequence binding
« Reply #2 on: December 15, 2020, 04:23:59 PM »
Hi Clark,
1. Are the VIM macros persistent over Slickedit close/open?

2. Can you please explain "You would have to write a Slick-C macro to bind a key macro to a key like Alt-C". Say I record the VIM macro and name it "a". What is the syntax to call this key macro "a" from the Slick-C macro? Is there an example?

3. If I need to write a normal Slick_C macro, I assume the steps would be
- from the current cursor position find the next comma and mark that position as end
- from the current cursor position find the previous comma and mark that position as start
- select the line from start to end
What would be a sample macro in the macros directory that I can study to implement the above.

4. I know alias can call a macro. But can a macro call an alias?

Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 6875
  • Hero Points: 530
Re: vim key sequence binding
« Reply #3 on: December 15, 2020, 05:29:24 PM »
1. The VIM key macros are stored in the clipboards which optionally are stored in vrestore.slk. You need to turn on Auto restore clipboards (Tools>Options>Application Options>Auto Restore).

2. The macro which executes VIM key macros is vi_execute_kbdmacro (Macro>Go to Definition). Something like this will do the trick.

   cb_name:='a';
   kbdmacro_string:=_getClipboardText(true,true,'"'cb_name_local,(p_object==OI_EDITOR)?p_UTF8:true);
   _macro('KP',1,kbdmacro_string);

3. If you are new to Slick-C, I would recommend recording various Slick-C macros (i.e use Edit>Select>Char, Search>Find), and then pressing the "Edit" button. This will show you the source for the Slick-C macro you just recorded. This saved a lot of time trying to figure what function/command you need to call.

4. The expand_alias(AliasName) command can be used to fetch and insert the value of a language specific or global alias.



photometer

  • Community Member
  • Posts: 61
  • Hero Points: 0
Re: vim key sequence binding
« Reply #4 on: January 14, 2021, 08:49:25 PM »
Hi Clark,

Here is what I wrote for the macro

#include "slick.sh"

_command vimcomma() name_info(','VSARG2_MACRO|VSARG2_MARK|VSARG2_REQUIRES_EDITORCTL)
{
   if (find(',',"+-")) stop();
   vi_cursor_right();
   vi_toggle_char_visual();
   if (find(',',"+")) stop();
   vi_visual_select_left();
   vi_visual_change();
}


I bound this to Alt-C key. It mostly works as intended.

void foo(int a, int b, int c, int d)

If I place my cursor say anywhere on 'int c' and press Alt-C, it will delete the text 'int c' and put my cursor in insert mode and I can type the new word.
Where I see an issue is that from the current cursor position, if there is no comma to the left or to the right, it keep searching backwards till it finds a comma and then keeps searching forward till it finds a comma and then deletes that many lines of code.

How I modify the above macro such that the find is restricted to the current line and if it does not find a comma to the left for the cursor or to the right of the cursor, the macro does not do anything.

Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 6875
  • Hero Points: 530
Re: vim key sequence binding
« Reply #5 on: January 15, 2021, 02:13:05 PM »
Find can be restricted to search within a selection. Select the current line.

_deselect();
_select_line();

Specify ‘m’ option to the find call.

photometer

  • Community Member
  • Posts: 61
  • Hero Points: 0
Re: vim key sequence binding
« Reply #6 on: January 15, 2021, 03:16:41 PM »
I modified the macro as below:
_command vimcomma() name_info(','VSARG2_MACRO|VSARG2_MARK|VSARG2_REQUIRES_EDITORCTL)
{
   _deselect();
   _select_line();
   if (find(',',"+-m")) stop();
   vi_cursor_right();
   vi_toggle_char_visual();
   if (find(',',"+m")) stop();
   vi_visual_select_left();
   vi_visual_change();
}

Now, it does not work at all. It will delete the entire line if a comma is present on that line rather than "between 2 commas". If no comma is present, it will not delete anything, so that is good.

Clark

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 6875
  • Hero Points: 530
Re: vim key sequence binding
« Reply #7 on: January 15, 2021, 05:29:43 PM »
You could trying using a regular expression which matches beginning of line or end of line. match_length() should return 0 when your regex matches beginning or end of line assuming you write your regular expression correctly.