Calling another user command inside the user defined command - vim

Is it not possible to call another user defined command in a user defined command?
I tried the following two lines but neither of them worked:
command! GetRapidLinks FindRapidLinks|MatchesOnly
command! GetRapidLinks :FindRapidLinks|:MatchesOnly
The Vim help reads:
You cannot use ":X", ":Next" and ":Print"
The context of this restriction is not clear. I guess one cannot use those in a user defined command, right?

This is probably because you didn't define the :FindRapidLinks command with -bar; without it, the command "eats" the entire remaining arguments (cp. :help command-bar). So, either redefine:
:command! -bar FindRapidLinks ...
:command! GetRapidLinks FindRapidLinks|MatchesOnly
or work around this (for cases when you cannot redefine the other command) via :execute:
:command! GetRapidLinks execute 'FindRapidLinks'|MatchesOnly
As a general rule, use -bar unless your custom command needs to be passed arguments that contain special characters like |.

Actually, what the help means is, you can't call a user command :Next or :X, because those are the few builtin commands, that start with a capital letter. I am not sure, why your vim actually says, you can't define a :Print command, as it is possible to define your own custom :Print command (since the builtin command is only an alias for :print anyhow)
I am not sure, what exactly you are trying to achieve here, so I can't answer the first part of your question. You can however in a function or in a custom command call another custom command.

Perhaps try backticks ` (not to be mistaken with quotes ' or ")
for e.g.
command! GetRapidLinks `FindRapidLinks` | MatchesOnly
or
command! GetRapidLinks $(FindRapidLinks) | MatchesOnly

Related

How to get the arguments during a custom defined command?

I have this custom command that I want to define. But I can't manage to get the arguments separately:
:command -nargs=2 :%s/<args1>/<args2>/gc // <args1> <args2> dont work
Anyone has some solution for this?
<args1> and <args2> doesn't exist. You invented this yourself. The easiest way to do this is to use a function wrapper and <f-args>:
fun! s:sub(search, replace)
execute ':%s/' . a:search . '/' . a:replace . '/gc'
endfun
command! -nargs=+ Replace call s:sub(<f-args>)
<f-args> splits the the command arguments at whitespace and adds proper quoting and commas, in order to pass it to a function.
Using a function wrapper also has the added benefit of resetting the #/ register after its finished running. If you don't want this, then explicitly assign it with let #/ = a:search.
Other than this, almost everything about your :command call is wrong:
-nargs=2 isn't supported − see :help :command-nargs
You forgot to fill in the command name.
// isn't used to denote comments in VimScript; " is used for that.
See :help :command for a full description of the syntax.

How to expand function arguments in Vim command line?

Vim's Utl plugin offers a convenient way for doing web queries from within the editor. When called directly from the command line, a dictionary lookup can be done like this:
:Utl ol http://dict.leo.org/?search=my+search+term
What's the correct way for defining a custom command with the same purpose (my+search+term being user input)? I can't seem to get <f-args> right with this one:
command -nargs=1 SearchLeo :exe ":Utl ol http://dict.leo.org/?search=" . expand("<f-args>")
What's the correct way of defining function arguments here? Or should I turn this into a more complete function? Thanks!
You probably don't need expand() here; it's just for expanding globs (like *.txt) or the special variables like % for the current file.
You're quoting the argument twice, once through <f-args> (<q-args> would be slightly more correct, though it only matters with a variable number of arguments), once literally.
Use this:
command -nargs=1 SearchLeo :exe ":Utl ol http://dict.leo.org/?search=" . <q-args>

VimL: Make function called through key receive input from Vim command line

So, this may not be a clear question because of the title. But here's the actual explanation:
I have this custom function, I want this function to be able to be called from the command line, this is done (:FunctionName arg), but now I need to make this function react to a certain mapping.
So when the user presses <leader>cs it will prompt the user for the arg part, mapping this is still kind of unclear to me, but also how to achieve this functionality. This is kind of what the Surround script does, where it lets you input the old character and the new character to replace it with.
I need this for my first script that I'm making BTW, which allows you to change the file's syntax in a manner similar to Sublime Text's way.
Thanks for all your help!
The simplest way is to remove the concluding <CR> from the mapping, so that it just enters command-line mode and fills the command line with your custom command:
:nnoremap <Leader>cs :FunctionName<Space>
You can then enter the arg and trigger the command with Enter.
Alternatively, you can query for user input via input() (and single characters with getchar(); obvious, isn't it?!), like this:
function! FunctionNameWithQuery()
let arg = input('arg: ')
execute 'FunctionName' arg
endfunction
nnoremap <Leader>cs :call FunctionNameWithQuery()<CR>

Vim search and highlighting control from a script

I'm writing a script in which I want to control searches programmatically, and get them highlighted. The search() function results are not highlighted (I think), so using that function is not of use to me.
What I want to do is use the 'normal /' command to search for a variable, but that doesn't seem to be straightforward. I can script the command:
execute 'normal /' . my_variable . '\<CR>'
(or other variations as suggested in the vim tip here: http://vim.wikia.com/wiki/Using_normal_command_in_a_script_for_searching )
but it doesn't do anything. I can see the correct search term down in the command line after execution of the script line, but focus is in the document, the search register has not been altered, and the cursor has not done any search. (It seems as though the < CR > isn't getting entered, although no error is thrown -- and yes, I have tried using the literal ^M too.)
I can at least control the search register by doing this:
execute 'let #/ ="' . a:term .'"'
and then the obvious thing seems to be to do a:
normal n
But that 'normal n' doesn't do anything if I run it in a script. Setting the search register does work, if I manually press 'n' after the scrip terminates the search happens (and highlighting appears, since hlsearch is on). I don't even care if the cursor is positioned, I just want the register pattern to be highlighted. But various combinations of 'set hlsearch' in the script don't work either.
I know I could use 'match()', but I want to get it working with regular search highlighting, and I wonder what I'm doing wrong. It must be something simple but I'm not seeing it. Thanks for any help.
run:
let #/ = a:searchStr
from inside your function then run
normal n
from outside your function (inside it does nothing) eg.
command -nargs=* Hs call MySearch() | normal n
or you can use:
set hlsearch
instead of normal n if you don't want the cursor to move
(I cannot work out another way of doing this without having something outside the function.)
If your script is using functions, then this quote from :help function-search-undo is relevant:
The last used search pattern and the redo command "."
will not be changed by the function. This also
implies that the effect of :nohlsearch is undone
when the function returns.
Vim usually tries to reset the search pattern (and a few other things) when a function ends, often you can get around this by adding the n (next search) to the end of a mapping, or using :map <expr> and having your function return the key sequence to be executed.
On closer inspection, it seems \<CR> is not picked up inside single quotes. Try using this instead:
execute 'normal /' . my_variable . "\<CR>"

VIM: how to put result of a colon command into a variable?

In VIM script, i want to check if VIM was started with command-line arguments or wthout. For this, i want to check a result of :args command that prints arguments. But how to put a result of :args inside an if() or a variable. Following wll not work:
let s:MyArgs = execute( "args" )
You need to play with :redir. I have encapsulated this operation in a function there.
BTW, argc() should also answer your original need in a simpler way. (:h argc())

Resources