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.

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