3

I do have a bunch of XHTML files and I need to put an @id for all the p elements inside all those files.

I have tried the following argdo approach in the following way:

:cd d:\testfiles\
:args *.xhtml
:argdo let i = 1 | g/<p/s/<p/\=printf("<p id=\"para%02d\"", i)/ | let i = i + 1

Now, this works fine. Doing it by this way value of i is getting incremented all the time. But, I want it to use unlet i, whenever argdo executes to a new buffer. In short, variable i must be set to 1 for each item in the arg-list.

I also have tried to use |unlet i at the end of the argdo statements or at the beginning of the argdo statements. But both ways have failed.


Is there any workaround? I am very new to vim. Using it only a few months and loving it day by day. Pointing out my error to get job done, is greatly appreciated.

3
  • 1
    @muru: thanks a ton! This is so easy...and I was heard of that...but never used it before...
    – Cylian
    Commented Feb 6, 2016 at 10:22
  • both the ways are failed is incorrect grammatically - it should be either both ways have failed, or just both ways failed.
    – muru
    Commented Feb 6, 2016 at 10:45
  • 1
    @muru: perhaps....you're right...
    – Cylian
    Commented Feb 6, 2016 at 10:48

1 Answer 1

4

When you use a variable without a scope (i instead of b:i or w:i, etc.) outside of a function, you're using a global variable. From :h internal-variables:

There are several name spaces for variables.  Which one is to be used is
specified by what is prepended:

                (nothing) In a function: local to a function; otherwise: global
buffer-variable    b:   Local to the current buffer.
window-variable    w:   Local to the current window.
tabpage-variable   t:   Local to the current tab page.
global-variable    g:   Global.
local-variable     l:   Local to a function.
script-variable    s:   Local to a :source'ed Vim script.
function-argument  a:   Function argument (only inside a function).
vim-variable       v:   Global, predefined by Vim.

A plain i is a variable common to all buffers. Since you need to reset the variable for each file, and each file will be in a separate buffer, you can use buffer-local variables (b:i instead of i).

1
  • 1
    Thanks for the explanation...now i understand where i was doing wrong...as i am already mentioned a very new to vim but very fond of it... i was regular user of "epsilon" (a emacs style editor), but i never have use before such a great editor like VIM...thanks again for your kind assistance...
    – Cylian
    Commented Feb 6, 2016 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.