VIM命令插入带参数的多行文本 [英] VIM command to insert multiline text with argument

查看:121
本文介绍了VIM命令插入带参数的多行文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的VIM用户.我正在尝试使我的类定义的创建python属性更容易.我想说的是我输入的内容

new VIM user. I'm trying to make creating python properties easier for my class definitions. What I would like for say I type

:pyp x

然后VIM会自动填充我的光标所在的位置

then VIM will autofill where my cursor is

@property
def x(self):
   return self.x
@property.setter
   def x(self,val):
      self._x = val

或更抽象地输入

:pyp <property_name>

VIM填充

@property
def <property_name>(self):
   return self.<property_name>
@property.setter
   def <property_name>(self,val):
      self._<property_name> = val

我看过一些关于函数,宏的文章和wiki,但是我不确定如何使用它或什至要查找什么内容,因为我是一个不到一周的全新VIM用户.

I've looked at a few posts and the wikis on functions, macros but I'm very unsure of how to go about it or what to even look up as I am brand new VIM user, less than a week old.

我尝试在[.vimrc]中以[this] [1]为例,但是我什至无法使它正常工作.

I tried using [this][1] as an example, in my .vimrc but I couldn't even get that to work.

所以我当前正在尝试的代码是

So the code I am currently trying is

function! PythonProperty(prop_name)
 let cur_line = line('.')
 let num_spaces = indent('.')
 let spaces = repeat(' ',num_spaces)
 let lines = [ spaces."@property",
             \ spaces."def ".prop_name."(self):",
             \ spaces."   return self.".property,
             \ spaces."@property.setter",
             \ spaces."def".prop_name."(self,val)",
             \ spaces."   self._".prop_name." = val" ]
 call append(cur_line,lines)
endfunction

and I am getting the errors

E121:未定义的变量:prop_name

E121: Undefined variable: prop_name

I am typing
`:call PythonProperty("x")`

  [1]: https://vi.stackexchange.com/questions/9644/how-to-use-a-variable-in-the-expression-of-a-normal-command

推荐答案

E121:未定义的变量:prop_name

E121: Undefined variable: prop_name

在VimScript中,变量具有作用域.函数参数的范围是a:,而函数内部的默认值是l:(局部变量).因此,该错误意味着尚未定义l:prop_name.

In VimScript variables have scopes. The scope for function arguments is a:, while the default inside a function is l: (local variable). So the error means that l:prop_name was not yet defined.

现在我该怎么做:

function! s:insert_pyp(property)
    let l:indent = repeat(' ', indent('.'))
    let l:text = [
        \ '@property',
        \ 'def <TMPL>(self):',
        \ '    return self.<TMPL>',
        \ '@property.setter',
        \ '    def <TMPL>(self,val):',
        \ '        self._<TMPL> = val'
    \ ]
    call map(l:text, {k, v -> l:indent . substitute(v, '\C<TMPL>', a:property, 'g')})
    call append('.', l:text)
endfunction

command! -nargs=1 Pyp :call <SID>insert_pyp(<q-args>)

或者,我们可以模拟实际的按键操作(请注意,我们不再需要在模板中放入缩进;希望当前的缓冲区具有set ft=python):

Alternatively, we can simulate actual key presses (note that we don't need to put indents in the template anymore; hopefully, the current buffer has set ft=python):

function! s:insert_pyp2(property)
    let l:text = [
        \ '@property',
        \ 'def <TMPL>(self):',
        \ 'return self.<TMPL>',
        \ '@property.setter',
        \ 'def <TMPL>(self,val):',
        \ 'self._<TMPL> = val'
    \ ]
    execute "normal! o" . substitute(join(l:text, "\n"), '\C<TMPL>', a:property, 'g') . "\<Esc>"
endfunction

command! -nargs=1 Pyp2 :call <SID>insert_pyp2(<q-args>)

很难甚至不可能获得插件

its very very difficult if not impossible to get pluggins

我建议您在YouTube上观看此视频.实际上,许多Vim插件都不过分.

I suggest you to watch this video on youtube. In fact, many of Vim plugins are just overkill.

这篇关于VIM命令插入带参数的多行文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