66

I'm able to get the cursor position with getpos(), but I want to retrieve the selected text within a line, that is '<,'>. How's this done?

UPDATE

I think I edited out the part where I explained that I want to get this text from a Vim script...

3
  • 10
    If only one line is selected, then :echo getline("'<")[getpos("'<")[2]-1:getpos("'>")[2]] is enough.
    – kev
    Commented Dec 8, 2011 at 12:53
  • Trying this answer almost a decade later. I preferred this answer since it's much shorter and easier to use in my key mapping. However, this gives me an extra character at the end. I'm not proficient in vim syntax so I just hacked it by inserting a backspace <BS> to remove the extra character.
    – Ken Lin
    Commented Jul 8, 2020 at 0:19
  • 1
    this worked for me (and fixes the trailing char): :echo getline("'<")[getpos("'<")[2]-1:getpos("'>")[2]-1] Commented Sep 29, 2021 at 13:53

10 Answers 10

107

I came here asking the same question as the topic starter and tried the code by Luc Hermitte but it didn't work for me (when the visual selection is still in effect while my code is executed) so I wrote the function below, which seems to work okay:

function! s:get_visual_selection()
    let [line_start, column_start] = getpos("'<")[1:2]
    let [line_end, column_end] = getpos("'>")[1:2]
    let lines = getline(line_start, line_end)
    if len(lines) == 0
        return ''
    endif
    let lines[-1] = lines[-1][: column_end - 2]
    let lines[0] = lines[0][column_start - 1:]
    return join(lines, "\n")
endfunction

I hope this is useful to someone!

Update (May 2013): Actually that's not quite correct yet, I recently fixed the following bug in one of the Vim plug-ins I published:

function! s:get_visual_selection()
    " Why is this not a built-in Vim script function?!
    let [line_start, column_start] = getpos("'<")[1:2]
    let [line_end, column_end] = getpos("'>")[1:2]
    let lines = getline(line_start, line_end)
    if len(lines) == 0
        return ''
    endif
    let lines[-1] = lines[-1][: column_end - (&selection == 'inclusive' ? 1 : 2)]
    let lines[0] = lines[0][column_start - 1:]
    return join(lines, "\n")
endfunction

Update (May 2014): This (trivial) code is hereby licensed as public domain. Do with it what you want. Credits are appreciated but not required.

9
  • 3
    @IdanArye: I don't mind at all, do with it what you want :-). I've added a clarification to my original post.
    – xolox
    Commented May 15, 2014 at 19:32
  • 2
    @xolox Thanks! I'm giving credit by linking to your answer. github.com/someboddy/vim-vebugger/blob/master/autoload/vebugger/…
    – Idan Arye
    Commented May 16, 2014 at 20:31
  • 3
    Question: It's been mentioned elsewhere that '< and '> don't update until after the selection has been left. Does that batter here? Should it be v and . instead?
    – anon
    Commented Nov 20, 2015 at 1:33
  • @QPaysTaxes I tried with v and . but found i couldn't get the selection, like it was gone when my function started executing, so what worked for me was to just clear the selection myself (because visually the selection was gone anyway) function! s:clear_visual_selection() call setpos("'<", [0, 0, 0, 0]) call setpos("'>", [0, 0, 0, 0]) endfunction Commented Aug 10, 2017 at 22:15
  • 1
    if (line2byte(line_start)+column_start) > (line2byte(line_end)+column_end) | let [line_start, column_start, line_end, column_end] = [line_end, column_end, line_start, column_start] you can actually select text backwards in visual mode; so you should account for that in the function unless you explicitly want an empty string to be returned in those cases. You could even reverse the returned string with join(reverse(split(str, ".\\zs"))) if you wanted it to work like that. Commented Nov 1, 2017 at 8:11
27

On Linux, there is a cheap but effective alternative to programming such a GetVisualSelection() function yourself: use the * register!

The * register contains the content of the most recent Visual selection. See :h x11-selection.

In your script you could then simply access @* to get the Visual selection.

let v = @*

Incidentally, * is also a neat little helper in interactive use. For example, in insert mode you can use CTRL-R * to insert what you had selected earlier. No explicit yanking involved.

This works only on operating systems that support the X11 selection mechanism.

4
  • 6
    This only works if vim is compiled with the +x11 option I think?
    – ffledgling
    Commented Feb 5, 2014 at 22:09
  • 2
    Yeah, I get E354: Invalid register name: '*' on Ubuntu. Commented Jul 17, 2014 at 15:54
  • 2
    If your vim doesn't have the @* x11-selection feature then an alternative that does the same thing is to do a normal "ay to stuff the visual selection into register a, then do a let visually_selected_text = getreg('s', 1, 1) See: vi.stackexchange.com/questions/12004/… Commented Nov 27, 2018 at 22:54
  • This is so useful, I think Vim should simulate the * register for non-Unix platforms! Commented Aug 12, 2021 at 9:17
