I'm writing a Vim script. How can I get the word under the cursor and the text of the current line?
1 Answer
You can with expand and getline:
let wordUnderCursor = expand("<cword>")
let currentLine = getline(".")
-
4Use
help cword
to get more options. For my vimrc, I neededcWORD
to grab the whitespace delimited text under the cursor– BenCommented May 31, 2018 at 0:11 -
for me
getline(".")
gives the same result asexpand("<cword>")
. i want to grab the entire line under cursor. how do i do that? Commented Jun 10, 2021 at 7:21 -
1Further to @Ben's comment, you can observe the difference between where the cursor jumps to for
cword
andcWORD
by pressingw
andW
on one's keyboard while in normal mode. Commented Jul 14, 2023 at 5:05