JamesMcCoy (talk | contribs) |
(Insert TipProposed template + minor manual clean) |
||
Line 1: | Line 1: | ||
+ | {{TipProposed |
||
− | == No `backwhacks` in Vim == |
||
+ | |id=0 |
||
+ | |previous=0 |
||
+ | |next=0 |
||
+ | |created=February 9, 2010 |
||
+ | |complexity=basic |
||
+ | |author=Perlsomian |
||
+ | |version=7.0 |
||
+ | |subpage=/201002 |
||
+ | |category1= |
||
+ | |category2= |
||
+ | }} |
||
+ | The sample function shown below can be used in a Vim script to capture the output of an external command run by the system shell. The function appends the output of the command (if successfully executed) to the current line. |
||
+ | Vim does not include the backquote syntax (aka backtick, backwhack, grave accent) that many Unix shells provide to capture command output. This function shows how to perform the equivalent operation using the <tt>system()</tt> Vim function. |
||
− | This sample function for a .vim script file illustrates the mechanism I worked out |
||
− | for capture of the output of external system commands (run by the system shell). It |
||
− | appends the output of the command (assuming the command is successfully executed) |
||
− | to the current line. |
||
− | |||
− | A shell of any '''sh'''-like type (a unix shell) has the backquotes syntax available as an |
||
− | easy means to ''capture'' the output of a command. Left-leaning ''grave accents'', backticks, backwhacks, |
||
− | etc. are other names for this punctuation. Vim scripting lacks this facility and so we |
||
− | might use the kind of approach illustrated below. |
||
− | |||
− | Please do make yourself familiar with the vim '''strftime()''' function before simply |
||
− | copying this exact example into your working scripts. That function may do all you |
||
− | need and is certainly a better choice than calling out to a system command. |
||
+ | The following example captures the output of the shell's <tt>date</tt> command. This is just an example: using Vim's <tt>strftime()</tt> function would be a better choice if you need the [[Insert current date or timei|date or time]] in a script. |
||
<pre> |
<pre> |
||
⚫ | |||
− | func! GimmeADate(format) |
||
+ | function! GetDate(format) |
||
− | + | let utilpath = '/bin/' |
|
− | + | let fmt = a:format ? a:format : '+%A m%m/d%d/y%Y %R UTC' |
|
− | + | let SPECIALCHARS = 1 |
|
− | + | let TemporalFormat = shellescape(fmt, SPECIALCHARS) |
|
− | + | let DStamp = substitute(system(utilpath .'date -u '. TemporalFormat) |
|
− | + | \ , '[\]\|[[:cntrl:]]', '', 'g') |
|
− | " Moves user's cursor: exec 'normal A'.DStamp |
||
− | + | " Append result to current line without moving cursor. |
|
− | + | call setline(line('.'), getline('.') . DStamp) |
|
+ | endfunction |
||
− | endfu |
||
− | |||
− | " Suggested mapping: |
||
− | " This thing is 'CTRL-&' |
||
⚫ | |||
</pre> |
</pre> |
||
− | + | ==Comments== |
|
⚫ | |||
⚫ | |||
− | |||
− | --[[User:Perlsomian|Perlsomian]] 19:42, February 11, 2010 (UTC) Thank you, JamesVega, suggestion was taken. |
||
− | |||
− | == Comments == |
||
− | |||
⚫ | |||
<pre> |
<pre> |
||
:redir => myvariable |
:redir => myvariable |
||
Line 65: | Line 56: | ||
</pre> |
</pre> |
||
− | :Doesn't the <tt>:command</tt> need to use the <tt>-bar</tt> flag here so it will see a <tt>|</tt> as one of its arguments? |
+ | :Doesn't the <tt>:command</tt> need to use the <tt>-bar</tt> flag here so it will see a <tt>|</tt> as one of its arguments? Without that, I don't think the <pre>:R find | xargs grep vim</pre> example will work. That should also remove the need for the use of <tt>:exe</tt>. |
− | : |
+ | :[[User:JamesVega|JamesVega]] 15:52, February 17, 2010 (UTC) |
⚫ | |||
+ | :::TODO: Determine whether any of these comments are now redundant; if so, delete them (but keep the "Comments" header). [[User:JohnBot|JohnBot]] 07:25, March 11, 2010 (UTC) |
Revision as of 07:25, 11 March 2010
Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).
The sample function shown below can be used in a Vim script to capture the output of an external command run by the system shell. The function appends the output of the command (if successfully executed) to the current line.
Vim does not include the backquote syntax (aka backtick, backwhack, grave accent) that many Unix shells provide to capture command output. This function shows how to perform the equivalent operation using the system() Vim function.
The following example captures the output of the shell's date command. This is just an example: using Vim's strftime() function would be a better choice if you need the date or time in a script.
nnoremap <F8> :call GetDate('')<CR> function! GetDate(format) let utilpath = '/bin/' let fmt = a:format ? a:format : '+%A m%m/d%d/y%Y %R UTC' let SPECIALCHARS = 1 let TemporalFormat = shellescape(fmt, SPECIALCHARS) let DStamp = substitute(system(utilpath .'date -u '. TemporalFormat) \ , '[\]\|[[:cntrl:]]', '', 'g') " Append result to current line without moving cursor. call setline(line('.'), getline('.') . DStamp) endfunction
Comments
How about
:redir => myvariable :!date :redir END :echo myvariable
Simply
:r !date
If you want capture output to new unnamed buffer and
command! -nargs=* -complete=shellcmd R exe "new | setlocal buftype=nofile | setlocal bufhidden=hide | setlocal noswapfile | r !<args>" "and now you can :R find | xargs grep vim "or :R ls -la "or whatever :R mysql -vvt -u root -e'show databases'
- Doesn't the :command need to use the -bar flag here so it will see a | as one of its arguments? Without that, I don't think the
:R find | xargs grep vim
example will work. That should also remove the need for the use of :exe.
- JamesVega 15:52, February 17, 2010 (UTC)
- Thank you, JamesVega, suggestion was taken. Perlsomian 19:42, February 11, 2010 (UTC)
- TODO: Determine whether any of these comments are now redundant; if so, delete them (but keep the "Comments" header). JohnBot 07:25, March 11, 2010 (UTC)
- Thank you, JamesVega, suggestion was taken. Perlsomian 19:42, February 11, 2010 (UTC)