what is vim's printf, as it is in c

346 views
Skip to first unread message

Steve liu

unread,
Nov 8, 2011, 4:23:03 AM11/8/11
to v...@vim.org
Hi

I just want to print something to my text. 

1) is there a "printf" in vim command set. Like it works in C. 
2) I know there is one command named 'append', but it is not convenience to me. so where do I find others? 

thanks

--
steve
<sliu...@gmail.com>

Marc Weber

unread,
Nov 8, 2011, 6:25:43 AM11/8/11
to vim_use
Excerpts from Steve liu's message of Tue Nov 08 10:23:03 +0100 2011:

> 1) is there a "printf" in vim command set. Like it works in C.
Why do you have to ask the mailinglist? The first think you want to
learn is how to use the help: All functions end with () Thus do:

:h *print*()<c-d>

and you'll see printf(). Hit <tab><cr> and be happy.

> 2) I know there is one command named 'append', but it is not convenience to
> me. so where do I find others?

"But I don't like it - not telling you about what I would prefer" - How
do you expect anybody being able to help you ?
Either use loop or put or python scripting interface which has buffer
objects. or <c-r>=[1,2,3]<cr> (insert mode)..
Or <c-r>=myfun(input('enter something'))<cr>

Without knowing what you want to do its impossible to assist.

Eventually you're just looking for a snippet engine such as snipmate?

Marc Weber

Steve liu

unread,
Nov 8, 2011, 6:51:11 AM11/8/11
to vim...@googlegroups.com
1) yeah, I've really used help. But result is printed to status line not on the text. 
    what i want to do is to use vim internal function to print sth. on the text, editing.
    so...
2) though your word is kind of ... but you really warmheated. 
    you know i'm newer at vim. And no offense here. 
    sorry for the second time. and thanks very much. 


--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php



--
steve
<sliu...@gmail.com>

Taylor Hedberg

unread,
Nov 8, 2011, 7:11:26 AM11/8/11
to vim...@googlegroups.com
From insert mode, you can type:

<C-R>=printf('%s', 'foo')

And upon hitting enter, "foo" will appear after the cursor in the
current buffer. Is that the sort of thing you're trying to do?

Note that "<C-R>" is a literal Ctrl-R in the above example.

Marc Weber

unread,
Nov 8, 2011, 7:21:22 AM11/8/11
to vim_use
You're still failing. You're still making it hard for others to help.
Let me show you why.

Excerpts from Steve liu's message of Tue Nov 08 12:51:11 +0100 2011:


> 1) yeah, I've really used help. But result is printed to status line not on
> the text.

Tell people about what you've tried in your email. So your problem is
not printf, but "How to get text into the buffer" (which is something I
had to guess in the last mail)

> what i want to do is to use vim internal function to print sth. on the
> text, editing.

What. Think about Vim being human and imagine Vim understanding the
English language. Then explain to vim what it should do.
Copy paste this text into your reply to this mail. Then you'll be
offered many nice ways to get your job done.

What do you mean by "print" ? printf outputs to stdout. Vim can't do
that. So its not clear to me what you mean by "print" in the context of
editing text with a text editor.

You usually
"insert text into a text file at particular position/line/..."
"replace text"
"insert templates you prepared so that you can reuse those lines many
times without retyping"
" .. "

But you don't "printf" - you printf, then insert somewhere.
It looks like you know how to printf. So the remaining issue is how can
I insert a string into a buffer? Now how to determine at which location
the text should be inserted? Why does append() not suffice?
.. Lot's of lots of questions.

> 2) though your word is kind of ...

That's a very bad habit: You should *always* make clear to what you're
referring to. The most common way is "bottom posting" which means:
1) delete everything you don't reply to.
2) put your text below the text you're referring to.