21

The best way I found was to paste the selection into a register:

function! lh#visual#selection()
  try
    let a_save = @a
    normal! gv"ay
    return @a
  finally
    let @a = a_save
  endtry
endfunction
3
  • 1
    It will make NULLs indistinguishable with NLs. You can try a function from my answer in your library, if you need I can also create a function for blockwise selection, or you can look at translitselection function.
    – ZyX
    Commented Jun 8, 2011 at 9:57
  • 1
    I tested this, I use it in a mapping of / to search for the selection. Then I have to use normal! "ay instead of normal! gv"ay otherwise it doesn't use current selection. If you use it after you leave the visual mode it's correct with gv"ay. Perhaps it's good to make a note in your answer (took me a while to figure it out), you got +1 anyway. (And I use return substitute(@a, '\n', '\\n', 'g') to be able to search if more than one line in selection).
    – 244an
    Commented Feb 25, 2013 at 23:03
  • 1
    That's not the role of this function to escape the characters in the selection when we want to use them to search for something. BTW, you would have to escape many other things like /, ``, etc. Commented Feb 26, 2013 at 13:13
17

This is quite an old question, but since I can imagine lots of people will come across it at some point, here's my modified version of @xolox answer

function! VisualSelection()
    if mode()=="v"
        let [line_start, column_start] = getpos("v")[1:2]
        let [line_end, column_end] = getpos(".")[1:2]
    else
        let [line_start, column_start] = getpos("'<")[1:2]
        let [line_end, column_end] = getpos("'>")[1:2]
    end
    if (line2byte(line_start)+column_start) > (line2byte(line_end)+column_end)
        let [line_start, column_start, line_end, column_end] =
        \   [line_end, column_end, line_start, column_start]
    end
    let lines = getline(line_start, line_end)
    if len(lines) == 0
            return ''
    endif
    let lines[-1] = lines[-1][: column_end - 1]
    let lines[0] = lines[0][column_start - 1:]
    return join(lines, "\n")
endfunction
  1. '< and '> don't get updated when the user is still in visual mode, thus . and v need to be used in that case.
  2. It is possible to select text backwards in visual mode, which means '> comes before '< in the text. In those cases the two positions simply need to be reversed.
  3. While this isn't in my version of the function, one could choose to reverse the string if the selection was backwards. Here's a snipped that shows how to do this.

Assuming the variable "reverse" is defined when the marks are in reverse order:

if exists("reverse")
    let lines_r = []
    for line in lines
        call insert(lines_r, join(reverse(split(line, ".\\zs"))))
    endfor
    return join(lines_r, "\n")
else
    return join(lines, "\n")
end
2
  • Modified this solution with a fix for when using :behave mswin and added support for visual block selection as well (please see my answer on the page).
    – pev.hall
    Commented Nov 2, 2020 at 4:22
  • 1
    the use of getpos('v') is critical if you want the current selection (say within a map expression) as opposed to the last selection. such maps need an <esc>gv prefix otherwise. additionally, if you separate the function into range and text components, you can just sort the range components rather than reverse the text, which should be faster. similar is done in the solution from the vim-asterisk plugin as shared by Enno.
    – pkfm
    Commented May 6, 2021 at 2:38
11

I'm not totally sure about the context here, because getpos() can indeed accept marks (like '< and '>) as arguments.

However, to take a stab at what you might be asking for, there's also v, which is like '< except it's always updated (i.e. while the user is still in visual mode). This can be used in combination with ., the current cursor position, which will then represent the end of the visual selection.

Edit: I found these in :help line(); several functions including line() and getpos() have the same set of possible arguments.

Edit: I guess you're probably simply asking how to get the text between two arbitrary marks, not going line-by-line... (i.e. this doesn't specifically pertain to visual mode). I don't think there actually is a way. Yes, this seems like a pretty glaring omission. You should be able to fake it by finding the marks with getpos(), getting all the lines with getline(), then chopping off on the first and last according to the column position (with casework depending on whether or not it's multi-line). Sorry it's not a real answer, but at least you can wrap it up in a function and forget about it.

6

I once wrote a function that is able to do it without touching registers or cursor position:

function s:F.map.getvrange(start, end)
    let [sline, scol]=a:start
    let [eline, ecol]=a:end
    let text=[]
    let ellcol=col([eline, '$'])
    let slinestr=getline(sline)
    if sline==eline
        if ecol>=ellcol
            call extend(text, [slinestr[(scol-1):], ""])
        else
            call add(text, slinestr[(scol-1):(ecol-1)])
        endif
    else
        call add(text, slinestr[(scol-1):])
        let elinestr=getline(eline)
        if (eline-sline)>1
            call extend(text, getline(sline+1, eline-1))
        endif
        if ecol<ellcol
            call add(text, elinestr[:(ecol-1)])
        else
            call extend(text, [elinestr, ""])
        endif
    endif
    return text
