0

In vimL, how to get a string from cursor to end of <cWORD>.

I know expand('<cWORD>', but don't known how to keep only right part of cursor.

Example

given text pre abc-xyz.xxx:123 post,

with cursor at -,

return string -xyz.xxx:123.

4

One option is to take the current line, then the substring starting at the current position, split it into multiple words and take the first one. Or, in Vimscript:

let word = split(getline('.')[col('.')-1:])[0]

One alternative is to use a normal mode command such as yW and then access the contents of the default register (or, better, use a named register.) You can save and restore the contents of the register, so that accessing their contents after the call to the function will still return the original value.

function! GetWord()
    let save_reg = getreg('x')
    let save_type = getregtype('x')
    normal! "xyW
    let word = @x
    call setreg('x', save_reg, save_type)
    return word
endfunction
  • 1
    Method 1 is what I need. Many thanks. – qeatzy Jan 3 at 11:21
4

Method 1.1

" from start of cWORD to cursor
echo matchstr(getline('.'), '\S*\%'. col('.') .'c.')
" from cursor to end of cWORD
echo matchstr(getline('.'), '\%'. col('.') .'c.\S*')

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.