I am trying to reverse a string, so that 'abc defg hij'
becomes 'jih gfed cba'
. The solution I came up with is
join(reverse(split(l:text, '.\zs')), '')
Is there a more elegant solution or should I stick with what I have come up with?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityYour solution is very elegant! An alternative if you are in Unix based env:
!!rev
From wikia
vnoremap <Leader>r c<C-O>:set revins<CR><C-R>"<Esc>:set norevins<CR>
For more see: :h revins
split()
without the{keepempty}
option should remove a single empty item at the beginning, and a single empty item at the end. It currently removes all empty items, including the ones in the middle of the list (which is more useful than the documented behaviour if you ask me, but you probably aren't). I think this has been discussed recently onvim_dev
.