Getting started with vim scripts with perl

I would like to create a vim function / command to insert an XSD style timestamp. I am currently using the following in my vimrc file:

nmap <F5> a<C-R>=strftime("%Y-%m-%dT%H:%M:%S-07:00")<CR><Esc>

I would like to use Perl code:

use DateTime;
use DateTime::Format::XSD;
print DateTime->now(formatter => 'DateTime::Format::XSD', time_zone => 'America/Phoenix');

But I donโ€™t know where to start. I know that I can define a function using Perl. Example:

function PerlTest() 
perl << EOF
  use DateTime;
  use DateTime::Format::XSD;
  print DateTime->now(formatter => 'DateTime::Format::XSD', time_zone => 'America/Phoenix');
EOF

But when I changed my vimrc to the following, I did not get what I expected:

nmap <F5> a<C-R>=PerlTest()<CR><Esc>

Can someone point me in the right direction to implement this? This is the first time I have tried writing functions in vim. In addition, I am using vim 7.2 compiled with perl support.

+3
source share
2 answers

, : help if_perl Perl Vim.

, - . , .

, .

fun! PerlTest()
    perl << EOF
    use DateTime;
    use DateTime::Format::XSD;
    my ($row, $col) = $curwin->Cursor();
    my ($line) = $curbuf->Get($row);
    substr($line, $col + 1, 0,
           DateTime->now(formatter => 'DateTime::Format::XSD',
                         time_zone => 'America/Phoenix'));
    $curbuf->Set($row, $line);
EOF
endfun

nnoremap <F5> :call PerlTest()<CR>.


, , , , , 1 != 1 (.. , ..). , , , , .

, Vim , .

, , , Perl , Vim.

fun! PerlTest()
    let a_reg = getreg('a', '1')
    let a_reg_type = getregtype('a')
    perl << EOF
    use DateTime;
    use DateTime::Format::XSD;
    my $date = DateTime->now(formatter => 'DateTime::Format::XSD',
                             time_zone => 'America/Phoenix');
    VIM::Eval("setreg('a', '$date', 'c')");
EOF
    normal "ap
    call setreg('a', a_reg, a_reg_type)
endfun

nnoremap <F5> :call PerlTest()<CR>
+4

Perl script, :

:map <F5> :let @a = system("perl script.pl")<cr>"ap

perl script.pl ( ), @a .

0

Source: https://habr.com/ru/post/1731579/


All Articles