Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to display grid in Vim?

Tags:

vim

vim-plugin

I am using DrawIt plugin in Vim 7 to draw some ASCII diagrams.

This might be too much, but still—

Is there any plugin which can display a grid in background, to make the drawing easier?

like image 699
m.divya.mohan Avatar asked Dec 27 '22 20:12

m.divya.mohan


2 Answers

I can't add anything to @David and @romainl's thoughts (I think @romainl's suggestion of using a semi-transparent window with a grid behind it is inspired!).

However, you might find it easier to visualise the cursor position by using:

set cursorline
set cursorcolumn

Of course it's not a substitute for a true grid, but it will at least let you see at a glance the alignment of the cursor.

like image 60
Prince Goulash Avatar answered Jan 27 '23 05:01

Prince Goulash


Let me propose an implementation emulating the guiding grid using Vim highlighting features. The following function creates the necessary highlighting taking two mandatory arguments and another two optional ones. The former two are distances between horizontal and vertical lines, correspondingly. The latter arguments are the height and the width of the area covered with grid (in lines and characters, correspondingly). When these arguments are not specified the number of lines in the buffer and the length of the longest line in it are used.

function! ToggleGrid(...)
    if exists('b:grid_row_grp') || exists('b:grid_prev_cc')
        call matchdelete(b:grid_row_grp)
        let &colorcolumn = b:grid_prev_cc
        unlet b:grid_row_grp b:grid_prev_cc
        return
    endif

    let [dr, dc] = [a:1, a:2]
    if a:0 < 4
        let [i, nr, nc] = [1, line('$'), 0]
        while i <= nr
            let k = virtcol('$')
            let nc = nc < k ? k : nc
            let i += 1
        endwhile
    else
        let [nr, nc] = [a:3, a:4]
    endif
    let rows = range(dr, nr, dr)
    let cols = range(dc, nc, dc)

    let pat = '\V' . join(map(rows, '"\\%" . v:val . "l"'), '\|')
    let b:grid_row_grp = matchadd('ColorColumn', pat)
    let b:grid_prev_cc = &colorcolumn
    let &colorcolumn = join(cols, ',')
endfunction
like image 32
ib. Avatar answered Jan 27 '23 05:01

ib.