Reputation: 8172
I have the following function defined in my .vimrc
. For a given file, this should change the beginning of each line from the 3rd line onwards with the line number.
function Foo()
3,$ s/^/ /g
3
let i=1 | ,$ g/^/ s//\=i/ | let i+=1
1
endfunction
However, I want to change the function, so that it will accept one argument. It will insert that word, so that the function will look something as follows:
function Foo(chr)
3,$ s/^/ /g
3
let i=1 | ,$ g/^/ s//\=i/ | let i+=1
1
3,$ s/^/chr /g
endfunction
EDIT: Providing an example.
My input file looks something like this:
some text1
some text 2
0000
0000
0001
0002
I want to make the file look as follows:
sm1 1 0000
sm1 2 0000
sm1 3 0001
.
.
So i want to be able to give that "sm1" as a argument to the function, so that for another file i might want to have "sm2" instead of "sm1".
Upvotes: 0
Views: 191
Reputation: 31040
You probably don't need a function since
:3,$s/^/chr /
should work. However, if you wanted to make a command
for this you could make one like this:
command! -nargs=1 Example 3,$s/^/<args> /
This would allow you to use :Example chr
to insert chr
at the beginning of lines 3 and above.
Also, you said that your original function inserts the "line number", but instead it will insert 1 on line 3 and so on. I'm sure you know that you can enable line numbers with :set nu
, but if you want to insert line numbers on each line 3 and above you can do:
fun! Foo()
3,$s/^/\=line('.')." "
endfun
or if you want to keep your previous functionality, this is more succint:
fun! Foo()
3,$s/^/\=(line('.')-2)." "
endfun
If you want to combine all of it into one command you can do
com! -nargs=1 Example 3,$s/^/\="<args> ".(line('.')-2)." "
This will give you an :Example <argument>
command. So now you can do :Example sm1
like you wanted.
If you want to keep your function as is, to make it work you should use a:chr
like this:
function Foo(chr)
3,$ s/^/ /g
3
let i=1 | ,$ g/^/ s//\=i/ | let i+=1
1
exe "3,$s/^/".a:chr." /g"
endfunction
Upvotes: 2