1

I want to be able to write my own custom escape method.

If I want to paste the contents of register s while in insert mode, I can type <C-R>s<CR>.

If I want to escape all occurrences of \, I can type <C-R>=escape(@s, '\').

How do I paste the contents of register s while executing an arbitrary formatting function?


Extra information:

The end goal is to be able to hit a key and append a function declaration from a .c file to a register. void MyClass::foo() { will turn into void foo();

I plan to solve this by writing an =escape-esque function that deletes words starting with capital letters, and does the equivalent of :substitute/ *{$/;/

  • Shameless plug: lh#cpp#AnalysisLib_Function#AnalysePrototype('void MyClass::foo(int)') (from my lh-cpp) will return {'throw':[], 'parameters': [{'nl':0, 'name':'', 'type':'int', 'default':''}], 'constexpr':0, 'name':['MyClass', 'foo'], 'noexcept':'', 'const':0, 'return':'void', 'pure':0, 'qualifier':'', 'overriden':0, 'volatile':0, 'final':0, 'special_definition':''}. From there you'll be able to reconstruct the declaration from the definition -- I usually use it the other way with :GOTOIMPL (virtual & co cannot be deduced from the definition) – Luc Hermitte Jul 18 '18 at 9:07
1

If you can define it all in one substitute all then replace the escape, since that can be any function. <C-R>=substitute(@s, '\m\(\w\+\) \w\+::\([^{]\+\) {', '\1 \2;', '')<CR>

If you don't want to do it all in one substitute, or to give it a descriptive name, you can write your own function.

For example:

function! FunctionDeclaration(snip)
  return substitute(a:snip, '\m\(\w\+\) \w\+::\([^{]\+\) {', '\1 \2;', '')
endfunction

Then invoke it with <C-R>=FunctionDeclaration(@s)<CR>

See :help user-functions for defining your own function like FunctionDeclaration above.

See :help substitute() for how to perform a regex substitution against a variable.

The breakdown of the regex suggested above:

  • \m - Enable magic so that the regex behavior is not impacted by user settings. See :help magic.
  • \(\w\+\) - Match a series of one or more "word characters". Matches the void in the example. See :help \w. Capture the result of that part of the match into the first match group. See :help pattern-overview. This can be referenced with \1 in the replacement.
  • \w\+:: - Match a series of one or more "word characters" followed by ::. This matches the MyClass:: in the example. This is not matched in a subgroup so it will be dropped in the replacement.
  • \([^{]+\) - Match a series of characters which are not { in a subgroup. This matches the foo() in the example. Can be reference by \2 in the replacement.
  • { - Match the literal { at the end. Not matched in a subgroup so it is dropped.
  • Excellent, so I can define a vimscript function of an arbitrary name and then plug in whatever logic I want. (Here's a link to a handy cheatsheet, for anyone new to vimscript like myself). Your function is rad!! Thanks. I'm going to try and tailor it for myself to see how much functionality I can get into it. I will upvote when I get 15 rep – Ari Sweedler Jul 17 '18 at 21:31
  • Added some references to the built-in help documentation in the answer. Note that \m at the start of the pattern has to do with magic (the syntax used for the pattern, which characters are escaped), not with multi-line regexes like it might in some other regex engines. – Nate Bosch Jul 18 '18 at 5:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.