-1

I constantly need to copy existing variable names or expressions as arguments for my current typing function:

// int TyrionLannister;

int JonSnow;

...

int AryaStark;

...

// int DaenerysTargaryen;

foo();
   ^ current currsor

I want to change foo() to:

foo(JonSnow, AryaStark, TyrionLannister, DaenerysTargaryen)

I have used two methods to do the job so far:

  1. use autocomplete plugin such as ycm, i can type "js", and select "JonSnow" from popup list.

  2. sometimes plugin can't help me, because target variable is commented or it's some compound expression or whatever reason, i have to copy and paste by myself.

    I have to move my cursor to the variable if i want to copy it, sometimes i use G, sometimes i use HML and a lot of {,} and j k, some times even/ seemes to be a good choice.

    Sometimes i copy all variables into different registers abc..., then ` . or gi to go back to foo(), paste from a, add comma, paste from b, add comma, etc. Sometimes i copy only one variable, go back to foo(), add comma, paste it, then repeat the process for other variables.

Method 2 is quite awkward, i hate it so much that sometimes i type the arguments manually(i know i shouldn't do it manually).

How should i change my workflow? What's your workflow for this problem?

  • Macros? Since it's either autocomplete or visit each variable for the latter create a macro that yank-appends each variable name (plus comma) to a single register. Visit each and run the macro (just @@ after the first stop). Then paste the now populated register to the funcdef. Done. Not fancy but relatively painless. – B Layer Apr 14 at 6:44
  • Thank you @BLayer , Macro is another viable option. – dedowsdi Apr 14 at 6:54
  • 1
    Autocomplete works fine if you use vim’s regular <C-n> and <C-p>. Ive never needed a better completion system than help ins-comp. – D. Ben Knoble Apr 14 at 12:27
  • I had to admit, i'm spoiled by ycm. There are 13 mappings in :h ins-comp , i was frightened away when i saw it 2 or 3 years ago. – dedowsdi Apr 14 at 12:56
2

As @D. Ben Knoble mentioned, I'd use <C-n> and <C-p> which will look mostly (but not only, cf :help i_ctrl-n for more information) in the words in the opened files, making the completion quite "local", and appropriate in your case.

Regarding the completion of Vim, I'd encourage you to have a look at it, my favourite ones being the path completion (:help i_CTRL-X_CTRL-F) and the whole line completion (i_CTRL-X_CTRL-L). But the tag completion is definitely worth having a look. Take them and get comfortable with them one after the other, and it will be less daunting.

  • Thanks, i_CTRL-X_CTRL-L is nice. <c-n> and <c-p> only works for keywords, it won't work for compound expression such as std::max(i, j), it also won't work for words in commented lines(maybe can be changed via some configuration). I'm considering some yank ring plugin right now. And i can't leave ycm...i'm beyond saving. – dedowsdi Apr 15 at 8:42
  • Are you sure about ` it also won't work for words in commented lines? I tried with vim -u NONE /tmp/test.c, added a line // int hello = 1;` and in the next line typed h<c-n> which completed with hello. – padawin Apr 15 at 9:08
  • As i said "maybe can be changed via some configuration". I guess some c or cpp filetype or syntax plugin stopped it from being recongnized as keywords.. – dedowsdi Apr 15 at 9:24
  • By the way. Ycm also support keywords auto completion, it's marked as identifier, but i think it's the same thing. – dedowsdi Apr 15 at 9:34
  • Sorry, the fact that it was a c file was just an example, it behaves the same with any type of file, even plain text. <c-n> and <c-p> will complete any word, in comment or not, which are in the opened buffers. However if you want to complete a compound expression as the one you mentioned you'll need some external pluging indeed I am afraid. – padawin Apr 15 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.