endfunction

It is called like this:

let [sline, scol, soff]=getpos("'<")[1:]
let [eline, ecol, eoff]=getpos("'>")[1:]
if sline>eline || (sline==eline && scol>ecol)
    let [sline, scol, eline, ecol]=[eline, ecol, sline, scol]
endif
let lchar=len(matchstr(getline(eline), '\%'.ecol.'c.'))
if lchar>1
    let ecol+=lchar-1
endif
let text=s:F.map.getvrange([sline, scol], [eline, ecol])

Note that at this point you will have a list of strings in text: one reason why I wrote this function is ability to keep NULLs in file. If you stick with any solution that yanks text in a register all NULLs will be replaced with newlines and all newlines will be represented as newlines as well. In the output of getvrange function though NULLs are represented as newlines while newlines are represented by different items: there is an NL between each list item, just like in output of getline(start, end).

This function can only be used to get lines for characterwise selection (as for linewise it is much simpler and for blockwise I iterate over lines and do not need such function. There are also functions for deleting given range (without touching registers) and inserting text at given position (without touching registers or cursor).

5

Added block selection to @xolox's great answer: mode() isnt used cause it this is planned to be used in function whereby the selection has been cleared by calling the operator etc.

xnoremap <leader>a :<C-U> call GetVisualSelection(visualmode())<Cr>

function! GetVisualSelection(mode)
    " call with visualmode() as the argument
    let [line_start, column_start] = getpos("'<")[1:2]
    let [line_end, column_end]     = getpos("'>")[1:2]
    let lines = getline(line_start, line_end)
    if a:mode ==# 'v'
        " Must trim the end before the start, the beginning will shift left.
        let lines[-1] = lines[-1][: column_end - (&selection == 'inclusive' ? 1 : 2)]
        let lines[0] = lines[0][column_start - 1:]
    elseif  a:mode ==# 'V'
        " Line mode no need to trim start or end
    elseif  a:mode == "\<c-v>"
        " Block mode, trim every line
        let new_lines = []
        let i = 0
        for line in lines
            let lines[i] = line[column_start - 1: column_end - (&selection == 'inclusive' ? 1 : 2)]
            let i = i + 1
        endfor
    else
        return ''
    endif
    for line in lines
        echom line
    endfor
    return join(lines, "\n")
endfunction
3
  • 3
    You should use xnoremap instead of vnoremap for a Visual mode mapping, see Why is vmap for both Visual and Select mode? for details.
    – filbranden
    Commented Apr 28, 2020 at 17:47
  • 2
    I prefer the conssitency of not using select mode, but for others sake, I changed it. Commented Apr 30, 2020 at 11:47
  • 1
    Thanks for this value info, visualmode() is savor for me.
    – jdhao
    Commented Sep 23, 2020 at 14:03
2

This function taken from vim-asterisk also works in <expr> mappings, supports block-wise selections and multibyte columns.

function! GetVisualSelection()
    let mode = mode()
    let end_col = s:curswant() is s:INT.MAX ? s:INT.MAX : s:get_col_in_visual('.')
    let current_pos = [line('.'), end_col]
    let other_end_pos = [line('v'), s:get_col_in_visual('v')]
    let [begin, end] = s:sort_pos([current_pos, other_end_pos])
    if s:is_exclusive() && begin[1] !=# end[1]
        " Decrement column number for :set selection=exclusive
        let end[1] -= 1
    endif
    if mode !=# 'V' && begin ==# end
        let lines = [s:get_pos_char(begin)]
    elseif mode ==# "\<C-v>"
        let [min_c, max_c] = s:sort_num([begin[1], end[1]])
        let lines = map(range(begin[0], end[0]), '
        \   getline(v:val)[min_c - 1 : max_c - 1]
        \ ')
    elseif mode ==# 'V'
        let lines = getline(begin[0], end[0])
    else
        if begin[0] ==# end[0]
            let lines = [getline(begin[0])[begin[1]-1 : end[1]-1]]
        else
            let lines = [getline(begin[0])[begin[1]-1 :]]
            \         + (end[0] - begin[0] < 2 ? [] : getline(begin[0]+1, end[0]-1))
            \         + [getline(end[0])[: end[1]-1]]
        endif
    endif
    return join(lines, "\n") . (mode ==# 'V' ? "\n" : '')
endfunction

let s:INT = { 'MAX': 2147483647 }

" @return Number: return multibyte aware column number in Visual mode to select
function! s:get_col_in_visual(pos) abort
    let [pos, other] = [a:pos, a:pos is# '.' ? 'v' : '.']
    let c = col(pos)
    let d = s:compare_pos(s:getcoord(pos), s:getcoord(other)) > 0
    \   ? len(s:get_pos_char([line(pos), c - (s:is_exclusive() ? 1 : 0)])) - 1
    \   : 0
    return c + d
endfunction

function! s:get_multi_col(pos) abort
    let c = col(a:pos)
    return c + len(s:get_pos_char([line(a:pos), c])) - 1
endfunction

" Helper:

function! s:is_visual(mode) abort
    return a:mode =~# "[vV\<C-v>]"
endfunction

" @return Boolean
function! s:is_exclusive() abort
    return &selection is# 'exclusive'
endfunction

function! s:curswant() abort
    return winsaveview().curswant
endfunction

" @return coordinate: [Number, Number]
function! s:getcoord(expr) abort
    return getpos(a:expr)[1:2]
endfunction

"" Return character at given position with multibyte handling
" @arg [Number, Number] as coordinate or expression for position :h line()
" @return String
function! s:get_pos_char(...) abort
    let pos = get(a:, 1, '.')
    let [line, col] = type(pos) is# type('') ? s:getcoord(pos) : pos
    return matchstr(getline(line), '.', col - 1)
endfunction

" @return int index of cursor in cword
function! s:get_pos_in_cword(cword, ...) abort
    return (s:is_visual(get(a:, 1, mode(1))) || s:get_pos_char() !~# '\k') ? 0
    \   : s:count_char(searchpos(a:cword, 'bcn')[1], s:get_multi_col('.'))
endfunction

" multibyte aware
function! s:count_char(from, to) abort
    let chars = getline('.')[a:from-1:a:to-1]
    return len(split(chars, '\zs')) - 1
endfunction

" 7.4.341
" http://ftp.vim.org/vim/patches/7.4/7.4.341
if v:version > 704 || v:version == 704 && has('patch341')
    function! s:sort_num(xs) abort
        return sort(a:xs, 'n')
    endfunction
else
    function! s:_sort_num_func(x, y) abort
        return a:x - a:y
    endfunction
    function! s:sort_num(xs) abort
        return sort(a:xs, 's:_sort_num_func')
    endfunction
endif

function! s:sort_pos(pos_list) abort
    " pos_list: [ [x1, y1], [x2, y2] ]
    return sort(a:pos_list, 's:compare_pos')
endfunction

function! s:compare_pos(x, y) abort
    return max([-1, min([1,(a:x[0] == a:y[0]) ? a:x[1] - a:y[1] : a:x[0] - a:y[0]])])
endfunction
1

I think you should use "clipboard resgisiter".

For more detail, you might read help ':h clipboard-autoselect'

If you enable this option( set clipboard=unnamed,autoselected),
you ca get selected text more easily like this " let l:text = @* "

0

Modified version of @DarkWiiPlayer answer.

The differences are:

1. It test &selection to give the proper functionality when

:behave mswin

As well as the default:

:behave xterm

2. It also works properly for visual block selections testing visualmode()

I'm also returned the visual selection as an array of lines (becuase that is what I needed). However I've left the return join(lines, "\n") in commented out if you need a signal text block instead.

function! VisualSelection()
    if mode()=="v"
        let [line_start, column_start] = getpos("v")[1:2]
        let [line_end, column_end] = getpos(".")[1:2]
    else
        let [line_start, column_start] = getpos("'<")[1:2]
        let [line_end, column_end] = getpos("'>")[1:2]
    end

    if (line2byte(line_start)+column_start) > (line2byte(line_end)+column_end)
        let [line_start, column_start, line_end, column_end] =
        \   [line_end, column_end, line_start, column_start]
    end
    let lines = getline(line_start, line_end)
    if len(lines) == 0
            return ['']
    endif
    if &selection ==# "exclusive"
        let column_end -= 1 "Needed to remove the last character to make it match the visual selction
    endif
    if visualmode() ==# "\<C-V>"
        for idx in range(len(lines))
            let lines[idx] = lines[idx][: column_end - 1]
            let lines[idx] = lines[idx][column_start - 1:]
        endfor
    else
        let lines[-1] = lines[-1][: column_end - 1]
        let lines[ 0] = lines[ 0][column_start - 1:]
    endif
    return lines  "use this return if you want an array of text lines
    "return join(lines, "\n") "use this return instead if you need a text block
endfunction
2
  • 1
    Do '> and '< now update while the user is still in visual mode? Commented Nov 2, 2020 at 14:44
  • No '> and '< are not updated in visual mode. Sorry I should have checked it better for my purposes it is not called in visual mode. I'll add your mode check back in
    – pev.hall
    Commented Nov 3, 2020 at 3:13

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.