How to deselect text


#1

I want to know how to deselect the currently selected text (without having to move the cursor up/down or left/right).
Specifically, I want to bind this behavior to escape key, but I couldn’t find any command that does this.
Does anyone know how to do this?


#2

Hello.

I tried the following in Atom V1.18 on Windows7…

// "init.coffee"
atom.commands.add 'atom-text-editor', 'custom:deselect', ->
  _editor = atom.workspace.getActiveTextEditor()
  _origin = _editor.getCursorScreenPosition()
  _editor.setSelectedBufferRange([_origin,_origin])
# "keymap.cson"
'atom-text-editor':
    'escape': 'custom:deselect'

Helpful?
- Dan Padric


#3

Hey @danPadric, thanks for the tip! Unfortunately, now the default behavior of escape is not working anymore (like quitting a menu or something like that). Any idea on how can I add this new behavior to escape key without overriding any other behavior? Thanks!


#4

Hi.

The code probably needs to have a test to see if the editor is active. :thinking:

Please give me an exact example on where ESC is not working as expected. If I use ESC after selecting a menu option it does execute as before.

Regards.


#5

…okay, I could reproduce the issue you mention when triggering the FIND pane.

- - - - Please try the following binding for me:

# "keymap.cson"
'atom-text-editor:not([mini])':
    'escape': 'custom:deselect'

#6

Hey @danPadric thanks again for all the support!

This time it worked with the FIND pane (as you tested), but I noticed later another problem: when the autocomplete menu is active the escape key stopped quitting the menu.

In other words, maybe we need another selector for binding the key, perhaps one specific when the text is selected and the file is focused. If anyone knows this please post here, I’ll continue searching for some solution.


#7

Hi there, I have a new plan.
:blush: (It is all about reading the manual!

# "init.coffee"
atom.commands.add 'atom-text-editor', 'custom:deselect', (e) ->
  if !(_editor = atom.workspace.getActiveTextEditor()) || _editor.getSelectedText().length == 0
    e.abortKeyBinding()
    return

  # console.log "Deselect the text."  # for diagnostic purposes
  _origin = _editor.getCursorScreenPosition()
  _editor.setSelectedBufferRange([_origin,_origin])
# "keymap.cson"
'atom-text-editor:not([mini])':
    'escape': 'custom:deselect'

Reference:
http://flight-manual.atom.io/behind-atom/sections/keymaps-in-depth/#overloading-key-bindings

Regards.
- Dan Padric


#8

Hey Dan! Thanks again for your help and sorry for taking all this time for replying, I was really busy these days.

Unfortunately, this new solution still does not allow some behaviors of escape to be the way they should—for example, when the autocomplete popup shows it does not cancel it anymore.

I will take a look at the link you provided to see if I find another approach. Thanks anyway for the great support and references.

Cheers,
Arthur


#9

Hello - good to hear from you.

I am learning as I go. Your request was something I wanted too. So that is
why I took on the challenge yet it is not fully formed as the pros whould
do it. Please be patient with my think-out-loud style below.

Since we spoke last, I have made some changes to the code… yet did not
update the post.

:blush: (Sorry about that !)

Regardless of the oversight, I find it strange that you would have an
issue you describe. The issue as you describe was a problem before.

Do remember to restart Atom before trying out changes made in
init.coffee. Do that by closing down and opening Atom again or
[ctrl-shift-F5] key combination. Does this help already?

The reason is the section of code that checks
_editor.getSelectedText().length == 0 should have aborted further
processing. The idea is that you cannot select code and still have the
autocomplete do its job. Am I assumption correct?

If all else fails, we can include a check for autocomplete… but that is
forcing the issue. It would be nice if we could execute both 'old’
key-binding and new custom one.

What I have seen today in Microsoft’s Visual Studio Code is an
environment check if the text editing has focus. But that probably would
not help either.

The variation in the theme then follows:

# "init.coffee"
atom.commands.add 'atom-text-editor', 'custom:deselect', (e) ->
  # test if abort function needed
  e.abortKeyBinding() unless _editor = atom.workspace.getActiveTextEditor()
  e.abortKeyBinding() if _editor.getSelectedBufferRange().isEmpty()

  # deselect selection
  _origin = _editor.getCursorScreenPosition()
  _editor.setSelectedBufferRange([_origin,_origin])```
```coffee
# "keymap.cson"
'atom-text-editor:not([mini])':
    'escape': 'custom:deselect'

I would appreciate your thoughts and feedback.

- Dan Padric


#10

Hey @danPadric thanks again for the help! This time it worked correctly (with the suggestion above).

However, I also came across a package named selection-mode, where deselection is done in a very neat way, so for now I decided to stick to that (but using your insights of aborting the key binding if the selection is empty). This new code should also work when there are multiple cursors active.

# in "init.coffee"
atom.commands.add 'atom-text-editor', 'custom:deselect', (e) ->
    _editor = atom.workspace.getActiveTextEditor()
    for selection in _editor.getSelections()
        if selection.isEmpty()
            e.abortKeyBinding()
        else
            selection.clear()
# in "keymap.cson"
'atom-text-editor:not([mini])':
    'escape': 'custom:deselect'

Thanks again and keep the great work!
- Arthur


#11

Arthur, good to hear from you.
Thank you for the hints… you are adding to my knowledge now too.

This was a bit of fun for me just because you played along in discovering more about Atom’s inner workings.

Hope to chat again.
-Dan Padric


#12

:sunglasses: (Please try this one!)

# in "init.coffee"
atom.commands.add 'atom-text-editor', 'custom:deselect', (e) ->
    _editor = atom.workspace.getActiveTextEditor()
    for _selection in _editor.getSelections()
        _selection.clear()
    e.abortKeyBinding() # execute above & will return to do other bound task(s)

#13

The above solution works, but I added a little tweak, since when there is no open editor panel, _editor is null and an error will be raised.

# in "init.coffee"
atom.commands.add 'atom-text-editor', 'custom:deselect', (e) ->
    _editor = atom.workspace.getActiveTextEditor()
    if _editor
      for selection in _editor.getSelections()
          if selection.isEmpty()
              e.abortKeyBinding()
          else
              selection.clear()
    else
      e.abortKeyBinding()