Vim obtain string between visual selection range with Python

python, vim

Solution

Try this:

fun! GetRange()
python << EOF

import vim

buf = vim.current.buffer
(lnum1, col1) = buf.mark('<')
(lnum2, col2) = buf.mark('>')
lines = vim.eval('getline({}, {})'.format(lnum1, lnum2))
lines[0] = lines[0][col1:]
lines[-1] = lines[-1][:col2]
print "\n".join(lines)

EOF
endfun

You can use `vim.eval` to get python values of vim functions and variables.

Problem

``` Here is some text here is line two of text ``` I visually select from `is` to `is` in Vim: (brackets represent the visual selection `[` `]`) ``` Here [is some text here is] line two of text ``` Using Python, I can obtain the range tuples of the selection: ``` function! GetRange() python << EOF import vim buf = vim.current.buffer # the buffer start = buf.mark('<') # start selection tuple: (1,5) end = buf.mark('>') # end selection tuple: (2,7) EOF endfunction ``` I source this file: `:so %`, select the text visually, run `:<,'>call GetRange()` and now that I have `(1,5)` and `(2,7)`. In Python, how can I compile the string that is the following: `is some text\nhere is` Would be nice to: - Obtain this string for future manipulation - then replace this selected range with the updated/manipulated string

Original source

Related problems