使VIM函数返回没有缩进的文本 [英] Make VIM function return text without indent
问题描述
我想这个问题可以通过两种方式解决...
I guess this question could be taken in two ways...
-
(通用)-有没有一种方法可以为功能指定本地"设置(在功能调用后
setlocal
更改似乎仍然存在)...
(Generic) - is there a way to specify settings 'local' to a function (
setlocal
changes seem to persist after the function call)...
(特定)-我有一个从imap
映射调用的函数(需要用户输入才能传递给该函数.如果我运行set paste
或set noai | set nosi
,则该函数可以完美运行
(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 run set paste
or set 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:
- 在插入模式下,键入
////
,这时会提示我输入输入文本,然后输入并按Enter. -
该函数使用我的输入进行调用.我需要禁用缩进功能,返回我的字符串,然后重新启用先前的设置.该字符串只是这样的PHP块注释:
- 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>
使用此功能,您可以按Ctrl + C 3次,在出现提示时输入文本,并且会写上漂亮的注释.它假定您在运行之前已禁用设置粘贴" ...
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...
推荐答案
既然您已经发布了更新,并且实际上只是在研究如何保存/恢复设置,所以我将给出一个通用的解决方案.
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
进行任何更改 - 将其恢复到最后:
let &paste = save_paste
- At the start of your function save the initial value of the setting:
let save_paste = &paste
- Make any changes to
paste
that you'd like to make - Restore it at the end:
let &paste = save_paste
在:help use-cpo-save
的文档中可以找到有关此示例的内容,他们讨论如何保存和恢复cpoptions
的值.
An example of this can be found in the documentation with :help use-cpo-save
where they talk about saving and restoring the value of cpoptions
.
这篇关于使VIM函数返回没有缩进的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!