2

I know in visual block mode, by <S-i> (I) one can insert in multiple selected lines, however I want to achieve the same effect by a function, let's say I have a functions which can tell the three sub-visual modes (visual-character, visual-line, visual-block) as follows,

function! VisualMappingSpace()
    let m = visualmode()
    if m ==# 'v'
        echo 'character-wise visual'
    elseif m == 'V'
        echo 'line-wise visual'
    elseif m == "\<C-V>"
        echo 'block-wise visual'
    endif
endfunction

I've tried as follows but it doesn't work. I want to insert soemthing to the lines I select when I hit <space> in visual-block mode.

function! VisualMappingSpace()
    let m = visualmode()
    if m ==# 'v'
        exec "normal y"
    elseif m == 'V'
        exec "normal y"
    elseif m == "\<C-V>"
        let g:block_insert_content = input("")
        exec "normal I ".g:block_insert_content
    endif
endfunction   
vnoremap <silent> <Space> :call VisualMappingSpace()<CR>
2
  • exec "normal y" should be normal y. Anyway, what do you expect? What do you get instead? Commented Oct 24, 2016 at 7:54
  • Actually what I expected is in visual-character and visual-line mode when I hit <space> I can copy the selected lines to the default register, and in visual-block mode when I hit <space> I can insert something to the lines I selected, just like what I hit I in visual-block mode do. Commented Oct 24, 2016 at 8:02

1 Answer 1

2

A visual-mode mapping that enters command-line mode via : will have the visual range ('<,'>) automatically inserted. With :call, that means that your function is invoked once per selected line. You should have noticed via the repeated queries.

To avoid this, insert <C-u> into your mapping; it clears the range.

Second problem: When you insert the queried text, you need to re-create the selection (your mapping left visual mode for command-line mode, remember?) via gv; then, I will work:

function! VisualMappingSpace()
    let m = visualmode()
    if m ==# 'v'
        exec "normal y"
    elseif m == 'V'
        exec "normal y"
    elseif m == "\<C-V>"
        let g:block_insert_content = input("")
        exec "normal gvI ".g:block_insert_content
    endif
endfunction   
vnoremap <silent> <Space> :<C-u>call VisualMappingSpace()<CR>

Also note that there is an additional space character before your queried text; I'm not sure you want that: gvI ".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.