Skip to main content
added 126 characters in body
Source Link
statox
  • 50.5k
  • 19
  • 153
  • 231

Here are two functions that you could use. Both takes as argument a pattern to search and a command to execute on the consecutive lines matching the pattern.

You can call them with:

call RangeFromPattern("^\s*-", "!sort -u")

The first one goes to the first line of the buffer, searches for the first line matching the pattern, then goes to the last line of the buffer, searches for the last line matching the pattern and then apply the command to the range firstline,lastline.

Note that if you buffer contains several sections like this:

1. First line
2. - matching pattern
3. - matching pattern
4. not matching pattern
5. - matching pattern
6. - matching pattern
7. Last line

The command will be applied to lines 2 to 6 (including the line 4 which doesn't match the pattern)

function! RangeFromPattern(pattern, command)
    " Save cursor position
    let saveCursor = getcurpos()

    " Get the first line matching the pattern
    call cursor(1, 1)
    let firstLine = search(a:pattern, "c")

    " Get the last line matching the pattern
    call cursor('$', 1)
    let lastLine = search(a:pattern, "b")

    " Restore the pattern position
    call setpos('.', saveCursor)

    " Execute the command
    execute firstLine . "," . lastLine . a:command
endfunction

Now to avoid the problem of the previous function here is another version which will start the search on the current line, keep the number of the first matching line, increment this number while the next lines matches the pattern and finally apply the command to this set of lines. On the previous example I gave, if you call the function with your cursor on line 5, the command will be applied on range 5 to 6.

function! RangeFromPattern2(pattern, command)
    " Save cursor position
    let saveCursor = getcurpos()

    " Get the first line matching the pattern
    let firstLine = search(a:pattern, "c")

    " Get the contiguous lines matching the pattern
    let prevLine = firstLine
    while (search(a:pattern) == prevLine + 1)
        let prevLine += 1
    endwhile

    " Restore the pattern position
    call setpos('.', saveCursor)

    " Execute the command
    execute firstLine . "," . prevLine . a:command
endfunction
ndfunction

An example of the function on your example:

enter image description here

Here are two functions that you could use. Both takes as argument a pattern to search and a command to execute on the consecutive lines matching the pattern.

You can call them with:

call RangeFromPattern("^\s*-", "!sort -u")

The first one goes to the first line of the buffer, searches for the first line matching the pattern, then goes to the last line of the buffer, searches for the last line matching the pattern and then apply the command to the range firstline,lastline.

Note that if you buffer contains several sections like this:

1. First line
2. - matching pattern
3. - matching pattern
4. not matching pattern
5. - matching pattern
6. - matching pattern
7. Last line

The command will be applied to lines 2 to 6 (including the line 4 which doesn't match the pattern)

function! RangeFromPattern(pattern, command)
    " Save cursor position
    let saveCursor = getcurpos()

    " Get the first line matching the pattern
    call cursor(1, 1)
    let firstLine = search(a:pattern, "c")

    " Get the last line matching the pattern
    call cursor('$', 1)
    let lastLine = search(a:pattern, "b")

    " Restore the pattern position
    call setpos('.', saveCursor)

    " Execute the command
    execute firstLine . "," . lastLine . a:command
endfunction

Now to avoid the problem of the previous function here is another version which will start the search on the current line, keep the number of the first matching line, increment this number while the next lines matches the pattern and finally apply the command to this set of lines. On the previous example I gave, if you call the function with your cursor on line 5, the command will be applied on range 5 to 6.

function! RangeFromPattern2(pattern, command)
    " Save cursor position
    let saveCursor = getcurpos()

    " Get the first line matching the pattern
    let firstLine = search(a:pattern, "c")

    " Get the contiguous lines matching the pattern
    let prevLine = firstLine
    while (search(a:pattern) == prevLine + 1)
        let prevLine += 1
    endwhile

    " Restore the pattern position
    call setpos('.', saveCursor)

    " Execute the command
    execute firstLine . "," . prevLine . a:command
endfunction
ndfunction

Here are two functions that you could use. Both takes as argument a pattern to search and a command to execute on the consecutive lines matching the pattern.

You can call them with:

call RangeFromPattern("^\s*-", "!sort -u")

The first one goes to the first line of the buffer, searches for the first line matching the pattern, then goes to the last line of the buffer, searches for the last line matching the pattern and then apply the command to the range firstline,lastline.

Note that if you buffer contains several sections like this:

1. First line
2. - matching pattern
3. - matching pattern
4. not matching pattern
5. - matching pattern
6. - matching pattern
7. Last line

The command will be applied to lines 2 to 6 (including the line 4 which doesn't match the pattern)

function! RangeFromPattern(pattern, command)
    " Save cursor position
    let saveCursor = getcurpos()

    " Get the first line matching the pattern
    call cursor(1, 1)
    let firstLine = search(a:pattern, "c")

    " Get the last line matching the pattern
    call cursor('$', 1)
    let lastLine = search(a:pattern, "b")

    " Restore the pattern position
    call setpos('.', saveCursor)

    " Execute the command
    execute firstLine . "," . lastLine . a:command
endfunction

Now to avoid the problem of the previous function here is another version which will start the search on the current line, keep the number of the first matching line, increment this number while the next lines matches the pattern and finally apply the command to this set of lines. On the previous example I gave, if you call the function with your cursor on line 5, the command will be applied on range 5 to 6.

function! RangeFromPattern2(pattern, command)
    " Save cursor position
    let saveCursor = getcurpos()

    " Get the first line matching the pattern
    let firstLine = search(a:pattern, "c")

    " Get the contiguous lines matching the pattern
    let prevLine = firstLine
    while (search(a:pattern) == prevLine + 1)
        let prevLine += 1
    endwhile

    " Restore the pattern position
    call setpos('.', saveCursor)

    " Execute the command
    execute firstLine . "," . prevLine . a:command
endfunction

An example of the function on your example:

enter image description here

Source Link
statox
  • 50.5k
  • 19
  • 153
  • 231

Here are two functions that you could use. Both takes as argument a pattern to search and a command to execute on the consecutive lines matching the pattern.

You can call them with:

call RangeFromPattern("^\s*-", "!sort -u")

The first one goes to the first line of the buffer, searches for the first line matching the pattern, then goes to the last line of the buffer, searches for the last line matching the pattern and then apply the command to the range firstline,lastline.

Note that if you buffer contains several sections like this:

1. First line
2. - matching pattern
3. - matching pattern
4. not matching pattern
5. - matching pattern
6. - matching pattern
7. Last line

The command will be applied to lines 2 to 6 (including the line 4 which doesn't match the pattern)

function! RangeFromPattern(pattern, command)
    " Save cursor position
    let saveCursor = getcurpos()

    " Get the first line matching the pattern
    call cursor(1, 1)
    let firstLine = search(a:pattern, "c")

    " Get the last line matching the pattern
    call cursor('$', 1)
    let lastLine = search(a:pattern, "b")

    " Restore the pattern position
    call setpos('.', saveCursor)

    " Execute the command
    execute firstLine . "," . lastLine . a:command
endfunction

Now to avoid the problem of the previous function here is another version which will start the search on the current line, keep the number of the first matching line, increment this number while the next lines matches the pattern and finally apply the command to this set of lines. On the previous example I gave, if you call the function with your cursor on line 5, the command will be applied on range 5 to 6.

function! RangeFromPattern2(pattern, command)
    " Save cursor position
    let saveCursor = getcurpos()

    " Get the first line matching the pattern
    let firstLine = search(a:pattern, "c")

    " Get the contiguous lines matching the pattern
    let prevLine = firstLine
    while (search(a:pattern) == prevLine + 1)
        let prevLine += 1
    endwhile

    " Restore the pattern position
    call setpos('.', saveCursor)

    " Execute the command
    execute firstLine . "," . prevLine . a:command
endfunction
ndfunction