使VIM函数返回不缩进的文本
我想这个问题可以用两种方式来解决...
(通用)-有没有一种方法可以指定函数的“本地”设置(
setlocal
更改似乎在函数调用后仍然存在)...(具体)-我有一个从某个函数调用的函数
imap
映射(将用户输入传递到函数中。如果我运行set Paste
或set noai | set nosi
,该函数将完美运行无论是在运行我的快捷方式之前,还是添加到函数本身中,问题是,无论我采用哪种方式,这些设置更改在我的函数调用之后仍然存在。
本质上,我的工作流程是:
- 在插入模式下,输入
////
,此时系统会提示我输入文本,我输入该文本并按 Enter 键。 该函数是根据我的输入调用的。我需要该函数来禁用缩进,返回我的字符串,然后重新启用以前的设置。该字符串只是一个 PHP 块注释,如下所示:
<前><代码>/** * 废话{输入文本} */
任何建议表示赞赏。我的脚本目前看起来像这样:
function CommentInjector(txt)
return "\/**" ."\<CR>"
\ . " * foo " . a:txt . " bar " . "\<CR>"
\ . " */"
endfunction
imap <silent> //// <C-R>=CommentInjector(input("Enter some text:"))<CR>
UPDATE
Managed to find it at at least how to dump a comment in... 希望知道如何获取/恢复设置...
function! CommentInjector(txt)
set paste
exe "normal! i/**\<CR>"
\ . " * fooo " . a:txt . " bar\<CR>"
\ . " */\<Esc>"
set nopaste
endfunction
map <C-C><C-C><C-C> :call CommentInjector(input("Enter some text:"))<CR>
使用此功能,您只需按 Ctrl+C 3 次,输入提示时输入文本,您会收到一条很好的注释。它假设您在运行之前禁用了“设置粘贴”...
I guess this question could be taken in two ways...
(Generic) - is there a way to specify settings 'local' to a function (
setlocal
changes seem to persist after the function call)...(Specific) - I have a function which gets called from an
imap
mapping (which takes a user input to pass into the function. The function works perfectly if I runset paste
orset noai | set nosi
either just before running my shortcut, or added into the function itself. The problem is, whichever way I do it, those setting changes persist after my function call.
Essentially, my workflow is:
- In insert mode, type
////
at which point I get prompted for input text, which I enter and press enter. The function is called with my input. I need the function to disable indenting, return my string and then re-enable the previous settings. The string would just be a PHP-block comment like this:
/** * Blah {INPUT TEXT} */
Any suggestions appreciated. My script currently looks like this:
function CommentInjector(txt)
return "\/**" ."\<CR>"
\ . " * foo " . a:txt . " bar " . "\<CR>"
\ . " */"
endfunction
imap <silent> //// <C-R>=CommentInjector(input("Enter some text:"))<CR>
UPDATE
Managed to figure it out at least how to dump a comment in... Would appreciate knowing how to get/restore settings though...
function! CommentInjector(txt)
set paste
exe "normal! i/**\<CR>"
\ . " * fooo " . a:txt . " bar\<CR>"
\ . " */\<Esc>"
set nopaste
endfunction
map <C-C><C-C><C-C> :call CommentInjector(input("Enter some text:"))<CR>
Using this you can just pres Ctrl+C 3 time, enter text when prompted and you get a nice comment written in. It assumes you had "set paste" disabled before running though...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您已经发布了更新并且实际上只是在研究如何保存/恢复设置,因此我将提供一个通用解决方案。
let save_paste = &paste
paste
进行任何您想要进行的更改let &paste = save_paste
可以在
:help use-cpo-save
的文档中找到这样的示例,其中讨论了保存和恢复值cpoptions
。Since you've posted an update and are really just looking at how to save/restore settings, I'll give a general solution.
let save_paste = &paste
paste
that you'd like to makelet &paste = save_paste
An example of this can be found in the documentation with
:help use-cpo-save
where they talk about saving and restoring the value ofcpoptions
.