Advertisement
FocusedWolf

Vim: Get text input from user

Aug 11th, 2017
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 3.14 KB | None | 0 0
  1. " Bind Ctrl + G to go-to-line {{{
  2.  
  3. " Pressing :3 to go to line 3 is so hard to remember that it was easier to write this script to map Ctrl + G, like every other text editor uses, to a menu that asks the user what line to go to.
  4. " Can't believe i had to actually google how vim does it... User friendly!
  5.  
  6. function! GoToLine()
  7.     try
  8.         let lineNumber = ''
  9.         while 1
  10.             let lineNumber = s:GetTextInputFromUser("Go to Line: ", lineNumber, '0')
  11.             if lineNumber =~ '^\d\+$' " If is a number
  12.                 call cursor(lineNumber, 0)
  13.                 break
  14.             endif
  15.         endwhile
  16.     catch /user\ cancelled\ providing\ input/
  17.     endtry
  18. endfunction
  19.  
  20. nnoremap <silent> <C-g> :call GoToLine()<CR>
  21.  
  22. " Provides a way to ask the user for text input.
  23. " The following editing keys are provided and work as expected in normal editors: Backspace, Delete, Left, Right, Home, End.
  24. " The user can cancel with Escape and submit text input with Enter.
  25. " You can also select text and do command-line copy|paste commands.
  26. "
  27. " Usage: let userInput = s:GetTextInputFromUser([inputRequest], [suggestedUserInput], [displayButtons])
  28. " Returns: Whatever the text state was when the user hit Enter. Can be ''.
  29. "
  30. " inputRequest - The question you want to ask the user, it can be '', and appears on the commandline left of the users entered text.
  31. " suggestedUserInput - Allows you to provide an initial version of the text you intend the user to modify and send back.
  32. " displayButtons - [0, 1] Display the "[Escape] [Enter]" buttons.
  33. "
  34. " Note: If the user cancels providing input, you need to handle the thrown exception like so: try | <code> | catch /user\ cancelled\ providing\ input/ | endtry
  35. "
  36. " Example: let userInput = s:GetTextInputFromUser("How many times have you been turned down for a job? ")
  37. "          let userInput = s:GetTextInputFromUser("Gender: ", "Attack Helicopter", 1)
  38. "
  39. "          If you don't like what the user is sending you, then just send it right back with a better request message.
  40. "          let pushTheButton = s:GetTextInputFromUser("Bomb North Korea? Say yes asshole! ", pushTheButton)
  41.  
  42. function! s:GetTextInputFromUser(...)
  43.     if a:0 > 3
  44.         throw "Too many arguments"
  45.     endif
  46.  
  47.     let inputRequest = ''
  48.     if a:0 >= 1
  49.        let inputRequest = a:1
  50.     endif
  51.  
  52.     let text = ''
  53.     if a:0 >= 2
  54.        let text = a:2
  55.     endif
  56.  
  57.     let displayButtons = 1
  58.     if a:0 >= 3
  59.         if a:3 !~ '^\d\+$' || a:3 > 1 || a:3 < 0
  60.             throw "ArgumentException: displayButtons value '" . a:3 . "' is not in the bounds [0,1]."
  61.         endif
  62.         let displayButtons = a:3
  63.     endif
  64.  
  65.     if displayButtons
  66.         echohl SpecialKey | echon "[Escape] "
  67.         echohl SpecialKey | echon "[Enter] "
  68.     endif
  69.  
  70.     call inputsave()
  71.  
  72.     echohl Question
  73.     let text = input(inputRequest, text)
  74.     echohl Normal
  75.  
  76.     call inputrestore()
  77.  
  78.     " Clear input prompt after input is entered
  79.     redraw!
  80.    
  81.     " If the user enters '' or hits escape
  82.     if text == ''
  83.         throw "user cancelled providing input"
  84.     endif
  85.  
  86.     return text
  87. endfunction
  88.  
  89. " }}} Bind Ctrl + G to go-to-line
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement