Vim function to copy a code function to clipboard -
i want have keyboard shortcut in vim copy whole function powershell file windows clipboard. here command it:
1) va{vok"*y
- visual mode, select {} block, visual line mode, go selection top, include header line, yank windows clipboard.
but work functions without inner {} block. here valid workaround it:
2) va{a{a{a{a{a{a{vok"*y
- same (1), selecting {} block done multiple times - work code blocks have 7 inner {} braces.
but thing - (1) command works fine when called vim function, (2) misbehaves , selects wrong code block when called vim function:
function! copycodeblocktoclipboard () let cursor_pos = getpos('.') execute "normal" 'va{a{a{a{a{a{a{vok"*y' call setpos('.', cursor_pos) endfunction " copy code block clipboard map <c-q> :call copycodeblocktoclipboard()<cr>
what doing wrong here in copycodeblocktoclipboard? (2) command works expected when executed directly in vim.
update:
i've noticed that:
- if there more a{ included blocks in function
- then vim wouldn't execute v
looks vim handles errors differently here. a{ produces error , regular command execution ignores it. execution withing function via :normal fails , wouldn't call v (or command follows error).
any workaround this?
try function
function! copycodeblocktoclipboard() let cursor_pos = getpos('.') let = 1 let done = 0 while !done call setpos('.', cursor_pos) execute "normal" 'v' . . 'abvok"*y' if mode() =~ "^[vv]" let done = 1 else let = + 1 endif endwhile execute "normal \<esc>" call setpos('.', cursor_pos) endfunction
this preforms execute command select blocks until fails select block larger block. ([count]ab
selects [count]
blocks) seems when selection fails end in visual mode. can use mode()
check this.
when function exits should in normal mode , cursor should restored started. , function in *
register.
Comments
Post a Comment