Vim Tips Wiki
No edit summary
(Change <tt> to <code>, perhaps also minor tweak.)
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
  +
{{TipNew
hi, ever wondering how to prompt a user for some input from python script within vim, but first make sure you have imported the vim module
 
  +
|id=1616
<code>
 
  +
|previous=1615
def python_input(message = 'input'):
 
  +
|next=1617
vim.command("call inputsave()")
 
  +
|created=2009
vim.command("let user_input = input('" + message + " : ')")
 
  +
|complexity=basic
vim.command("call inputrestore()")
 
  +
|author=Slackdna
return vim.eval('user_input')
 
  +
|version=7.0
</code>
 
  +
|subpage=/200902
now everytime you wanna prompt user, in your python script do something like
 
  +
|category1=Python
  +
|category2=Scripting
  +
}}
  +
This tip shows how to obtain input from the user from within a script. Examples showing Vim script and Python are given.
   
  +
==Overview==
<code>
 
  +
Vim provides the function <code>input()</code> that can display a prompt for the user, then wait for the user to type some input. To avoid problems with Vim's typeahead mechanism (which includes the processing of mappings), you need to first call <code>inputsave()</code>, then call <code>input()</code>, then <code>inputrestore()</code>.
input = python_input('input something')
 
</code>
 
   
  +
Often it is better to write a Vim command, so you can simply type the desired command and arguments when wanted (you don't need to be prompted for input). However, it is sometimes useful for a script to prompt the user to enter a required parameter.
it will prompt the user to input something with 'input something' as the caption of the prompt, in the command-line
 
   
  +
If your are asking the user to confirm a choice, use instead the command
for more detail
 
  +
confirm(text[,choices[,default[,type]]])
  +
(Note: confirm() is only supported when compiled with dialog support). For more details see the help reference.
   
  +
==Vim example==
<code>
 
  +
Save the following in a file called, say, <code>vimdemo.vim</code>:
<nowiki>:help input()</nowiki>
 
</code>
+
<pre>
  +
function! Demo()
  +
let curline = getline('.')
  +
call inputsave()
  +
let name = input('Enter name: ')
  +
call inputrestore()
  +
call setline('.', curline . ' ' . name)
  +
endfunction
 
</pre>
   
  +
Use Vim to edit any file, then enter the following to source (execute) the above script:
hope, this would help.
 
  +
<pre>
  +
:so vimdemo.vim
 
</pre>
   
  +
That defines function <code>Demo()</code> which you can call with:
[[User:Slackdna|Slackdna]] 00:18, 10 February 2009 (UTC)
 
  +
<pre>
  +
:call Demo()
  +
</pre>
  +
  +
The function gets the current line to a variable, prompts the user to enter a name, then appends the entered name to the current line. This is only a simple demonstration, and is not intended to do anything useful.
  +
  +
==Python example==
  +
Save the following in a file called, say, <code>pydemo.vim</code>:
  +
<pre>
  +
function! DefPython()
  +
python << PYEND
  +
import vim
 
def python_input(message = 'input'):
 
vim.command('call inputsave()')
 
vim.command("let user_input = input('" + message + ": ')")
 
vim.command('call inputrestore()')
 
return vim.eval('user_input')
  +
  +
def demo():
  +
curline = vim.current.line
 
name = python_input('Enter name')
  +
vim.current.line = curline + ' ' + name
  +
PYEND
  +
endfunction
  +
call DefPython()
  +
</pre>
  +
  +
Use Vim to edit any file, then enter the following to source the script:
  +
<pre>
  +
:so pydemo.vim
  +
</pre>
  +
  +
That defines a Python function <code>demo()</code> which you can call with:
  +
<pre>
  +
:py demo()
  +
</pre>
  +
  +
The Python example does the same as the Vim example: it prompts you to enter a name, then appends that name to the current line.
  +
  +
==References==
  +
*{{help|input()}}
  +
*{{help|confirm()}}
  +
*{{help|python}}
  +
  +
==Comments==

Latest revision as of 06:39, 13 July 2012

Tip 1616 Printable Monobook Previous Next

created 2009 · complexity basic · author Slackdna · version 7.0


This tip shows how to obtain input from the user from within a script. Examples showing Vim script and Python are given.

Overview[]

Vim provides the function input() that can display a prompt for the user, then wait for the user to type some input. To avoid problems with Vim's typeahead mechanism (which includes the processing of mappings), you need to first call inputsave(), then call input(), then inputrestore().

Often it is better to write a Vim command, so you can simply type the desired command and arguments when wanted (you don't need to be prompted for input). However, it is sometimes useful for a script to prompt the user to enter a required parameter.

If your are asking the user to confirm a choice, use instead the command

confirm(text[,choices[,default[,type]]])

(Note: confirm() is only supported when compiled with dialog support). For more details see the help reference.

Vim example[]

Save the following in a file called, say, vimdemo.vim:

function! Demo()
  let curline = getline('.')
  call inputsave()
  let name = input('Enter name: ')
  call inputrestore()
  call setline('.', curline . ' ' . name)
endfunction

Use Vim to edit any file, then enter the following to source (execute) the above script:

:so vimdemo.vim

That defines function Demo() which you can call with:

:call Demo()

The function gets the current line to a variable, prompts the user to enter a name, then appends the entered name to the current line. This is only a simple demonstration, and is not intended to do anything useful.

Python example[]

Save the following in a file called, say, pydemo.vim:

function! DefPython()
python << PYEND
import vim
def python_input(message = 'input'):
  vim.command('call inputsave()')
  vim.command("let user_input = input('" + message + ": ')")
  vim.command('call inputrestore()')
  return vim.eval('user_input')

def demo():
  curline = vim.current.line
  name = python_input('Enter name')
  vim.current.line = curline + ' ' + name
PYEND
endfunction
call DefPython()

Use Vim to edit any file, then enter the following to source the script:

:so pydemo.vim

That defines a Python function demo() which you can call with:

:py demo()

The Python example does the same as the Vim example: it prompts you to enter a name, then appends that name to the current line.

References[]

Comments[]