Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vim功能用于查找模式、更换和视觉模式_Vim - Fatal编程技术网

Vim功能用于查找模式、更换和视觉模式

Vim功能用于查找模式、更换和视觉模式,vim,Vim,所以我使用Vim在工作中编写报告,报告基本上是一堆我们反复编写的“常见问题”,所以它们是模板化的。这些模板具有占位符块{==BLOCK=},以确保人们在需要时修改内容,因此这是一个示例: The test revealed that it was possible to access {==sensitive data==}, as shown in the examples below... 该块可能需要修改,也可能不需要修改。因此,我的想法是,我正在编辑常见问题,我看到有3或4个块与示例中

所以我使用Vim在工作中编写报告,报告基本上是一堆我们反复编写的“常见问题”,所以它们是模板化的。这些模板具有占位符块
{==BLOCK=}
,以确保人们在需要时修改内容,因此这是一个示例:

The test revealed that it was possible to access {==sensitive data==},
as shown in the examples below...
该块可能需要修改,也可能不需要修改。因此,我的想法是,我正在编辑常见问题,我看到有3或4个块与示例中的块类似,我想按[leader]b,然后在视觉模式下为第一个块选择模板文本,而周围没有
{==
=}

我尝试了一些东西,但没有走得太远,有什么建议吗


谢谢

大多数模板/代码片段扩展插件都支持这一点

使用我的插件,您可以执行

:SetMarkers {== ==}
然后使用vim使用CTRL+J从一个占位符跳到下一个占位符,或者使用gvim使用META-DEL。不负责加载/扩展模板。将添加此层

如果您选择使用一个更流行的代码段插件,肯定会有一个选项来更改占位符的语法,但我不知道

穷人的解决方案如下:

nnoremap <c-j> <c-\><c-n>/{==.*==}<cr>v/==}/e<cr><c-g>
snoremap <c-j> <c-\><c-n>/{==.*==}<cr>v/==}/e<cr><c-g>
nnoremap/{==.*=}v/==}/e
snoremap/{==.*=}v/==}/e
但它不会恢复搜索模式,也不会恢复光标已经在占位符中的情况,等等

编辑:自动删除占位符标记的版本为

nnoremap <c-j> <c-\><c-n>/{==.*==}<cr>v/==}/e<cr>s<c-r>=matchstr(@", '{==\zs.*\ze==}')<cr><esc>gv6h<c-g>
nnoremap/{==.*=}v/==}/es=matchstr(@“,“{==\zs.*\ze==}”)gv6h
  • 在snoremap中也一样

您可以定义以下功能:

function! VisualSelect()                                                 
    call search("==}")                                                      
    norm hvT=                                                             
endfunction                                                                 
nnoremap <leader>b :call VisualSelect()<cr> 
vnoremap <leader>b Ol<esc>:call VisualSelect()<cr>
函数!VisualSelect()
呼叫搜索(“=}”)
标准hvT=
端功能
nnoremap b:调用VisualSelect()
vnoremap b Ol:调用VisualSelect()
这将直观地选择
{=
=}
之间的内容。反复键入
b
将选择下一次出现的内容。

简而言之:

nnoremap <leader>b /{==.*==}<cr>xxxvt=<esc>/==}<cr>xxxgv
3.)可视化选择文本,直到第一个=(可能这也可以优化为使用正则表达式,而不是简单地搜索下一个=)

4.)转到结束序列

/==}<cr>
6.)再次选择您最后的选择

gv

根据@snap所说的,我找到了一种方法,最后我将代码添加到一个Python插件中以运行它,因为这更符合我正在尝试做的事情,下面的代码片段:

@neovim.command('VimisEditTemplateBlock')
def urlify(self):
    """Search next codify block and prepare for editing"""
[...]
    self.nvim.command('call search("{==")') # Find beginning of codify block
    self.nvim.command('norm xxx') # Delete {==
    self.nvim.command('norm vf=') # Select the inner text
    self.nvim.command('norm v')
    self.nvim.command('norm xxxgvh') #Delete ==} and leave the inner text selected in Visual Mode

抱歉,也许我没有正确地表达自己,但我不想用REGEX替换里面的内容,我希望函数找到块,去掉{==和==},然后保持块中的模板文本不变,但在视觉模式下选择谢谢,但仅为这项任务使用2个插件对我来说似乎有点过分!关于“可怜的人的解决方案”,问题是它不会删除{==和==}位,这是我想自动化的部分。谢谢!第二部分很容易完成,一旦在视觉模式下,用(
s=
)替换内部部分(用
matchstr
substitute()
),然后重新选择(
gv
),修复结束(
6h
)并转到选择模式(
)。
nnoremap/{==.*=}v/=}/es=matchstr(@“,“{==\zs.*\ze=}”)gv6h
,但实际上,在大多数用例中,选择模式已经足够了,因为最后必须将占位符替换为与占位符描述不同的内容。使用
xnoremap
仅允许在视觉模式下进行此操作,而不允许同时选择选择模式。
/==}<cr>
xxx
gv
@neovim.command('VimisEditTemplateBlock')
def urlify(self):
    """Search next codify block and prepare for editing"""
[...]
    self.nvim.command('call search("{==")') # Find beginning of codify block
    self.nvim.command('norm xxx') # Delete {==
    self.nvim.command('norm vf=') # Select the inner text
    self.nvim.command('norm v')
    self.nvim.command('norm xxxgvh') #Delete ==} and leave the inner text selected in Visual Mode