有没有办法模拟ReSharper的“扩展选择” Vim 中的功能?

发布于 2024-07-17 05:25:18 字数 483 浏览 4 评论 0原文

ReSharper 有一个很好的功能,称为“扩展选择”:通过反复按 CTRL+W(我认为这是默认设置),您可以从当前插入符号位置选择越来越多的内容。 首先是一个单词,然后是越来越多的单词、一行、内部和外部的行块(例如 if 块),然后是一个函数,等等......

基本上,通过重复按组合键,您可以结束选择整个文件。 我相信至少你们中的一些人会熟悉它。

我刚刚开始学习 vim 的所有复杂之处,并且没有足够的经验来了解如何在 Vim 中实现这样的东西(尽管我认为这是可能的)。 所以我的问题是针对 Vim 专家的:这可以做到吗?如何做到?

更新:一些背景故事。 我一直在和我的前老板谈论 Vim 的所有好处,他认为这都很棒。 他唯一的问题是:有“扩展选择”吗?到目前为止我的问题是没有。 所以,如果有人知道答案,我最终会赢得一场讨论:P(也许会创建一个新的 Vim 转换:-)

ReSharper has a nice feature called "extend selection": by pressing CTRL+W (I think this is the default) repeatedly, you select more and more from your current caret location. First it's a word, then more and more words, a line, inner then outer block of lines (for example an if-block), then a function, etc...

Basically, by pressing the key combination repeatedly, you can end up selecting the entire file. I'm sure at least some of you will be familiar with it.

I have just started learning all the intricacies of vim and I don't have enough experience to see how something like this could be implemented in Vim (although I assume it's possible). So my question is meant for Vim gurus out there: can this be done and how?

Update: a bit of a background story. I've been talking to my ex-boss about all the benefits of Vim, and he thinks it's all great. His only question/problem was: does it have "extend selection"? My question so far has been no. So, if someone knows the answer, I'll finally win a discussion :P (and maybe create a new Vim convert:-))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

香橙ぽ 2024-07-24 05:25:18

我快速解决了这个问题。 它不能按原样工作。 如果您对其进行了改进,请随意进行编辑并发布在 vim wiki 上或作为插件。

您可能想为每种语言创建 ag:resharp_list (例如,为括号语言等创建 ag:resharp_list )。

所需要的只是原始光标位置的标记:标记和重置索引的超时自动命令。

"resharp emulator
"TODO this needs a marker
"also c-w is bad mapping as it has a lag with all the other-
"window mappings
"
let g:resharp_index = 0

let g:resharp_select =  ['iw', 'is', 'ip', 'ggVG']

func! ResharpSelect()
    if g:resharp_index >= len (g:resharp_select)
        let g:resharp_index = 0
    endif

    exe "norm \<esc>v" . g:resharp_select[g:resharp_index]
    let g:resharp_index = g:resharp_index + 1
endfun

nnoremap <c-w>  :call ResharpSelect()<cr>
vnoremap <c-w>  :call ResharpSelect()<cr>

"Something to reset on timeout. TODO this doesn't work
au CursorHold :let g:resharp_index = 0<cr>

I had a quick go at this problem. It doesn't work as is. Feel Free to make edits and post on the vim wiki or as a plugin if you get it refined.

chances are you'd want to make a g:resharp_list for each language (eg. one for paranthesised languages, etc.)

All that is needed is a marker for the original cursor position :he markers and a timeout autocommand that resets the index.

"resharp emulator
"TODO this needs a marker
"also c-w is bad mapping as it has a lag with all the other-
"window mappings
"
let g:resharp_index = 0

let g:resharp_select =  ['iw', 'is', 'ip', 'ggVG']

func! ResharpSelect()
    if g:resharp_index >= len (g:resharp_select)
        let g:resharp_index = 0
    endif

    exe "norm \<esc>v" . g:resharp_select[g:resharp_index]
    let g:resharp_index = g:resharp_index + 1
endfun

nnoremap <c-w>  :call ResharpSelect()<cr>
vnoremap <c-w>  :call ResharpSelect()<cr>

"Something to reset on timeout. TODO this doesn't work
au CursorHold :let g:resharp_index = 0<cr>
不顾 2024-07-24 05:25:18

答案是肯定的。 进入可视模式后,您可以使用所有常规导航方法以及一些额外的方法。

我最喜欢的一些? 首先在正常模式下按 v 进入可视模式,然后按:

  1. iw - 选择内部单词。 非常适合选择一个单词,同时排除周围的大括号或引号
  2. w - 多次点击以继续选择每个后续单词。
  3. b - 选择逐字反义词
  4. ^ - 选择从当前位置到行文本开头的所有内容
  5. $ - 选择从当前位置到行尾的所有内容

我相信这里的其他人也可以添加到这个列表中。 哦,别忘了 Visual Block 模式 Cv 在 vi​​m 中使用上面的命令尝试它在二维中工作:-)

The answer is yes. Once in Visual mode you can use all the regular navigation methods as well as some extra ones.

Some of my favourites? First hit v while in normal mode to get to visual mode then hit:

  1. iw - to select the inner word. Great for selecting a word while excluding surrounding braces or quotes
  2. w - hit multiple times to keep selecting each subsequent word.
  3. b - select wordwise backwords
  4. ^ - select all from current position to beginning of text on line
  5. $ - select all from current position to end of line

I'm sure others here could add to this list as well. Oh and don't forget Visual Block mode C-v try it out in vim with the above commands it works in two dimensions :-)

混吃等死 2024-07-24 05:25:18

如果您正在谈论 Vim(您应该是:-),您可以开始使用 v 命令标记文本,然后您就拥有了所有标准光标移动命令(并且,如您所知,有有很多),这将扩展选择范围并移动光标。

然后您只需对选定的文本执行任何您想要的操作即可。

请参阅此处了解详细信息。

If you're talking about Vim (and you should be :-), you can start marking text with the v command, then you have all the standard cursor movement commands (and, as you know, there are a lot of them) which will extend the selection, as well as moving the cursor.

Then you just do whatever you want with the selected text.

See here for the gory details.

对你而言 2024-07-24 05:25:18

人们需要编写一个函数来保存当前选择,然后尝试越来越宽的选择,直到新选择超过已保存的选择或选择所有文本。 一些可能的选择是:

  • viW - 选择单词
  • vis - 选择句子
  • vip - 选择段落
  • viB - 选择最内括号内的文本
  • v2iB - 选择下一个最内括号内的文本
  • ggVG - 选择所有文本

One would need to write a function that would save the current selection, then try increasingly wide selections, until the new selection exceeds the saved one or selects all text. Some possible selections are:

  • viW - select word
  • vis - select sentence
  • vip - select paragraph
  • viB - select text within the innermost brackets
  • v2iB - select text within the next most innermost brackets
  • ggVG - select all text
ま昔日黯然 2024-07-24 05:25:18

我认为杰里米·沃尔正朝着正确的方向前进。 为了在这个方向上更进一步,您可以查看 Tim Pope 的“surround.vim”脚本。 github 上提供了很好的描述。 或者,如果您愿意,可以从 vim.org 获取它。 它可能会帮助您做一些您想做的事情,尽管它似乎没有一个功能,例如简单地在标签内进行选择。 如果我错了请告诉我。

最终,您真正想要的是封闭文本对象的层次结构。 如果还没有的话,你应该阅读有关文本对象的内容。 此处有一个很好的概述。 请注意,您可以使用计数一次性抓取多个对象,或者迭代地执行此操作(在正常模式下尝试 vawasap}}})。

您还可以获得定义其他文本对象的脚本,例如使用缩进的 这个定义一个文本对象。 如果您按照通用标准进行格式化,并且保证适用于 python,它将适用于多种语言。

一个烦恼是光标最终位于可视块的末尾,因此,例如,您无法轻松选择某些 () 之间的所有内容,然后获取它们前面的函数名称......

但是,我刚刚在 这篇文章 中发现您可以更改此设置与 o 的行为。 凉爽的!

我怀疑从长远来看,您会发现自己能够跳过中间选择,从而提高自己的效率。

无论如何,我很想看看其他人是否也提出了更通用的解决方案!

I think Jeremy Wall's heading in the right direction. And to get a little further in that direction, you might look at the "surround.vim" script from Tim Pope. A good description is available on github. Or, if you'd rather, get it from vim.org. It'll probably help you do some of the things you'd like to do, though it doesn't seem to have a feature for say, simply selecting within a tag. Let me know if I'm wrong.

Ultimately, what you'd really like is a hierarchy of enclosing text-objects. You should read up on text-objects if you haven't. A nice overview is here. Note that you can grab multiple objects in one go using counts, or do this iteratively (try vawasap}}} from normal mode).

You can also get scripts which define other text-objects, like this one that uses indentation to define a text-object. It'll work for many languages if you're formatting according to common standards, and guaranteed for python.

One annoyance is that the cursor ends up at the end of the visual block, so, for example, you can't easily select everything between some ()'s, then get the function name that precedes them...

...BUT, I just found in this post that you can change this behavior with o. Cool!

I suspect you'll find yourself more efficient being able to skip over intermediate selections in the long run.

Anyway, I'll be curious to see if anyone else comes up with a more general solution as well!

不奢求什么 2024-07-24 05:25:18

在 Rider 中 [在与 IdeaVim 绑定 VS Mac 的 Mac 上],我将:

  • Ctrl+= 绑定到扩展选择
  • Ctrl+-Shrink Selection

不会与任何其他后果绑定发生冲突,并且不需要 v 进行模式切换,并且比 Cmd+Option+->Cmd+Option+< 更容易code><-


把它放在这里,因为我总是在任何 Rider Vim 选择 搜索中遇到这个问题。 如果我受到足够的骚扰,我将创建一个自我回答“如何在 Rider Vim 模式下使用扩展选择”。

In Rider [on a Mac with VS Mac bindings with IdeaVim], I bind:

  • Ctrl+= to Extend Selection
  • Ctrl+- to Shrink Selection

Doesn't clash with any other bindings of consequence and doesn't require a v for mode switching, and easier than Cmd+Option+-> and Cmd+Option+<-


Putting it here as I always hit this question with any Rider Vim selection searches. If I get enough harassment, I'll create a self-answered "How to use Extend Selection with Rider Vim mode".

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