Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1 | *if_perl.txt* For Vim version 7.0aa. Last change: 2005 Mar 29 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | |
| 3 | |
| 4 | VIM REFERENCE MANUAL by Sven Verdoolaege |
| 5 | and Matt Gerassimof |
| 6 | |
| 7 | Perl and Vim *perl* *Perl* |
| 8 | |
| 9 | 1. Editing Perl files |perl-editing| |
| 10 | 2. Compiling VIM with Perl interface |perl-compiling| |
| 11 | 3. Using the Perl interface |perl-using| |
| 12 | |
| 13 | {Vi does not have any of these commands} |
| 14 | |
| 15 | The Perl interface only works when Vim was compiled with the |+perl| feature. |
| 16 | |
| 17 | ============================================================================== |
| 18 | 1. Editing Perl files *perl-editing* |
| 19 | |
| 20 | Vim syntax highlighting supports Perl and POD files. Vim assumes a file is |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 21 | Perl code if the filename has a .pl or .pm suffix. Vim also examines the first |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 22 | line of a file, regardless of the filename suffix, to check if a file is a |
| 23 | Perl script (see scripts.vim in Vim's syntax directory). Vim assumes a file |
| 24 | is POD text if the filename has a .POD suffix. |
| 25 | |
| 26 | To use tags with Perl, you need a recent version of Exuberant ctags. Look |
| 27 | here: |
| 28 | http://ctags.sourceforge.net |
| 29 | |
| 30 | Alternatively, you can use the Perl script pltags.pl, which is shipped with |
| 31 | Vim in the $VIMRUNTIME/tools directory. This script has currently more |
| 32 | features than Exuberant ctags' Perl support. |
| 33 | |
| 34 | ============================================================================== |
| 35 | 2. Compiling VIM with Perl interface *perl-compiling* |
| 36 | |
| 37 | To compile Vim with Perl interface, you need Perl 5.004 (or later). Perl must |
| 38 | be installed before you compile Vim. Vim's Perl interface does NOT work with |
| 39 | the 5.003 version that has been officially released! It will probably work |
| 40 | with Perl 5.003_05 and later. |
| 41 | |
| 42 | The Perl patches for Vim were made by: |
| 43 | Sven Verdoolaege <skimo@breughel.ufsia.ac.be> |
| 44 | Matt Gerassimof |
| 45 | |
| 46 | Perl for MS-Windows can be found at: |
| 47 | http://www.perl.com/CPAN/ports/nt/Standard/x86/ |
| 48 | |
| 49 | ============================================================================== |
| 50 | 3. Using the Perl interface *perl-using* |
| 51 | |
| 52 | *:perl* *:pe* |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 53 | :pe[rl] {cmd} Execute Perl command {cmd}. The current package |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 54 | is "main". |
| 55 | |
| 56 | :pe[rl] << {endpattern} |
| 57 | {script} |
| 58 | {endpattern} |
| 59 | Execute Perl script {script}. |
| 60 | {endpattern} must NOT be preceded by any white space. |
| 61 | If {endpattern} is omitted, it defaults to a dot '.' |
| 62 | like for the |:append| and |:insert| commands. Using |
| 63 | '.' helps when inside a function, because "$i;" looks |
| 64 | like the start of an |:insert| command to Vim. |
| 65 | This form of the |:perl| command is mainly useful for |
| 66 | including perl code in vim scripts. |
| 67 | Note: This command doesn't work when the Perl feature |
| 68 | wasn't compiled in. To avoid errors, see |
| 69 | |script-here|. |
| 70 | |
| 71 | |
| 72 | Example vim script: > |
| 73 | |
| 74 | function! WhitePearl() |
| 75 | perl << EOF |
| 76 | VIM::Msg("pearls are nice for necklaces"); |
| 77 | VIM::Msg("rubys for rings"); |
| 78 | VIM::Msg("pythons for bags"); |
| 79 | VIM::Msg("tcls????"); |
| 80 | EOF |
| 81 | endfunction |
| 82 | < |
| 83 | |
| 84 | *:perldo* *:perld* |
| 85 | :[range]perld[o] {cmd} Execute Perl command {cmd} for each line in the |
| 86 | [range], with $_ being set to the text of each line in |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 87 | turn, without a trailing <EOL>. Setting $_ will change |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 88 | the text, but note that it is not possible to add or |
| 89 | delete lines using this command. |
| 90 | The default for [range] is the whole file: "1,$". |
| 91 | |
| 92 | Here are some things you can try: > |
| 93 | |
| 94 | :perl $a=1 |
| 95 | :perldo $_ = reverse($_);1 |
| 96 | :perl VIM::Msg("hello") |
| 97 | :perl $line = $curbuf->Get(42) |
| 98 | < |
| 99 | *E299* |
| 100 | Executing Perl commands in the |sandbox| is limited. ":perldo" will not be |
| 101 | possible at all. ":perl" will be evaluated in the Safe environment, if |
| 102 | possible. |
| 103 | |
| 104 | |
| 105 | *perl-overview* |
| 106 | Here is an overview of the functions that are available to Perl: > |
| 107 | |
| 108 | :perl VIM::Msg("Text") # displays a message |
| 109 | :perl VIM::Msg("Error", "ErrorMsg") # displays an error message |
| 110 | :perl VIM::Msg("remark", "Comment") # displays a highlighted message |
| 111 | :perl VIM::SetOption("ai") # sets a vim option |
| 112 | :perl $nbuf = VIM::Buffers() # returns the number of buffers |
| 113 | :perl @buflist = VIM::Buffers() # returns array of all buffers |
| 114 | :perl $mybuf = (VIM::Buffers('qq.c'))[0] # returns buffer object for 'qq.c' |
| 115 | :perl @winlist = VIM::Windows() # returns array of all windows |
| 116 | :perl $nwin = VIM::Windows() # returns the number of windows |
| 117 | :perl ($success, $v) = VIM::Eval('&path') # $v: option 'path', $success: 1 |
| 118 | :perl ($success, $v) = VIM::Eval('&xyz') # $v: '' and $success: 0 |
| 119 | :perl $v = VIM::Eval('expand("<cfile>")') # expands <cfile> |
| 120 | :perl $curwin->SetHeight(10) # sets the window height |
| 121 | :perl @pos = $curwin->Cursor() # returns (row, col) array |
| 122 | :perl @pos = (10, 10) |
| 123 | :perl $curwin->Cursor(@pos) # sets cursor to @pos |
| 124 | :perl $curwin->Cursor(10,10) # sets cursor to row 10 col 10 |
| 125 | :perl $mybuf = $curwin->Buffer() # returns the buffer object for window |
| 126 | :perl $curbuf->Name() # returns buffer name |
| 127 | :perl $curbuf->Number() # returns buffer number |
| 128 | :perl $curbuf->Count() # returns the number of lines |
| 129 | :perl $l = $curbuf->Get(10) # returns line 10 |
| 130 | :perl @l = $curbuf->Get(1 .. 5) # returns lines 1 through 5 |
| 131 | :perl $curbuf->Delete(10) # deletes line 10 |
| 132 | :perl $curbuf->Delete(10, 20) # delete lines 10 through 20 |
| 133 | :perl $curbuf->Append(10, "Line") # appends a line |
| 134 | :perl $curbuf->Append(10, "Line1", "Line2", "Line3") # appends 3 lines |
| 135 | :perl @l = ("L1", "L2", "L3") |
| 136 | :perl $curbuf->Append(10, @l) # appends L1, L2 and L3 |
| 137 | :perl $curbuf->Set(10, "Line") # replaces line 10 |
| 138 | :perl $curbuf->Set(10, "Line1", "Line2") # replaces lines 10 and 11 |
| 139 | :perl $curbuf->Set(10, @l) # replaces 3 lines |
| 140 | < |
| 141 | *perl-Msg* |
| 142 | VIM::Msg({msg}, {group}?) |
| 143 | Displays the message {msg}. The optional {group} |
| 144 | argument specifies a highlight group for Vim to use |
| 145 | for the message. |
| 146 | |
| 147 | *perl-SetOption* |
| 148 | VIM::SetOption({arg}) Sets a vim option. {arg} can be any argument that the |
| 149 | ":set" command accepts. Note that this means that no |
| 150 | spaces are allowed in the argument! See |:set|. |
| 151 | |
| 152 | *perl-Buffers* |
| 153 | VIM::Buffers([{bn}...]) With no arguments, returns a list of all the buffers |
| 154 | in an array context or returns the number of buffers |
| 155 | in a scalar context. For a list of buffer names or |
| 156 | numbers {bn}, returns a list of the buffers matching |
| 157 | {bn}, using the same rules as Vim's internal |
| 158 | |bufname()| function. |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 159 | WARNING: the list becomes invalid when |:bwipe| is |
| 160 | used. Using it anyway may crash Vim. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 161 | |
| 162 | *perl-Windows* |
| 163 | VIM::Windows([{wn}...]) With no arguments, returns a list of all the windows |
| 164 | in an array context or returns the number of windows |
| 165 | in a scalar context. For a list of window numbers |
| 166 | {wn}, returns a list of the windows with those |
| 167 | numbers. |
Bram Moolenaar | d4755bb | 2004-09-02 19:12:26 +0000 | [diff] [blame] | 168 | WARNING: the list becomes invalid when a window is |
| 169 | closed. Using it anyway may crash Vim. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 170 | |
| 171 | *perl-DoCommand* |
| 172 | VIM::DoCommand({cmd}) Executes Ex command {cmd}. |
| 173 | |
| 174 | *perl-Eval* |
| 175 | VIM::Eval({expr}) Evaluates {expr} and returns (success, val). |
| 176 | success=1 indicates that val contains the value of |
| 177 | {expr}; success=0 indicates a failure to evaluate |
| 178 | the expression. '@x' returns the contents of register |
| 179 | x, '&x' returns the value of option x, 'x' returns the |
| 180 | value of internal |variables| x, and '$x' is equivalent |
| 181 | to perl's $ENV{x}. All |functions| accessible from |
| 182 | the command-line are valid for {expr}. |
| 183 | |
| 184 | *perl-SetHeight* |
| 185 | Window->SetHeight({height}) |
| 186 | Sets the Window height to {height}, within screen |
| 187 | limits. |
| 188 | |
| 189 | *perl-GetCursor* |
| 190 | Window->Cursor({row}?, {col}?) |
| 191 | With no arguments, returns a (row, col) array for the |
| 192 | current cursor position in the Window. With {row} and |
| 193 | {col} arguments, sets the Window's cursor position to |
| 194 | {row} and {col}. Note that {col} is numbered from 0, |
| 195 | Perl-fashion, and thus is one less than the value in |
| 196 | Vim's ruler. |
| 197 | |
| 198 | Window->Buffer() *perl-Buffer* |
| 199 | Returns the Buffer object corresponding to the given |
| 200 | Window. |
| 201 | |
| 202 | *perl-Name* |
| 203 | Buffer->Name() Returns the filename for the Buffer. |
| 204 | |
| 205 | *perl-Number* |
| 206 | Buffer->Number() Returns the number of the Buffer. |
| 207 | |
| 208 | *perl-Count* |
| 209 | Buffer->Count() Returns the number of lines in the Buffer. |
| 210 | |
| 211 | *perl-Get* |
| 212 | Buffer->Get({lnum}, {lnum}?, ...) |
| 213 | Returns a text string of line {lnum} in the Buffer |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 214 | for each {lnum} specified. An array can be passed |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 215 | with a list of {lnum}'s specified. |
| 216 | |
| 217 | *perl-Delete* |
| 218 | Buffer->Delete({lnum}, {lnum}?) |
| 219 | Deletes line {lnum} in the Buffer. With the second |
| 220 | {lnum}, deletes the range of lines from the first |
| 221 | {lnum} to the second {lnum}. |
| 222 | |
| 223 | *perl-Append* |
| 224 | Buffer->Append({lnum}, {line}, {line}?, ...) |
| 225 | Appends each {line} string after Buffer line {lnum}. |
| 226 | The list of {line}s can be an array. |
| 227 | |
| 228 | *perl-Set* |
| 229 | Buffer->Set({lnum}, {line}, {line}?, ...) |
| 230 | Replaces one or more Buffer lines with specified |
| 231 | {lines}s, starting at Buffer line {lnum}. The list of |
| 232 | {line}s can be an array. If the arguments are |
| 233 | invalid, replacement does not occur. |
| 234 | |
| 235 | $main::curwin |
| 236 | The current window object. |
| 237 | |
| 238 | $main::curbuf |
| 239 | The current buffer object. |
| 240 | |
| 241 | |
| 242 | *script-here* |
| 243 | When using a script language in-line, you might want to skip this when the |
| 244 | language isn't supported. But this mechanism doesn't work: > |
| 245 | if has('perl') |
| 246 | perl << EOF |
| 247 | this will NOT work! |
| 248 | EOF |
| 249 | endif |
| 250 | Instead, put the Perl/Python/Ruby/etc. command in a function and call that |
| 251 | function: > |
| 252 | if has('perl') |
| 253 | function DefPerl() |
| 254 | perl << EOF |
| 255 | this works |
| 256 | EOF |
| 257 | endfunction |
| 258 | call DefPerl() |
| 259 | endif |
| 260 | Note that "EOF" must be at the start of the line. |
| 261 | |
| 262 | vim:tw=78:ts=8:ft=help:norl: |