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.

80 lines
1.7 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
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 lines = getline(lnum1, lnum2)
  13. execute lnum1 . "," . lnum2 . "delete"
  14. " let lines[-1] = lines[-1][: col2 - (&selection == 'inclusive' ? 1 : 2)]
  15. " let lines[0] = lines[0][col1 - 1:]
  16. let c = 0
  17. let no_of_cols = 6
  18. let new_string = ""
  19. let old_string = split(&selection, "\n")
  20. echom "Old String: "
  21. echom &selection
  22. echom join(old_string, ", ")
  23. let @z = ""
  24. for i in lines
  25. " start combining
  26. if (c == 0)
  27. let new_string = join([new_string, lines[i]], "")
  28. else
  29. if (c % no_of_cols)
  30. let new_string = join([new_string, lines[i]], "\t")
  31. else
  32. let new_string = join([new_string, lines[i]], "\n")
  33. endif
  34. endif
  35. let c += 1
  36. endfor
  37. let @z = join([new_string, ""], "\n")
  38. return lines
  39. endfunction
  40. function! s:replace_selected_text()
  41. execute "normal! \"zP"
  42. echo "Just tried to replace the selection."
  43. return ""
  44. endfunction
  45. function! s:makecols() range
  46. let mode = visualmode()
  47. if (mode !=# "V")
  48. echo "You must be in linewise visual mode"
  49. return s:beep()
  50. else
  51. echo "You are in the right mode"
  52. endif
  53. echo s:get_visual_selection()
  54. return s:replace_selected_text()
  55. endfunction
  56. vnoremap <silent> mc :<C-U>call <SID>makecols()<CR>
  57. " vim:set ft=vim sw=4 sts=4 et: