3

I use this key mapping to duplicate a line, and go to the same cursor position on this newly created line:

nnoremap , mqYp`qj

What this does:

  1. Create mark 'q'
  2. Yank line
  3. Paste/put line (below current line, cursor is now at start of new line)
  4. Go back to mark at previous line
  5. Move one line down. (cursor is now on new line at the same place of start of previous line)

This works perfectly fine, however I see a flaw when putting a number in front of the command. Imagine I want to duplicate this line 10 times. It would try to create 10 marks basically. I could do Y10p for this, I do understand that. My problem with that approach is that I'm not on the same cursor position as I was in the first line, the one I'm duplicating.

So I'm looking for a way to do basically 10,, using my previously made mapping, and ending on the last line, at the same cursor position of where I was in the first line. Note that I am using IdeaVIM exclusively to code, which means I can't make any functions for this.

Is it possible to get this 10, working in this situation?

Edit #1:

Example text

# Start
# Initialize new variables
new_invoice_name_one = 'New Name One'
new_invoice_name_two = 'New Name Two'
new_invoice_address_one  = 'New Address One'

Command executed: 3, with the cursor being on the first I of line 2

Desired output

# Start
# Initialize new variables
# Initialize new variables
# Initialize new variables
# Initialize new variables

new_invoice_name_one = 'New Name One'
new_invoice_name_two = 'New Name Two'
new_invoice_address_one  = 'New Address One'

with the cursor being on the first I of line 5 `

Edit #2:

I see some potential at the LetHandler.java here, however I can not seem to figure out how to use it to match the use case. On the other hand, here it says it is not supported at all.

romainl
  • 186,200
  • 21
  • 280
  • 313
Edwin
  • 43
  • 5

2 Answers2

2

When I play vimgolf, I use a trick [count]@='... sometimes. It could be used for your requirement.

you can map:

nnoremap , @='mqYp`q'<cr>

Then you can just simple press 200, to achieve what you want.

Kent
  • 189,393
  • 32
  • 233
  • 301
  • With the addition of a `j` (`nnoremap , @='mqYp\`qj'`) this works as expected in VIM. However, IdeaVIM does not seem to support this (as expected). `200,` would not make a difference in comparison to just typing `,`. Even `,` does not work the same as this command does in VIM. It duplicates the current line, then goes one line below the duplicated one (with my edited command, so inluding the `j`. – Edwin May 03 '17 at 09:22
  • @EdwinvanRooij I didn't notice that you need this in ideavim. I use ideavim too, however, just for very basic vim function. After all it is not vim, some simulations are buggy. But I have to say, it is good enough. I have a rather long (>1000lines) vimrc file, it is increasing, since I change the config, create new functions etc. from time to time. But I won't invest too much time on ideavim. – Kent May 03 '17 at 09:34
  • For me IdeaVIM is a **huge** thing. I do all my programming in IntelliJ IDEs (CLion, Android Studio, PyCharm, etc). I noticed myself copying print statements a lot, for example. That's one use case for this feature. I even mapped `nnoremap ; :action VimFilePrevious` to go to the last used tab. It feels so smooth and efficient. Anyway, that's why I'm quite eager to get this working. I'll use it in any language and any IDE I use. + it opens up possibilities for my other commands. – Edwin May 03 '17 at 09:48
1

You can achieve this by pasting to the above of current line. Do this nnoremap , YmqP`q

update

You cannot do that since vim's key mapping is just string concatenation. One way to achieve this is to predefine a macro. Put this in the related rc file,

let @q="mqYP`q"
nnoremap , @q
Community
  • 1
  • 1
Amos
  • 3,238
  • 4
  • 19
  • 41
  • Maybe I haven't made myself clear, I want to copy and paste the _current_ line for an `x` amount of times. `10,` with you mapping would yank the _next_ ten lines. – Edwin May 03 '17 at 08:20
  • @EdwinvanRooij Does the updated solution work for you? – Amos May 03 '17 at 09:31
  • It does not, nothing happens in IdeaVIM. In normal VIM this is a good solution. I wanted to continue experimenting in IdeaVIM before posting back, oh well. I have however noticed some kind of progress indicating it might work in IdeaVIM as well, when I enter your `let` as a command, it says `VIM - Only simple '=' assignments are supported in 'let' expressions` – Edwin May 03 '17 at 09:42