So if you say 'your word' I don't know what you're referring to.
There have been many "words" in my mail. And if you want to talk about
the way I express myself (I'm not a native speaker) "wording" would have
been a better word to say this.


> sorry for the second time. and thanks very much.

You're welcome. Never feel sorry. Try to improve your communication.
Each additional round trip just means that you've forgotten to add some
important information others need to know before they can help you.

Marc Weber

Steve liu

unread,
Nov 8, 2011, 7:24:36 AM11/8/11
to vim...@googlegroups.com
sorry no. 

I was trying to print sth to my text by using external command line, which was

.!printf "%s\n" 'hello world'

but what I got is 'a.shs'

yeah my text is 'a.sh'. so a '%' means the editing text? 
I don't know what is wrong with my vim. so...

the following is my vim version compiled by me in CentOS6

====================================================
VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Aug 10 2011 17:24:52)
Included patches: 1-201, 203-212, 214-255, 257-269
Modified by <sl...@steve.org>
Compiled by <sl...@steve.org>
Huge version without GUI.  Features included (+) or not (-):
+arabic +autocmd -balloon_eval -browse ++builtin_terms +byte_offset +cindent +clientserver +clipboard +cmdline_compl +cmdline_hist +cmdline_info +comments +conceal +cryptv +cscope +cursorbind +cursorshape +dialog_con +diff +digraphs
-dnd -ebcdic +emacs_tags +eval +ex_extra +extra_search +farsi +file_in_path +find_in_path +float +folding -footer +fork() +gettext -hangul_input +iconv +insert_expand +jumplist +keymap +langmap +libcall +linebreak +lispindent +listcmds
+localmap -lua +menu +mksession +modify_fname +mouse -mouseshape +mouse_dec -mouse_gpm -mouse_jsbterm +mouse_netterm -mouse_sysmouse +mouse_xterm +multi_byte +multi_lang -mzscheme -netbeans_intg +path_extra +perl +persistent_undo
+postscript +printer +profile +python -python3 +quickfix +reltime +rightleft +ruby +scrollbind +signs +smartindent -sniff +startuptime +statusline -sun_workshop +syntax +tag_binary +tag_old_static -tag_any_white +tcl +terminfo
+termresponse +textobjects +title -toolbar +user_commands +vertsplit +virtualedit +visual +visualextra +viminfo +vreplace +wildignore +wildmenu +windows +writebackup +X11 +xfontset -xim -xsmp +xterm_clipboard -xterm_save
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
      user exrc file: "$HOME/.exrc"
  fall-back for $VIM: "/usr/local/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H     -g -O2 -D_FORTIFY_SOURCE=1     -I/usr/include  -D_LARGEFILE64_SOURCE=1
Linking: gcc   -L.  -rdynamic -Wl,-export-dynamic  -Wl,-E -Wl,-rpath,/usr/lib64/perl5/CORE   -L/usr/local/lib -Wl,--as-needed -o vim    -lSM -lICE -lXpm -lXt -lX11 -lXdmcp -lSM -lICE -lm -ltinfo -lelf   -lacl -lattr -ldl    -Wl,-E -Wl,-r
path,/usr/lib64/perl5/CORE  -fstack-protector  -L/usr/lib64/perl5/CORE -lperl -lresolv -lnsl -ldl -lm -lcrypt -lutil -lpthread -lc -L/usr/local/lib/python2.7/config -lpython2.7 -lpthread -ldl -lutil -lm -Xlinker -export-dynamic  -L/usr/l
ib64 -ltcl8.5 -ldl -lieee -lm -lruby -lpthread -lrt -ldl -lcrypt -lm  -L/usr/lib64
===================================================================================================================================================================



--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php



--
steve
<sliu...@gmail.com>

Marc Weber

unread,
Nov 8, 2011, 7:38:24 AM11/8/11
to vim_use
I still feel somewhat helpless.

I asked you for phrasing the task using english words by purpose.
Does this describe what you're trying to do:

'pipe current line to external command
replacing current line by its output.'
?

Then .!printf "\s\n" 'hello world'

is close. Now all I have to tell you is that % is special: Vim
substitutes it by the buffer name. Thus escape it by \ and you're done
or use <c-r>=system('your shell command')<cr> in insert mode.

