0

I have a few Regex expressions that I use with xVim for Xcode. Rather than repeatedly typing them out in the command bar with \<Regex>, I'd like to be able to invoke them with a custom command, like :Regex1. So I've added command Regex1 “/-\s*\(“ to my xvimrc file and restarted Xcode. When I run :Regex1 however nothing happens.

Alex Bollbach
  • 4,370
  • 9
  • 32
  • 80
  • What makes you think anything should happen? – romainl Jun 22 '17 at 14:47
  • Maybe that wasn't clear. What I want to happen is for that regex to be entered as a Vim search. Subsequently, stepping through the results with `n`, `N` should jump to each match. So when I type `:Regex1` I expected that to be equivalent to `/-\s*\(`. – Alex Bollbach Jun 22 '17 at 14:53
  • Again what makes you think anything should happen? Do you see `:command` [in that file](https://github.com/XVimProject/XVim/blob/master/Documents/Users/FeatureList.md)? xVim is **not** Vim so there's no reason whatsoever to expect any Vim thing to work as-is in xVim. – romainl Jun 22 '17 at 16:50
  • I'm confused. The command I have the expectation for is `:Regex1`, which from my understanding is a custom command defined in the `xvimrc` file. xVim supports a `xvimrc` config file so I guessed that custom commands would be an included feature. – Alex Bollbach Jun 22 '17 at 17:11
  • The more thorough you are at reading the documentation of your tools the less confused you are. – romainl Jun 22 '17 at 18:33
  • 1
    I think `:command` is a vim extension, that is not available in other vi clones (xvim) – Christian Brabandt Jun 23 '17 at 11:00

3 Answers3

1

Your command wouldn't even work in original Vim. I don't know xVim, but try something along these lines:

" With cursor moving to match.
command Foo /foo/

" Just updating the search pattern (but less likely to be portable to xVim).
command Foo let @/ = 'foo'

If none of that works; try defining a mapping instead. As this is just translating keys, it has the highest chance of being supported.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • so i have `command Foo /-\s*\(` in my vimrc file, but when i type `:Foo` in my editor, i don't feel like it is working. if I type `n` or `N` the cursor does not move to the matching text. am i missing a step? – Alex Bollbach Jun 22 '17 at 15:10
  • That particular command yields `E54: Unmatched \(` for me. Change `\(` to `(` to make it work. – Ingo Karkat Jun 22 '17 at 15:51
  • Perhaps we are experience different results based on the environment. I'm doing this in `xvim` which I assumed supports custom commands. When I add `command Foo /-\s*(` to my `xvimrc` file, then type `:Foo` in the editor and hit Enter, the bottom bar clears text (the command was entered..). And then using `n` or `N` does not jump to the expected Regex matches. In my case I'm trying to match against method signature's such as `-(void)` or `- (void)` which is why I had a backslash before the `(` to escape that, so i believe my original Regex was correct. Tho the command mechanism is failing – Alex Bollbach Jun 22 '17 at 17:17
  • Does xVim have a `:help` that documents what is supported (or any limitations, or change in syntax compared to Vim)? It looks like this isn't supported. Maybe you'll get better results with a mapping... – Ingo Karkat Jun 22 '17 at 18:10
  • Sure. I'll look into a mapping. My assumption was that, given my pathetic Vim comprehension, it was almost definitely something I was doing incorrectly. I mentioned xVim as my environment to provide context. So I'm feeling now like the problem is pointing towards xVim not supporting custom commands? I'll look into that. – Alex Bollbach Jun 22 '17 at 20:24
1

I would suggest using this PERL Regex plugin since it already does what you want.

https://github.com/othree/eregex.vim

Arithran
  • 1,219
  • 3
  • 11
  • 22
1

Abbreviations ...

I understand you often use the same regex. You can use abreviations instead of a command to do a search.

ab re -\s*(

then type / + re + space and your long regex (here just "-\s*(" should expand).

... Not user defined command

User defined commands are not available in ed nor in vi nor in vim without the +eval compilation flag (:h user-commands and scroll one line up).

For a list of ex commands: http://www.csb.yale.edu/userguides/wordprocess/vi-summary.html

For a list of ed commands: http://pubs.opengroup.org/onlinepubs/7908799/xcu/ed.html

Tinmarino
  • 3,693
  • 24
  • 33