4

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?

ib.
  • 27,830
  • 11
  • 80
  • 100
m.divya.mohan
  • 2,261
  • 4
  • 24
  • 34
  • 2
    I don't think it's possible without hacking the source or… making your terminal window semi transparent and placing such a grid behind. – romainl Aug 07 '12 at 11:53
  • 2
    I have not suggestions about a grid, but for drawing you may want to use `set virtualedit=all`. This will free your cursor so you can position your cursor where there is no actual character. See `:h 've'` – Peter Rincker Aug 07 '12 at 15:45

3 Answers3

8

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.

Prince Goulash
  • 15,295
  • 2
  • 46
  • 47
2

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
ib.
  • 27,830
  • 11
  • 80
  • 100
  • I copied this to .vimrc. I get errors while calling the function: `Error detected while processing function 19_ToggleGrid: line 8: E121: Undefined variable: a:1 E15: Invalid expression: [a:1, a:2] line 19: E121: Undefined variable: dr . .` – m.divya.mohan Aug 08 '12 at 12:47
  • 1
    @divya: Please specify distances between horizontal and vertical lines of the grid as arguments. (See the updated answer.) For example, run `:call ToggleGrid(3, 5)`. – ib. Aug 08 '12 at 13:44
  • thanks for the update. Now I am getting the error " Unknown function: matchadd". Observed that, another plugin that I am using has some code like 'check if matchadd exists, if so use it, else use 2match'. [MarkLines Plugin](http://www.vim.org/scripts/script.php?script_id=2028). Could you please help. – m.divya.mohan Aug 08 '12 at 13:58
0

I'm inclined to agree with @romainl; I can't think of any way to do this truly in Vim without mucking around with the source. However, I can think of a few workarounds.

  1. In many terminal emulators, you can set a background image. (xfce4-terminal has this feature, for example). You could design a background where the dimensions of each cell correspond to the space occupied by your monospace font.

  2. Nate Kane's vim-indent-guide might be helpful- it displays vertical lines you could use to align characters. See the screenshots page for some examples.

  3. You could abuse Vim's highlighting to simulate a grid of sorts.

David Cain
  • 16,484
  • 14
  • 65
  • 75