No reread your first mail. Try to understand why I wasn't able to sent
you this small solution the first time. We'll all perform better then.

Sincerly
Marc Weber

Steve liu

unread,
Nov 8, 2011, 7:39:51 AM11/8/11
to vim...@googlegroups.com
yeah what you guess is 100 percent correct. 
I also noticed function 'append' 

I'll try to use it. 

thanks



Marc Weber

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php



--
steve
<sliu...@gmail.com>

Steve liu

unread,
Nov 8, 2011, 7:51:36 AM11/8/11
to vim...@googlegroups.com

firstly I want to direct stdout to buffer and then get it in text. but failed.

and now i'm trying to use external command. 

when i'm trying yours

.!printf "\s\n" 'hello world'

I've got this

"\s"

...

but when I type this directly in system ( printf "%s\n" 'hello world' ), it works. 
so is there a solution? 

ps: I remember that printf "%s\n" 'hello world' works @ CentOS 5. 
     I'll find one to test it

--
steve
<sliu...@gmail.com>

Marc Weber

unread,
Nov 8, 2011, 7:58:51 AM11/8/11
to vim_use
Excerpts from Steve liu's message of Tue Nov 08 13:39:51 +0100 2011:

> yeah what you guess is 100 percent correct.
> I also noticed function 'append'
getline('.') or line('.') will return current line

alternative:

:put='abc'
caution:
:put="a" string "a but register "a!

Marc Weber

unread,
Nov 8, 2011, 8:13:28 AM11/8/11
to vim_use
Excerpts from Steve liu's message of Tue Nov 08 13:51:36 +0100 2011:

> firstly I want to direct stdout to buffer and then get it in text. but
> failed.

redirect command and error to file
!command > file 2>&1

read file:

:r file

However then you can also use system, piping etc.

To understand the printf issue do:

:e NEW_FILE (Yes, I want you to open a buffer which is named NEW_FILE
sot hat you recognize this word easily)

Then try all of these:

:.!echo "%s\n" 'hello world'
:.!echo "%:ps\n" 'hello world'
:.!echo "\%s\n" 'hello world'
:.!echo "\s\n" 'hello world'

echo is just as printf. But it echoes arguments only - it doesn't
process them.

Now it you should understand what you're still doing wrong.

Marc Weber

Steve liu

unread,
Nov 8, 2011, 8:39:36 AM11/8/11
to vim...@googlegroups.com
you know in my code lots of are the same in pattern.
So what I want to do is to write a simple command there in case the pattern would be changed.

so system redirect is not my first choice. 

then I turned into external command in vim. but it's not gonna work. 

then I turned into vim internal command, and found that I don't know it very much.
googled a lot and trying a lot but still failed. I send a mail to this mail list. so... 

forgive me if I ... I'm not a native speaker. you're better than me. 

it's too late for me now.  

I'll try yours tomorrow. 

and thanks very much for your responses and such warm heart solutions.

I think I need to learn more about buffer usage in vim. 

thanks


Marc Weber

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php



--
steve
<sliu...@gmail.com>
338.gif

Christian Brabandt

unread,
Nov 8, 2011, 8:45:31 AM11/8/11
to vim...@googlegroups.com
On Tue, November 8, 2011 1:58 pm, Marc Weber wrote:
> caution:
> :put="a" string "a but register "a!

