0

In visual-block mode, it's very convenient to press I to be able to modify text at the beginning of the selection(s). What is the equivalent way to do this in normal Visual mode? For example, the closest I've found is probably something like:

[Esc]`<i

And I'm not sure there exists a way to do it i Visual-line mode? What would be the simplest way to do this: is there a way with a single keystroke like there is in visual-block mode? And the same with A to insert (append) at the end of the visual selection(s)?

6
  • See :h v_o and :h v_O
    – Matt
    Jun 19, 2020 at 5:46
  • @Matt -- sure, but if I'm calling it in a function I don't necessarily know which 'side' I'm on, if I'm to toggle the start/end.
    – David542
    Jun 19, 2020 at 5:50
  • If used in script then it's not really about "hit"-convenience.
    – Matt
    Jun 19, 2020 at 5:56
  • @Matt -- I see, what's the closest thing (multicursor?) for visual line mode?
    – David542
    Jun 19, 2020 at 5:59
  • @Matt I see, yea I was going to try to do a keyboard mapping for it if it was possible.
    – David542
    Jun 19, 2020 at 6:00

1 Answer 1

2

Not sure to have correctly understood, but try this:

let s:niceblock_keys = {
    \   '$'  : {'v': 'g$h',      'V': '$',          "\<c-v>": '$h'},
    \   'I'  : {'v': "\<c-v>I",  'V': "\<c-v>^o^I", "\<c-v>": 'I'},
    \   'A'  : {'v': "\<c-v>A",  'V': "\<c-v>0o$A", "\<c-v>": 'A'},
    \   'gI' : {'v': "\<c-v>0I", 'V': "\<c-v>0o$I", "\<c-v>": '0I'},
    \   '>'  : {'v': "\<c-v>>",  'V': "0\<c-v>>",   "\<c-v>": '>'},
    \   '<'  : {'v': "\<c-v><",  'V': "0\<c-v><",   "\<c-v>": '<'},
    \ }
fu s:niceblock(key) abort
    return s:niceblock_keys[a:key][mode()]
endfu

xno <expr> $  <sid>niceblock('$')
xno <expr> I  <sid>niceblock('I')
xno <expr> gI <sid>niceblock('gI')
xno <expr> A  <sid>niceblock('A')
xno <expr> >  <sid>niceblock('>')
xno <expr> <  <sid>niceblock('<')

Adapted from the vim-niceblock plugin.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.