-1

Given the following block of text:

title,tags
Run-ons - Comma Splices - Fused Sentences
Rule 1 - Run-ons - Comma Splices - Fused Sentences 
Rule 2 - Run-ons - Comma Splices - Fused Sentences
Example 1 - Rule 1 - Run-ons - Comma Splices - Fused Sentences
Example 1 - Rule 2 - Run-ons - Comma Splices - Fused Sentences
Example 2 - Rule 2 - Run-ons - Comma Splices - Fused Sentences

Why does the following code result in the same value twice instead of the contents of line 2 followed by the contents of line 3?

function! TempTest()

    messages clear

    call cursor(2, 1)

    call feedkeys('0v/\(,\|$\)' . "\<CR>" . 'h"ay')
    let l:v = '' . @a
    echom 'l:v ' . l:v

    call cursor(3, 1)

    call feedkeys('0v/\(,\|$\)' . "\<CR>" . 'h"ay')
    let l:v = '' . @a
    echom 'l:v ' . l:v

    messages

endfunction

Results:

l:v Rule 1 - Run-ons - Comma Splices - Fused Sentences
l:v Rule 1 - Run-ons - Comma Splices - Fused Sentences

Expected Results:

 l:v Run-ons - Comma Splices - Fused Sentences
 l:v Rule 1 - Run-ons - Comma Splices - Fused Sentences
  • I was initially doing this in a loop, but then I noticed it wasn't working, which is why I included the example above... – leeand00 May 25 '17 at 16:17
1

From :help feedkeys

Characters in {string} are queued for processing as if they come from a mapping or were typed by the user. They are added to the end of the typeahead buffer, thus if a mapping is still being executed these characters come after them. The function does not wait for processing of keys contained in {string}.

This means feedkeys only queues up the keystrokes you passed to it. The keystrokes actually play back only after your function exits.


I suggest that instead of using a combination of simulated keystrokes and vimscript, just use one or the other.

This sets register a to line 1, up to the first comma or end-or-line

let @a = matchstr(getline(1),'[^,]*')

or

call feedkeys( '1Gv/\(,\|$\)' . "\<CR>" . 'h"ay'
  • I read this (after I posted the question) and I tried using the t mode, but it didn't fix the issue either. Should I be using :visual instead? – leeand00 May 25 '17 at 17:42
  • hi, I updated my answer with a suggestion – Jim U May 26 '17 at 15:08
0

call feedkeys('0v/\(,\|$\)' . "\<CR>" . 'h"ay')

  • There's no starting :.
  • 0 runs v in line 1 only.
  • The pattern always matches, so the command given to v will never run, no matter which line you run it in.
  • v isn't given a command so it defaults to printing the line (or it would if it ever ran) which seems somewhat pointless.
  • h occurs with the cursor in column 1 and causes the rest of the command sequence to fail.
  • "ay isn't a complete command, so nothing would be yanked even if you got to this point.

So, you probably see the same line in the messages twice because it already happened to be in register a anyway.

feedkeys() is rarely needed and you are usually better served with :normal. See @JimU's answer for some things to be aware of. Also you can yank in this scenario with y a; there's no need to even use Normal mode..

I don't really want to make further suggestions for fixing this since it isn't clear to me what you're trying to do.

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.