Execute a command under the cursor in Vim

How can I take the current word under the cursor in VIM?

echo expand("<cword>")

or

let l:WordValue = expand("<cword>")

How can I execute the command under the cursor? I am not exactly sure what kind of commands you're refering to, but since you mentioned man pages, I'd assume that you want to execute unix and or cmd commands. If this is the case you'll want something like

fu ExecuteCommand()
   let l:Command = expand("<cword>")
   execute "!" . l:Command
endfu 

Alternatively, you might want to change the value of keywordprg:

:set keywordprg=!

If you have a command typed in a buffer and the cursor is on the line, to execute it do this: first yank what you want to execute, Y or yy will copy the whole line, then in the command line you can type @" and hit Enter

or do this combination <C-R> " to paste literally

or put this mapping in .vimrc

" execute line under the cursor
nnoremap <leader>el yy:@"<CR>

@" stands for the default " register, any text that you delete (with d, c, s or x) or yank will be placed there


Another tip: If you want to put the word currently under the cursor into the command-line mode, use the sequence CTRL + R then CTRL + W

Tags:

Vim