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.

72 lines
1.5 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 == 0)
  21. let new_string = lines[0]
  22. else
  23. if (i % no_of_cols)
  24. let new_string = join([new_string, lines[i]], "\t")
  25. else
  26. let new_string = join([new_string, lines[i]], "\n")
  27. endif
  28. endif
  29. endfor
  30. let @a = new_string
  31. return lines
  32. endfunction
  33. function! s:replace_selected_text()
  34. execute "normal! \"ap"
  35. echo "Just tried to replace the selection with the lines."
  36. return ""
  37. endfunction
  38. function! s:makecols() range
  39. let mode = visualmode()
  40. if (mode !=# "V")
  41. echo "You must be in linewise visual mode"
  42. return s:beep()
  43. else
  44. echo "You are in the right mode"
  45. endif
  46. echo s:get_visual_selection()
  47. return s:replace_selected_text()
  48. endfunction
  49. vnoremap <silent> mc :<C-U>call <SID>makecols()<CR>
  50. " vim:set ft=vim sw=4 sts=4 et: