Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- " Bind Ctrl + G to go-to-line {{{
- " 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.
- " Can't believe i had to actually google how vim does it... User friendly!
- function! GoToLine()
- try
- let lineNumber = ''
- while 1
- let lineNumber = s:GetTextInputFromUser("Go to Line: ", lineNumber, '0')
- if lineNumber =~ '^\d\+$' " If is a number
- call cursor(lineNumber, 0)
- break
- endif
- endwhile
- catch /user\ cancelled\ providing\ input/
- endtry
- endfunction
- nnoremap <silent> <C-g> :call GoToLine()<CR>
- " Provides a way to ask the user for text input.
- " The following editing keys are provided and work as expected in normal editors: Backspace, Delete, Left, Right, Home, End.
- " The user can cancel with Escape and submit text input with Enter.
- " You can also select text and do command-line copy|paste commands.
- "
- " Usage: let userInput = s:GetTextInputFromUser([inputRequest], [suggestedUserInput], [displayButtons])
- " Returns: Whatever the text state was when the user hit Enter. Can be ''.
- "
- " inputRequest - The question you want to ask the user, it can be '', and appears on the commandline left of the users entered text.
- " suggestedUserInput - Allows you to provide an initial version of the text you intend the user to modify and send back.
- " displayButtons - [0, 1] Display the "[Escape] [Enter]" buttons.
- "
- " Note: If the user cancels providing input, you need to handle the thrown exception like so: try | <code> | catch /user\ cancelled\ providing\ input/ | endtry
- "
- " Example: let userInput = s:GetTextInputFromUser("How many times have you been turned down for a job? ")
- " let userInput = s:GetTextInputFromUser("Gender: ", "Attack Helicopter", 1)
- "
- " If you don't like what the user is sending you, then just send it right back with a better request message.
- " let pushTheButton = s:GetTextInputFromUser("Bomb North Korea? Say yes asshole! ", pushTheButton)
- function! s:GetTextInputFromUser(...)
- if a:0 > 3
- throw "Too many arguments"
- endif
- let inputRequest = ''
- if a:0 >= 1
- let inputRequest = a:1
- endif
- let text = ''
- if a:0 >= 2
- let text = a:2
- endif
- let displayButtons = 1
- if a:0 >= 3
- if a:3 !~ '^\d\+$' || a:3 > 1 || a:3 < 0
- throw "ArgumentException: displayButtons value '" . a:3 . "' is not in the bounds [0,1]."
- endif
- let displayButtons = a:3
- endif
- if displayButtons
- echohl SpecialKey | echon "[Escape] "
- echohl SpecialKey | echon "[Enter] "
- endif
- call inputsave()
- echohl Question
- let text = input(inputRequest, text)
- echohl Normal
- call inputrestore()
- " Clear input prompt after input is entered
- redraw!
- " If the user enters '' or hits escape
- if text == ''
- throw "user cancelled providing input"
- endif
- return text
- endfunction
- " }}} Bind Ctrl + G to go-to-line
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement