Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to emulate ReSharper's "extend selection" feature in 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:-))

like image 967
dr Hannibal Lecter Avatar asked Apr 18 '09 00:04

dr Hannibal Lecter


2 Answers

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>
like image 134
michael Avatar answered Nov 12 '22 18:11

michael


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 :-)

like image 3
Jeremy Wall Avatar answered Nov 12 '22 19:11

Jeremy Wall