0

I have the following snippet:

snippet ta "table" b
.${1:Title}
[${2:width="${3:80%}",cols="${4:}",frame="topbot",options="header",stripe="even"}]                                            
|====                                   
|$0                                                         
|====      

But once I tab up to the $0, I want to insert 'a|sometext' at every new line. I would prefer using tab or enter. How can I achieve this? Thanks for any suggestions.

Boyd
  • 351
  • 4
  • 14
  • Can you be a bit more specific? Looks like you're typesetting a markup table here. Do you want to add columns? Do you want to add fields for the cells? Does it depend on how "cols" is populated? Can you give an example of final result and what's populated by UltiSnips? – filbranden May 01 '20 at 15:32
  • Its a table in asciidoc but it could be anything. Ive inserted a snippet and filled in the variables. I get to the last $0. I then want to insert ^a|sometext| for every new line. So its like I need a second snippet that adds ^a|sometext every time i press enter (or tab) untill I leave the $0 – Boyd May 01 '20 at 19:47
  • Is that a literal thing to insert? Like when you're on `$0` and you type `my text` then enter you get a new line with `a|something`? Sorry but it's still unclear what you're trying to accomplish. (Or maybe I don't know enough asciidoc...) If you could [edit] the question and break it down, you might be able to attract useful answers. Be as detailed as you can. – filbranden May 01 '20 at 20:23
  • You might also want to consider [vi.se] for questions on Vim. Asking them on Stack Overflow is typically OK, but at [vi.se] you'll normally get more Vim experts hanging around and usually you get faster response times. – filbranden May 01 '20 at 20:24

1 Answers1

0

I don't believe that's doable with Ultisnips, unfortunately.

You can modify the mirrors of the input, but not the input itself. You could use a post expand python function, but that seem overly complicated for your problem.

From my point of view, the simplest solution is to wrap your <CR> (=enter) key into a vim function, and to add a | character when necessary:

function HandleCR()
    if getline(line('.')) =~ '^|'
        return "\<CR>|"
    endif
    return "\<CR>"
endfunction
inoremap <buffer><silent> <CR> <C-r>=HandleCR()<CR>

(to put in your .vimrc, not in the snippet file)

Note that this will always work, even when out of a snippet

Zorzi
  • 718
  • 4
  • 9