1

Given an integer n and a block of text, rotate every character or element in the n-th column up or down to the next position that exists.

A line with more than k characters.
A longer line with more than k character.
A short line.
Rotate here: ------v--
This is long enough.

This is not enough.
Wrapping around to the first line.

Output:

A line with more thtn k characters.
A longer line with aore than k character.
A short line.
Rotate here: ------m--
This is long enoughv

This is not enough.
Wrapping around to .he first line.

Rotating the k-th column of the same input where 35 < k < 42 would yield the input text unchanged.

Related: https://codegolf.stackexchange.com/questions/182363/rotate-a-column

1
" s : start line, e : end line, c : col
function! RotateColumn(s, e, c)
  " place cursor at line a:s
  call cursor(a:s, 1)
  " search backward to find last line that's longer enough
  if search(printf('\v%%>%dl%%<%dl%%%dc', a:s-1, a:e+1, a:c), 'b')
    " copy current character into @"
    norm! yl
    " for each line, if character exists in screen column a:c, swap it with @"
    exec printf('%d,%dg/\v%%%dc/ norm! %d|vp', a:s, a:e, a:c, a:c)
  endif
endfunction

update

No longer need to find last line.

" s : start line, e : end line, c : col
function! RotateColumn2(s, e, c)
  " set @" to any letter
  let @"='x'
  " copy all lines to below a:e
  exec a:s.','.a:e.'t'.a:e
  " for each line, if character exists in screen column a:c, swap it with @"
  exec printf('%d,%dg/\v%%%dc/ norm! %d|vp', a:s, a:e*2-a:s+1, a:c, a:c)
  " remove original lines
  exec a:s.','.a:e.'d'
endfunction

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.