开发者

How to write a function or macro to send a parameter to a system call in gVim for Windows?

开发者 https://www.devze.com 2022-12-18 07:14 出处:网络
The useful command :r!date is not so useful in gVim for Windows (not Cygwin\'s gVim) because Windows has its own date function which does not do what I want.

The useful command

:r!date

is not so useful in gVim for Windows (not Cygwin's gVim) because Windows has its own date function which does not do what I want.

So, something like

:r!c:\cygwin\bin\date

would be great. But that's a lot 开发者_运维百科to type. And considering that I might want to call a few things this way, it would be nice to write a function, which I could pass an argument foo and it would run

:r!c:\cygwin\bin\foo

What's the best way to do this? It should be:

  1. Permanent: stored in .vimrc or some startup file.
  2. Executed with as few keystrokes as possible.

Any suggestions for good places to create mappings are appreciated.

Thanks!


I think, a simpler solution for day-to-day use (especially if you are interested in only one of the many system-like commands) would be creating a custom synonym-command:

:command! -nargs=1 -range=% RR <line1>,<line2>read! c:\cygwin\bin\<args>

This tells Vim to create a new command named RR that accepts a range and one (mandatory) argument. The command just passes arguments to read! prepending c:\cygwin\bin\ to the argument.

You could even provide a file completion for the files in c:\cygwin\bin\ directory. All you need for that to work is to create completion-list function like this:

function! RRComplete(A, L, P)
    return system('dir /b /l c:\cygwin\bin')
endfunction

and then specify the name of that function when creating the command:

:command! -nargs=1 -complete=custom,RRComplete -range=% RR <line1>,<line2>read! c:\cygwin\bin\<args>


:h strftime() ?

Otherwise, a longtime ago I wrote system() wrappers in order to execute external programs from vim either on *nix, or on win32-gvim with cygwin's bash or $COMSPEC as &shell.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号