3

I've written a short vim function that allows me to edit the contents of vim registers:

fun! RegBuff()
    let vimstuff = "/tmp/vimstuff"
    if !isdirectory(vimstuff)
        call mkdir(vimstuff)
    endif
    let reg = getchar()
    let reg = nr2char(reg)
    exec 'au BufWrite vimreg-* exec "normal gg0\"'.reg.'yy"'
    exec '1new '.vimstuff.'/vimreg-'.reg
    exec 'normal ggdG"'.reg.'P'
endfun
nnoremap <leader><F2> :call RegBuff()<CR>

If I do, for instance, qqi1234^[ to record insert 1234 into the q register, and then call my function :call RegBuff() or <leader><F2>, I get a nice little window at the top with the contents of the buffer. Cool. if I edit it to be i12345678^[, save the file and exit, and then do @q, it properly now enters insert mode and inserts 12345678. Also cool.

The problem is that I generally run vim in tmux, and for some reason after executing this macro it switches to the tmux pane below vim. This is as if the macro is additionally running <Ctrl>j after the macro is run.

If I go back to the vim pane, and type "qp to see whats in the q register it looks totally normal, it just contains what I expect to be in there.

Why does there appear to be this extra key code sent after I call the macro?

3
  • Are you writing the buffer? It might be, that when writing the buffer, Vim writes an additional EOL. Try setting 'nofixendofline' Jul 7 '17 at 15:02
  • Thanks for the help Christian. I am writing the buffer, which triggers the BufWrite autocommand that copies the first line of the buffer back into the register. Neither putting set nofixendofline in the function or setting it manually doesn't change the behavior. Jul 7 '17 at 15:07
  • Note that you can call :echomsg @q to see what is in register q, including special characters.
    – piojo
    Feb 15 '19 at 3:09
2

Came across this question is looking into a different ^J phenomenon.

Register Situation

In your RegBuff function, you yank text of the buffer into the register with yy. This is linewise yank, thus it appends the newline ^J to the register.

Printing the register ("qp) will not show the newline, but it will insert a line below cursor on which it prints the text. You can see the ^J explicitly by using the :reg {arg} command (:reg q shows the contents of q).

Solution

Change the linewise yank (yy -- yank line) to be a motion yank (y$ -- yank to end of line):

"Original linewise
exec 'au BufWrite vimreg-* exec "normal gg0\"'.reg.'yy"'

becomes

"Updated to motion
exec 'au BufWrite vimreg-* exec "normal gg0\"'.reg.'y$"'
1
  • 1
    I had no idea that ^J was also interpreted as a newline character. Thanks for the response! (also you've got a typo in your explaination, the second yy should be y$, I can't edit it because the edit is less than 6 characters. Oct 21 '20 at 7:30

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.