Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim - if/elseif/else statements in for loop (command mode)

Tags:

vim

I'm trying to generate a bunch of text in vim (command mode) using for loops, e.g.

:for i in range(1,10) | put=i | endfor

this outputs 12345678910

I want to add logic inside of the for loop like the following pseudo code:

:for i in range(1,10) | if i>5 put=i endif | endfor

My issue is that, after exhausting google searches, I am unable to find the proper syntax for producing this kind of if statement. Does anyone know how to perform if, elseif and/or else statements in vim's command mode?

Edit: so I finally found vimscript

so now I have:

func! Test()
    for i in range(1,10)
        for j in range(1,10)
            if i<10
                echo i*j
            endif
        endfor
    endfor
endfunction

so i can :call Test()

which outputs 12345678910, but it doesn't insert it into the page..

like image 622
tester Avatar asked Dec 17 '22 12:12

tester


1 Answers

Every if/elseif/else/endif is a command on its own, so on one line that would be:

:for i in range(1,10) | if i > 5 | put =i | endif | endfor
like image 196
Raimondi Avatar answered Feb 01 '23 03:02

Raimondi