Is there a way to select a text block (e.g. a first word "TODO:") programmatically (with some commands or functions from a script)? Not simply highlight, like with a search, but visually select like with mouse?
If you are looking to visually select a search pattern you can do it using two commands. For example searching for "TODO...." lines:
This will search your your pattern and then visually select to the end of it.
If you want to move this to a function and then be able to call it in a single action:
function! VisualSelectSearch (query) execute "normal /".a:query."/s\<CR>v//e\<CR>" endfunction command! -nargs=+ VSS call VisualSelectSearch(<q-args>)
Now you can type
:VSS ^TODO.*$ and visually select the next instance. If you want to call the function directly in a script use
Any sequence of key presses can be represented in vimscript with the
:normal command. For instance, to visually select the next occurrence of
"TODO:" you might normally hit the following keys:
/TODO:<CR> jump to the next occurence of TODO: v start a visual selection (on the T) f: jump to the next ':'
To simulate these key presses in vimscript, you'd want to write something like:
<CR> will literally type
< C R > instead of enter. To get around this, we have to wrap this in an execute command:
execute "normal! /TODO:\<CR>vf:"
I recommend reading
:help normal and Learn Vimscript the Hard Way for more information.