0

Suppose I have this (where my insertion point is designated "|"):

col1           col2

thing          oeueaoue|
another
something
test

What keystroke(s) would I use so the insertion point jumps to here:

col1           col2

thing          oeueaoue
another        |
something
test

?

Is return+A+tab… (normal mode) the only way?

  • 1
    How do you feel about using 'virtualedit'? You could use set virtualedit=all. Then you can just move to where you want to start editing. – Peter Rincker Mar 22 '18 at 19:08
  • @PeterRincker That wouldn't be a very automated way of doing it, would it? – Geremia Mar 22 '18 at 21:22
1

You can use :Tabularize to do this.

:Tabularize /\s\+\zs/l1

There are plenty of other alignment plugins. e.g. vim-easy-align and lion.vim. I would recommend installing an alignment tool if this becomes common.

If you want it automated then you can do something similar to Tim Pope's cucumbertables.vim. Example:

inoremap <silent> <space>   <space>.<Esc>:call <SID>align()<CR>"_s

function! s:align()
  let p = '^\%(\w\+\s\+\)\+\w\+$'
  if exists(':Tabularize') && getline('.') =~# '^\w\+\s' && (getline(line('.')-1) =~# p || getline(line('.')+1) =~# p)
    let column = strlen(substitute(substitute(getline('.')[0:col('.')],'\S\+','.','g'), '\s*', '', 'g'))
    Tabularize/\s\+\zs/l1
    normal! 0
    call search(repeat('\w\+\s\+',column-1) . '.', 'ce', line('.'))
  endif
endfunction

Note: I have not fully tested this code. Use as is.

I would recommend this to be a buffer local mapping. See :h map-local.

Want a more native/simpler approach? Use <c-y> to copy a character right above the current location. You can simply spam <c-y> until you have your whitespace alignment. See :h i_CTRL-y for more information.

0

from here:

When there are many lines to edit, it can be useful to map the return key temporarily, something like that in your case:

inoremap <buffer> <cr> <esc>jA<tab>

When you're done, just cancel the map:

iunmap <buffer> <cr>

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.