使VIM函数返回不缩进的文本

发布于 2024-12-15 21:53:02 字数 1337 浏览 1 评论 0原文

我想这个问题可以用两种方式来解决...

  1. (通用)-有没有一种方法可以指定函数的“本地”设置(setlocal 更改似乎在函数调用后仍然存在)...

  2. (具体)-我有一个从某个函数调用的函数imap 映射(将用户输入传递到函数中。如果我运行 set Pasteset noai | set nosi,该函数将完美运行无论是在运行我的快捷方式之前,还是添加到函数本身中,问题是,无论我采用哪种方式,这些设置更改在我的函数调用之后仍然存在。

本质上,我的工作流程是:

  1. 在插入模式下,输入 ////,此时系统会提示我输入文本,我输入该文本并按 Enter 键。
  2. 该函数是根据我的输入调用的。我需要该函数来禁用缩进,返回我的字符串,然后重新启用以前的设置。该字符串只是一个 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...

  1. (Generic) - is there a way to specify settings 'local' to a function (setlocal changes seem to persist after the function call)...

  2. (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:

  1. In insert mode, type //// at which point I get prompted for input text, which I enter and press enter.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸵鸟症 2024-12-22 21:53:02

由于您已经发布了更新并且实际上只是在研究如何保存/恢复设置,因此我将提供一个通用解决方案。

  • 在函数开始时保存设置的初始值: let save_paste = &paste
  • paste 进行任何您想要进行的更改
  • 将其恢复到end: 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.

  • 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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文