makecols.vim: converting lists to columns made simple
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.4 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. " makecols.vim - Makecolumns
  2. " Author: Levi Olson <http://leviolson.com/>
  3. " Version: 1.0
  4. function! s:beep()
  5. exe "norm! \<Esc>"
  6. return ""
  7. endfunction
  8. function! s:get_visual_selection()
  9. let [lnum1, col1] = getpos("'<")[1:2]
  10. let [lnum2, col2] = getpos("'>")[1:2]
  11. let no_of_lines = lnum2 - lnum1
  12. let no_of_cols = 5
  13. let lines = getline(lnum1, lnum2)
  14. let lines[-1] = lines[-1][: col2 - (&selection == 'inclusive' ? 1 : 2)]
  15. let lines[0] = lines[0][col1 - 1:]
  16. execute lnum1 . "," . lnum2 . "delete"
  17. let new_string = ""
  18. for i in lines
  19. " start combining
  20. if (i % no_of_cols)
  21. let new_string = join([new_string, lines[i]], "\t")
  22. else
  23. let new_string = join([new_string, lines[i]], "\n")
  24. endif
  25. endfor
  26. let @a = new_string
  27. return lines
  28. endfunction
  29. function! s:replace_selected_text()
  30. execute "normal! \"ap"
  31. echo "Just tried to replace the selection with the lines."
  32. return ""
  33. endfunction
  34. function! s:makecols() range
  35. let mode = visualmode()
  36. if (mode !=# "V")
  37. echo "You must be in linewise visual mode"
  38. return s:beep()
  39. else
  40. echo "You are in the right mode"
  41. endif
  42. echo s:get_visual_selection()
  43. return s:replace_selected_text()
  44. endfunction
  45. vnoremap <silent> mc :<C-U>call <SID>makecols()<CR>
  46. " vim:set ft=vim sw=4 sts=4 et: