2

I am looking to insert list items

to-buy
> apple
> milk
> 

but I would like to insert a constant string (" > ") on each newline

is there a way to do this?

  • I'm not sure if I completely follow your question; what is your input, and what is the expected output? – Martin Tournoij Aug 30 '18 at 1:29
  • enter -> "\n > " – ebichuhamster Aug 30 '18 at 1:43
1

Well maybe it is not exactly what you want, but why not use such subst command after insert finished:

'[+1,']s/^/> /

Where

  • '[+1 is second inserted line number
  • '] is last inserted line number
  • s - subst command
  • ^ - start of line (position after "enter")
  • > - replacement text

You can map this to key for example:

nnoremap <F4> :'[+1,']s/^/> /<CR>
0

You can remap the behaviour of <cr> in insert-mode to a custom function that checks whether you are on a line starting with any amount of whitespace, then > then any other characters. If so, enter a new-line and start with >, otherwise enter a new-line as normal.

function! MakeNewLine()
    if getline(line('.')) =~# "^\s*>.*$"
        execute "normal! a\<cr>> "
        startinsert!
    else
        execute "normal! a\<cr>"
        normal! ^
        startinsert
    endif
endfunction
inoremap <cr> <esc>:call MakeNewLine()<cr>

Put this in your ~/.vimrc, ~/_vimrc or equivalent so it is run every time you run vim. When you enter a new-line in a > list in the middle of an entry, this also has the effect of splitting it into two entries.

  • 1
    I think we can simplify your code: see this gist. You don't need execute for your normal! commands, we can use o instead of a\<cr>, I don't think normal! ^ is useful here and if you use <c-o> instead of <esc> in your mapping you don't need startinsert anymore. (Also you may want to put the function in an autoload directory but that's not the most important.) – statox Aug 30 '18 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.