vim + c++: insert a uuid in a guard clause
Asked Answered
F

2

6

I'm trying to automate file comment headers. I'm stuck trying to figure out how to insert the result of the uuidgen command into my header using vim's autocmd.

Inside the header, the placeholder text is present, like this:

#ifndef _UUID_
#define _UUID_

// Code goes here!

#endif // _UUID_

The autocmd line to populate _UUID_ in .vimrc is:

autocmd bufnewfile *.h exe "1,$s/_UUID_/" . r!uuidgen ."/g"

The problem is coming in under r!uuidgen. How do i insert the result of a shell-command-execution as text in the autocmd line? Or in a vi substitution command for that matter?

Fogbow answered 5/5, 2010 at 15:10 Comment(0)
I
5

Use system(), and don't forget to chomp the result -> matchstr(system('uuidgen'), "[^\n\r]*")

NB: For more complex templates, you could use solutions like mu-template. For instance, in c-header.template, you'd have had to change the value of s:guard to the call to matchtr()+system().

Illa answered 5/5, 2010 at 15:21 Comment(1)
Worked, many thanks! Will have to checkout your templates sometime.Fogbow
F
1

My resulting autocmd line after Luc's suggestion was, for posterity sake:

autocmd bufnewfile *.h exe "1,$g/_UUID_/s/_UUID_/" . matchstr(system('uuidgen'), "[^\n\r]*")
Fogbow answered 5/5, 2010 at 15:33 Comment(2)
A simple "%s/_UUID_" should be enough. However, I'm not sure about the order in which several autocommands are executed. That's one of the reasons I always prefer to rely on functions (or ftplugins, or local vimrcs) where autocommands are concerned.Illa
@Luc Hermitte - My vim-fu is improving. Thanks for the tips.Fogbow

© 2022 - 2024 — McMap. All rights reserved.