How do you send part of a command to vim(in Ex mode) using if_py?

I'm trying to open a file on Ctrl-f. If the command is typed in the presence of an empty buffer 'None' then I want the file to be opened in that buffer, but if there is no empty buffer I'd like to open a new buffer using :tabnew and then open the file in that. For this purpose I have a function OpenFile which is invoked.

function! OpenFile()
python << EOF
import vim
import re
buffer = vim.current.buffer
name = str(buffer.name)
if re.match('None', name):
    vim.command(':e ')
else:
    vim.command(':tabnew')
    vim.command(':e ')
EOF
endfunction

"Open file
 :map <C-f> :call OpenFile()<CR> 
:imap <C-f> <Esc>:call OpenFile()<CR>

vim.command executes the command so this is equivalent to :w!ENTER What I want to do is setup part of the command.. :e FILENAME ENTER

So I want to send the :e part in Ex mode via the python-function and get the user to type the filename and hit ENTER

Answers


First of all, why do you write this in Python? Sure, Vimscript is a bit strange (but since Vim 7 is has become a lot like Python), and you need to learn about the integration points, anyway, and this task has very little real logic in it.

This is easiest solved via a map-expression (:help map-expression):

:noremap <expr> <C-f> empty(bufname('')) ? ':edit ' : ':tabnew '

If you must, extract the conditional into a function and code it in Python, but I would recommend sticking to Vimscript, unless the logic is really complex or you could benefit from certain libraries.


Need Your Help

Shibboleth SP passing wrong user identity to application

saml adfs2.0 shibboleth

I have a Rails app that I've Shibbolized using Shibboleth SP (latest version - 2.4.2). I'm using it with Apache 2.2. My IdP is an MS AD FS 2.0 server.

Express wild card route with parts split

node.js express routes

I've googled and checked other answers at SO but they are not what I'm looking for.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.