t9md / atom-vim-mode-plus

vim-mode improved
https://atom.io/packages/vim-mode-plus
MIT License
1.4k stars 112 forks source link

[question] Command to add cursor for each preset-occurence #890

Closed bogdan0083 closed 7 years ago

bogdan0083 commented 7 years ago

Hello! I've been looking at 5th step in advanced tutorial and found that preset occurence one of the best things i use every day in mvp.

But in that step, you use c o p to change each occurence. Do we have a command to add cursor for each occurence?

Real example:

Let's say we have a long json file with many objects (about 100 objects with the same properties):

{
  "groupId": "11488395",
  "likePosts": false,
  "likeComments": true,
  "writeComments": false,
  "doRepost": false,
  "postsToProcess": 10,
  "chanceToLikeComment": 0,
  "chanceToRepost": 0.1,
  "postDelay": 100000
},
{
  "groupId": "1345287283",
  "likePosts": false,
  "likeComments": true,
  "writeComments": false,
  "doRepost": false,
  "postsToProcess": 10,
  "chanceToLikeComment": 0,
  "chanceToRepost": 0.1,
  "postDelay": 100000
}
// ... more the same objects

And we decided to add new property to each object. We can set cursor on likePosts property, press g o and then we can use c a e and get something like this:

{
  "groupId": "11488395",
  "|": false,
  "likeComments": true,
  "writeComments": false,
  "doRepost": false,
  "postsToProcess": 10,
  "chanceToLikeComment": 0,
  "chanceToRepost": 0.1,
  "postDelay": 100000
},
{
  "groupId": "1345287283",
  "|": false,
  "likeComments": true,
  "writeComments": false,
  "doRepost": false,
  "postsToProcess": 10,
  "chanceToLikeComment": 0,
  "chanceToRepost": 0.1,
  "postDelay": 100000
}
// ... more the same objects

We have our cursors, and we could use o to go on line below and add new property, but as you can see we have our property changed, so we need to write likePosts before going on line below.

It would be awesome to have some command that only add cursor on each occurence and doesn't change/deletes/yanks anything, like this:

{
  "groupId": "11488395",
  "|likePosts": false,
  "likeComments": true,
  "writeComments": false,
  "doRepost": false,
  "postsToProcess": 10,
  "chanceToLikeComment": 0,
  "chanceToRepost": 0.1,
  "postDelay": 100000
},
{
  "groupId": "1345287283",
  "|likePosts": false,
  "likeComments": true,
  "writeComments": false,
  "doRepost": false,
  "postsToProcess": 10,
  "chanceToLikeComment": 0,
  "chanceToRepost": 0.1,
  "postDelay": 100000
}
// ... more the same objects

It's probably very easy to do, but i can't find any info how to do it quickly with mvp

Thanks! :)

t9md commented 7 years ago

Question: How to insert "newProp": true to each of properties.

Sample text:

{
  "groupId": "11488395",
  "likePosts": false,
  "likeComments": true,
  "writeComments": false,
  "doRepost": false,
  "postsToProcess": 10,
  "chanceToLikeComment": 0,
  "chanceToRepost": 0.1,
  "postDelay": 100000
},
{
  "groupId": "1345287283",
  "likePosts": false,
  "likeComments": true,
  "writeComments": false,
  "doRepost": false,
  "postsToProcess": 10,
  "chanceToLikeComment": 0,
  "chanceToRepost": 0.1,
  "postDelay": 100000
}

Answer: There are many way to do so.

You can replace cmd-a part to v i e or v a e(omit v based on context). For cmd-e enter part, you can use cmd-enter(editor:newline-blow) unless some pkg overwrite this atom's default keymap.

FYI: I remap m for g o, and M for g O, since I use occurrence a lot, but don't use mark. So, my answer would be m cmd-a I cmd-enter then insert new prop.

t9md commented 7 years ago

