How can I write a function to get the content between mark 1 and mark 2 for further processing? mark can be a string, or a function call, it doesn't matter.

mark 1


mark 2

1 Answer 1


See :h getbufline():

getbufline({expr}, {lnum} [, {end}])

Return a List with the lines starting from {lnum} to {end} (inclusive) in the buffer {expr}. If {end} is omitted, a List with only the line {lnum} is returned.

To get the line numbers you're looking for you can use :search() for example. See :h search().

For example in this file:

mark 1


mark 2

The command:

echo getbufline(bufnr('%'), 1, 7)

Will return the following list:

['mark 1', '', 'foo1', 'foo2', 'foo3', '', 'mark 2']

You might even want to wrap it in a function:

function! SearchForLines(pattern1, pattern2)
    let line1 = search(a:pattern1)
    let line2 = search(a:pattern2)

    return getbufline(bufnr('%'), line1, line2)

This way to get the list of the lines between mark 1 and mark 2 you can simply do:

:echo SearchForLines('mark 1', 'mark 2')

Edit Note that as @Christian Brabandt noted in the comments, the function getline() can also be used and in some circumstances may be easier to use. See :h getline().

  • 1
    you can also just ust getline() Nov 27, 2016 at 19:27
  • Absolutely I'll edit my answer :-)
    – statox
    Nov 28, 2016 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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