A couple options occur:
1) use :redir to a register and paste the register contents:
:redir @a
:chdir
:redir END
"ap
2) use the system() call in an expression-register to perform the
command in your OS's command-interpreter:
In insert mode in *nix:
^R=system('pwd')
In insert mode in Win32:
^R=system('cd')
In normal mode in *nix:
"=system('pwd')<cr>p
In normal mode in Win32:
"=system('cd')<cr>p
where "^R" is control+R and "<cr>" is hitting <enter>.
For more info, you can read up at
:help :redir
:help i_CTRL-R
:help @=
As a caveat, most of those end up being line-wise and have odd
behaviors if your pulled-contents have a trailing newline (which
is most of the time).
-tim
I'm not sure it's *that* strange -- using ":redir" is the
canonical way to access the messages Vim produces, and Vim gives
plenty of options for redirection (files/registers/variables, and
appending/overwriting). I could see adding a hist-names (":help
hist-names") entry for messages to allow access to the
message-history list which would then allow you to insert it via
something like ^R=histget('messages')
That said, I could count on one hand[*] the number of times in 10
years of using Vim that I've wanted to include the output of a
vim command in my text, and
> And of course if there is already a redirect going on this
> will mess things up.
exactly 0 of those times have I been using :redir for something
else at the same time.
-tim
[*] okay, it might be cheating that I often count in binary on my
fingers, but that's still less than 32 :)
Why not try your hand at writing a function to do that? Admittedly, it
would have to begin with a capital letter.
Regards,
Chip Campbell
> Why not try your hand at writing a function to do that? Admittedly, it
> would have to begin with a capital letter.
It is not possible: Vsystem("redir @a> | echom 'abc' | redir END") won't work.
Original message:
> > Spiros Bousbouras wrote:
> > > [snip]
> > > I would prefer a vsystem() function (vim system) or whatever
> > > you want to call it so that for example you could do
> > > ^R=vsystem("swapname")
> >
> > [snip]
> >
> > Why not try your hand at writing a function to do that? Admittedly, it
> > would have to begin with a capital letter.
> It is not possible: Vsystem("redir @a> | echom 'abc' | redir END") won't work.
If one were writing a function as Chip suggested, one would put the
redir commands in the body of the function, not in the argument.
Regards,
Gary
I totally agree with you. Would be much more simpler if we could wrote
something like:
:<echo 25*47
And the output was directly put in the current buffer in the cursor position.
The sequence "redir 'something'", type a command and then 'redir END' is
really to much to do when we need to be fast. To get the result of the
calculation above is much more easy just "see" the value in the command window
and write it by your hand in the buffer.
This is a feature that a miss in Vim/GVim for quite sometime.
Regards.
The above is already possible in vim.
in insert mode press <Ctrl-R>=25*47 and that's it.
:h i_CTRL-R_=
regards,
Christian
you can insert the output of every VimL expression with
<C-R>=your-expression-goes-here<CR>
while you are in insert mode. Your example would then be typed as
<C-R>=25*47<CR>
Regards,
J�rgen
--
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us. (Calvin)
Regards,
Chip Campbell
For this particular use case (insert the value of an expression) you can
do it by means of the expression register:
in Insert mode
^R=25 * 47
(where ^R is a Ctrl-R keystroke), followed by Enter, or in Normal mode
"=25 * 47↓p
where ↓ represents a press on the Enter key.
See :help quote=
Best regards,
Tony.
--
Real computer scientists don't comment their code. The identifiers are
so long they can't afford the disk space.
While this works for expressions (the followup question about
"echo 25*47"), it doesn't satisfy the OP's request for arbitrary
Ex commands. Assuming the commands to be executed don't include
redirs and there's not a redir already in process (that you don't
want to terminate), it could be wrapped in a function
function! Capture(excmds)
redir => s:results
exec a:excmds
redir END
return s:results
endfunction
You could then use the function in concert with the expression
register:
<c-r>=Capture('scriptnames')
(odd results if you don't have 'paste' set) in insert mode, or
you can use
"<c-r>=Capture('scriptnames')<cr>p
to paste it in normal mode. The command should also work with
multiple pipe-separated commands:
Capture('scriptnames|set')
Hope this helps,
-tim
> If one were writing a function as Chip suggested, one would put the
> redir commands in the body of the function, not in the argument.
I do understand this: I meant that if function argument calls redir itself then
redir inside Vsystem function won't work.
Original message:
I suppose either would do -- l:results would work just as well.
I was just grabbing for something that wouldn't leak into globals.
>> Hope this helps,
>
> It's good enough for government work. Thank you.
Sorry...If I knew I was aiming for government work, I would have
provided a horribly baroque user-interface, overcharged, come in
way over budget, and taken 3 years over estimate to provide
something that didn't quite work right. :)
-tim (who used to work for a large contracting company where that
was the unfortunate way things ran. SOOooo glad to be outta there)
Right...I don't think Vim has a means by which to detect if/where
redirection is occurring so it can be saved and restored. So
it's a classic case of
Patient: "Doctor, it hurts when I do this..."
Doctor: "Well, don't do that!"
where in this case, the "do this" is nested redirection. :)
-tim
When following some convention, nesting can be enabled, see attachment.
(must not use :redir directly, only StrRedir() -- where "StrRedir" is my
name for "Vsystem").
Attachment defines two commands, both use StrRedir().
:FiltRedir {cmd} {pattern}
echo the output of {cmd}, filtered by {pattern}
:PutRedir {cmd}
:put the output of {cmd} into the buffer
Example with nested call to StrRedir():
:PutRedir FiltRedir scrip \<plug
Puts the output of :scriptnames into the buffer, filtered to lines
matching '\<plug'.
--
Andy