Actually, this will put into your buffer the last used expression (or
an empty line, if you haven't used the expression register yet), because
" terminates the command and indicates a comment following.

In command mode you need to escape the quotes.

regards,
Christian

Marc Weber

unread,
Nov 8, 2011, 8:57:47 AM11/8/11
to vim_use
In fact you're doing well. You've found a help channel.

Excerpts from Steve liu's message of Tue Nov 08 14:39:36 +0100 2011:


> then I turned into external command in vim. but it's not gonna work.
> then I turned into vim internal command, and found that I don't know it

Here I'm not sure I understand what you mean by internal vs external
command.
External command could be command line commands executed by shell.
internal commands are usually the :XXXX lines inside vim or .vim files.

> forgive me if I ... I'm not a native speaker. you're better than me. [?]
There is nothing I have to forgive. You didn't do anything wrong.
I'm trying to make you understand why I can't help you faster.

After waking up don't spend too much time. If you still don't get this
job done reply again - eventually also describing the patterns you're
talking about.
Also consider joining #vim on irc.freenode.net (any irc client will do)

Marc Weber

Steve liu

unread,
Nov 8, 2011, 11:47:10 PM11/8/11
to vim...@googlegroups.com
Hi all 

@Marc Weber

I finally got it. I don't know your words at all yesterday. So I learn 'register' and 'buffer' the whole morning. And finally got it.
I now know how to redirect output in vim. 

the followings are just three parts of generators of those huge codes . And they are in everywhere of my code. 
And I just redirect stdout to one register, and then get it.

again, thanks for your help

242   :let cmdpacket=['burstcount', 'agent', 'tag', 'space', 'row', 'col', 'threadid', 'execute']
243   :for i in cmdpacket
244   :  let str=printf( "  assign %-21s = wq_unload[0] ? ( wq_valid[1] ? %-16s : %-16s ) :", 'wq_'.i.'_next[0]', 'wq_'.i.'[1]', 'inreq_'.i )
245   :  let len=strlen( str )
246   :  let pattern=printf( "%%32s ~wq_valid[0] ? %%-%ds :", len-32-18 )
247   :  echo printf( "  assign %-21s = wq_unload[0] ? ( wq_valid[1] ? %-16s : %-16s ) :", 'wq_'.i.'_next[0]', 'wq_'.i.'[1]', 'inreq_'.i )
248   :  echo printf( pattern, ' ', 'inreq_'.i )
249   :  echo printf( "%47s wq_%s[0];", ' ', i )
250   :endfor

285   :let cmdpacket=['burstcount', 'agent', 'tag', 'space', 'row', 'col', 'threadid', 'execute']
286   :for i in cmdpacket
287   :  let str=printf( "  assign %-21s = |wq_unload[i:0] ? ( wq_valid[i+1] ? %-16s : %-16s ) :", 'wq_'.i.'_next[i]', 'wq_'.i.'[i+1]', 'inreq_'.i )
288   :  let len=strlen( str )
289   :  let pattern=printf( "%%32s ~wq_valid[i]    ? %%-%ds :", len-32-19 )
290   :  echo printf( "  assign %-21s = |wq_unload[i:0] ? ( wq_valid[i+1] ? %-18s : %-16s ) :", 'wq_'.i.'_next[i]', 'wq_'.i.'[i+1]', 'inreq_'.i )
291   :  echo printf( pattern, ' ', 'inreq_'.i )
292   :  echo printf( "%50s wq_%s[i];", ' ', i )
293   :endfor

436   :let cmdpacket=['burstcount', 'agent', 'tag', 'space', 'row', 'col', 'threadid', 'execute']
437   :for i in cmdpacket
438   :  let str=printf( "  assign %-21s = |rq_unload[i:0] ? ( rq_valid[i+1] ? %-16s : %-16s ) :", 'rq_'.i.'_next[i]', 'rq_'.i.'[i+1]', 'inreq_'.i )
439   :  let len=strlen( str )
440   :  let pattern=printf( "%%32s ~rq_valid[i]    ? %%-%ds :", len-32-19 )
441   :  echo printf( "  assign %-21s = |rq_unload[i:0] ? ( rq_valid[i+1] ? %-18s : %-16s ) :", 'rq_'.i.'_next[i]', 'rq_'.i.'[i+1]', 'inreq_'.i )
442   :  echo printf( pattern, ' ', 'inreq_'.i )
443   :  echo printf( "%50s rq_%s[i];", ' ', i )
444   :endfor


ps: 
what I do now is using internal commands, from vim. 

a "!" would invoke a system call as command line in vim, so, I call it external command. 

am I wrong? or? 



Marc Weber

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php



--
steve
<sliu...@gmail.com>

Marc Weber

unread,
Nov 8, 2011, 11:59:37 PM11/8/11
to vim_use
Excerpts from Steve liu's message of Wed Nov 09 05:47:10 +0100 2011:

> I finally got it. I don't know your words at all yesterday. So I learn
> 'register' and 'buffer' the whole morning. And finally got it.
> I now know how to redirect output in vim.

It doesn't matter how you did it. It matters that you did it.
Congratulation!

Keep in Mind that VimL is nice for scripting tasks but doesn't scale
very well. Thus for larger projects consider interfacing with another
language such as python, perl, ruby, (there are countless others)

Marc Weber

Steve liu

unread,
Nov 9, 2011, 12:07:24 AM11/9/11
to vim...@googlegroups.com



Marc Weber

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php



--
steve
<sliu...@gmail.com>
338.gif

John Beckett

unread,
Nov 9, 2011, 5:23:07 AM11/9/11
to vim...@googlegroups.com
Please bottom post on this mailing list.

Quote a small (relevant) part of the message you are replying to,
and put your text underneath.

Delete text that is not needed to understand your reply

Note the footer below each message ("Do not top-post!").

John

Ben Fritz

unread,
Nov 9, 2011, 11:29:30 PM11/9/11
to vim_use


On Nov 8, 11:07 pm, Steve liu <sliu....@gmail.com> wrote:
> On Wed, Nov 9, 2011 at 12:59 PM, Marc Weber <marco-owe...@gmx.de> wrote:
> > Excerpts from Steve liu's message of Wed Nov 09 05:47:10 +0100 2011:
> > > I finally got it. I don't know your words at all yesterday. So I learn
> > > 'register' and 'buffer' the whole morning. And finally got it.
> > > I now know how to redirect output in vim.
>
> > It doesn't matter how you did it. It matters that you did it.
> > Congratulation!
>
> > Keep in Mind that VimL is nice for scripting tasks but doesn't scale
> > very well. Thus for larger projects consider interfacing with another
> > language such as python, perl, ruby, (there are countless others)
>
> [?]
>

Probably Marc is referring to a few things:

1. Vim's internal scripting language (sometimes called either VimL or
vimscript) can be hard to use, because a large number of user settings
can affect script execution
2. VimL is often slower than other interpreted languages such as
python, perl, ruby, tcl, scheme, etc. and almost always slower than
compiled languages like C or C++.
3. Other modern languages have more support/examples/read-made
libraries (though probably not more documentation), more common syntax/
semantics, better debugging support, cleaner object-oriented methods,
etc.
4. All those interpreted languages I mentioned above have an
interface which can be used from within Vim to run a script in this
other language and control Vim functionality. Some very impressive
plugins are written in other languages, e.g. the gundo plugin, written
mostly in python (I believe partially to make it easier to pull in
tree-rendering code from Mercurial's source code).

So Marc is recommending, if you do anything "heavy", consider a
different language than Vim's internals.

That said, I've never felt the need to resort to a different language
when hacking Vim.

Tony Mechelynck

unread,
Nov 10, 2011, 3:00:51 PM11/10/11
to vim...@googlegroups.com, Ben Fritz, sliu...@gmail.com

Neither have I; and it should also be remembered that Vim is often
compiled without MzScheme, or without Python, etc., but never without
Vim-script capability � indeed, the latter cannot be left out at
compile-time. Parts of it can: for instance the stripped-down Vim
installed as "vi" on some Linux distros comes with (among others) no
syntax highlighting, no split windows and no arithmetic evaluation � but
it will still accept vim-script language (of a kind) in its vimrc.


Best regards,
Tony.
--
A chubby man with a white beard and a red suit will approach you soon.
Avoid him. He's a Commie.

Reply all
Reply to author
Forward
0 new messages