How do I create multiple cursors from search result?


#1

I use Atom on Ubuntu 17.10 with wayland window manager.

I can create multiple cursors with CTRL+Mousclick or with CTRL+d so select the next same string.

But how can I for example mark some lines and create a cursor at the start of each line?

Also would be great to use search and find-all to select lots of results in a text to create a cursor at all these results.

I workaround at the moment by selecting the line-break and use CTL+d to select the next linebreaks too with extra cursors , then go one left and Pos1 to have the cursors at the start of each line.


#2

This isn’t a solution, but why not just use the replace functionality? You can use capture groups with $n syntax. For example, I find "(\w+)": and replace it with $1: to remove quotes from CSON keys. It also supports \n and \t etc. in the replacement.


#3

You can always write a command in init.coffee that uses editor.scan() to find locations and then editor.addCursorAtBufferPosition() for each match.


#4

I use Atom version 1.23.3


#5

Hello.

Yes that is possible.
You can use the
(1)"find all" with the search tool then
(2) press Alt + Enter to create cursors at all the found locations.

Using ^(?=.+) as a (regular expression) search term and then press Alt + Enter, would place cursor at all lines that has text in them. Combine this with the “only in selection” and the range is reduced to be inside the original selected text.


If you want to convert selections to cursors you can try the following codes:

###   init.coffee   ###

# ****************************************************
#   CONVERT SELECTION TO MULTIPLE CURSORS
# ****************************************************
atom.commands.add 'atom-text-editor', 'custom:convert-selections-to-cursors', (e) ->
  _result = false
  if _editor = atom.workspace.getActiveTextEditor()
    if _editor.getSelectedText().length > 0
      # _checkpoint = _editor.createCheckpoint()
      for _range in _editor.getSelectedBufferRanges()
        for _row in [_range.start.row.._range.end.row]
          if not _result
            _result = true
            _editor.setCursorBufferPosition([_row,0])
          else
            _editor.addCursorAtBufferPosition([_row,0])
      # _editor.groupChangesSinceCheckpoint(_checkpoint)

  unless _result # procedure was aborted
    e.abortKeyBinding()
###   keymap.cson   ###

# ****************************************************
#   MAIN TEXT EDITOR
# ****************************************************
'atom-text-editor:not(.mini)':
  'ctrl-k ctrl-c': 'custom:convert-selections-to-cursors'

With those hints I can leave you in the good company of the forum friends.
Bye


#6

Thanks for the thorough answer!

This is what I was searching for. it is missing in the manual: https://flight-manual.atom.io/using-atom/sections/editing-and-deleting-text/#multiple-cursors-and-selections

I added it in a stackexchange post, so I hope others will find it this way: https://stackoverflow.com/questions/49233309/create-multicursor-in-atom