But in that step, you use c o p to change each occurence. Do we have a command to add cursor for each occurence?

Direct answer for this is vim-mode-plus:select-occurrence.

By default enter in visual-mode is mapped to vim-mode-plus:create-persistent-selection. But if you don't use persistent-selection feature(I don't use than I though initially), you can remap enter like this in your keymap.cson. And also remap m and M.

'atom-text-editor.vim-mode-plus.visual-mode.is-narrowed':
  'enter': 'vim-mode-plus:select-occurrence'

'atom-text-editor.vim-mode-plus.normal-mode, atom-text-editor.vim-mode-plus.visual-mode':
  'm': 'vim-mode-plus:toggle-preset-occurrence'
  'M': 'vim-mode-plus:toggle-preset-subword-occurrence'

Then keystroke flow get really simple.

Answer: m cmd-a enter.

bogdan0083 commented 7 years ago

Answer: m cmd-a enter.

@t9md Holy Molly, it worked! Thank you very much. Learn something new everyday with vmp :smile:

You don't need cool IDE for refactoring features if know how to use preset-occurence + narrow plugin. Crazy stuff! :+1:

Closing

t9md commented 7 years ago

You don't need cool IDE for refactoring features if know how to use preset-occurence + narrow plugin. Crazy stuff! 👍

Yah! that's what I wanted. Preset-occurrence is technically marker-edit(mark > select-target > apply operator). It's general-purpose fundamental building-block of your advanced editing, it's not depends on language grammar, not depending of analysis of language semantics, thus always available!

t9md commented 7 years ago

After some experimenting/evaluating different way I explained in original answer section.

Here is current my conclusion for "recommended keystroke".

Why I recommend this is, this is a pattern you can use in variety of situation. No need to remember/master situation specific keymap like m cmd-a enter.

The pattern appar here is.

  1. mark by m
  2. select target (cmd-a here).
  3. apply operator(I to insert start of target, user A to insert at end of target).
  4. escape to return normal-mode.(clearMultipleCursorsOnEscapeInsertMode setting should be false for this pattern to work).

Why this is good is, you can use this pattern for delete, change, insert text. Thus most general-purpose. I want to remember/use small set of pattern which is usable in many situation than remembering different situation specific keymap.

bogdan0083 commented 7 years ago

Here is current my conclusion for "recommended keystroke".

m cmd-a I escape o then enter new prop. (Provided having 'm': 'vim-mode-plus:toggle-preset-occurrence' keymap.)

@t9md this combination doesn't work on Ubuntu actually and i think the problem is on the 2nd step:

  1. select target (cmd-a here).

cmd-a is mapped to vim-mode-plus:inner-entire (the same as i e) so it should probably be used after vim-mode-plus:select-occurrence, otherwise it doesn't work for me

I've also noticed that vim-mode-plus:select-occurrence is mapped to g cmd-d which works only on Mac i guess. I cannot reproduce g cmd-d on Linux. (maybe is it just me, i don't know). I checked default keymap and didn't found an alias for g cmd-d (for those who don't have cmd key).

If i add custom mapping like this: 'g d': 'vim-mode-plus:select-occurrence'

i can do m g d i e I escape o and it does the same stuff as m cmd-a I escape o

t9md commented 7 years ago

select target (cmd-a here). cmd-a is mapped to vim-mode-plus:inner-entire (the same as i e) so it should probably be used after vim-mode-plus:select-occurrence, otherwise it doesn't work for me

cmd-a here is executed in normal-mode(not mapped to inner-entire in this mode) and invoke core:select-all(vmp auto-start visual-mode by detecting selection change).

In your Linux environment, ctrl-a is mapped to core:select-all, but its conflicting with vim-mode-plus:increase, that's why not working in your environment.

t9md commented 7 years ago

Adding yet another answer based on upcoming v1.8.0. When keymapSToSelect enabled(which makes s work as select operator).

Or user preset-occurrence like this.