55

I'm writing a Vim script. How can I get the word under the cursor and the text of the current line?

1 Answer 1

79

You can with expand and getline:

let wordUnderCursor = expand("<cword>")
let currentLine   = getline(".")
3
  • 4
    Use help cword to get more options. For my vimrc, I needed cWORD to grab the whitespace delimited text under the cursor
    – Ben
    Commented May 31, 2018 at 0:11
  • for me getline(".") gives the same result as expand("<cword>"). i want to grab the entire line under cursor. how do i do that?
    – alexzander
    Commented Jun 10, 2021 at 7:21
  • 1
    Further to @Ben's comment, you can observe the difference between where the cursor jumps to for cword and cWORD by pressing w and W on one's keyboard while in normal mode. Commented Jul 14, 2023 at 5:05

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.