Mikey
Mikey

Reputation: 3017

Move selected text to the left or right in Visual Studio Code

In Visual Studio Code, is there any command currently to move the selected text to the left or right?

I'm not talking about indentation btw.

Upvotes: 40

Views: 89777

Answers (6)

Nitin Garad
Nitin Garad

Reputation: 21

I have add this feature in my vs code using following steps.

Usage : By using this key bind feature we can move selected text move left/right using keyboard shortcut.

Move selection texts

Steps to implement this -->

Open VS code.

Press ctrl + p

Press > and search --> Open keyboard shortcuts (JSON)

Filename : keybindings.json

Add this following code as it is.

Restart your vs code once.

{
    "key": "alt+shift+left",
    "command": "editor.action.moveCarretLeftAction",
    "when": "editorTextFocus && !editorReadonly"
},
{
    "key": "alt+shift+right",
    "command": "editor.action.moveCarretRightAction",
    "when": "editorTextFocus && !editorReadonly"
},

Upvotes: 2

R. Gurung
R. Gurung

Reputation: 1506

Ok, so these answers didn't really help me as I am a beginner, here is what I did.

  1. Goto File->Preferences->Keyboard Shortcuts.
  2. Search Move Carret Left and Move Carret Right and add your desired keys combinations. (2022 update: If searching these terms don't work search the term carret.)
  3. You are done, now test it by selecting a test and firing the key combinations.

Upvotes: 15

Dalton
Dalton

Reputation: 447

Thanks to suggestions here I created custom key binding (ctrl+shift+p -> shortcut json) accordingly:

    {
        "key": "alt+left",
        "command": "editor.action.moveCarretLeftAction",
        "when": "editorFocus && editorHasSelection || editorHasMultipleSelections "
    },
    {
        "key": "alt+right",
        "command": "editor.action.moveCarretRightAction",
        "when": "editorFocus && editorHasSelection || editorHasMultipleSelections"
    }

This "when" functionality is nice, ref here

Hmm, strange that "when": "editorFocus && (editorHasSelection || editorHasMultipleSelections)" doesn't work.. I forgot to write ending ")" and it worked, added it while writing this answer and it stopped functioning.. hm, maybe somebody can point out what is wrong.

Upvotes: 3

Sanju Bhaskar K S
Sanju Bhaskar K S

Reputation: 61

Add/edit keyboard shortcut:

enter image description here

  1. Keyboard shortcuts settings - ctrl+k, ctrl+s
  2. Search "Move Selected Text Left" or "Move Selected Text Right"
  3. Double click - to add/edit "Key binding"
  4. ctrl+k, ctrl+E - to add/edit "When" condition

Upvotes: 5

Stefano Bossi
Stefano Bossi

Reputation: 1437

Moving blocks of text

A workaround for moving a block of text of just one space is to use the "column selection" feature of Visual Studio. here the detailed instruction:

  • move the cursor at the upper left corner of the block of text you want to shift;
  • click with the mouse holding down shift + alt, on the down left corner of the block of text you want to shift;
  • now you could insert as many spaces as you want.

If you need more info on Block Selection in Visual Studio you could easily find information on the official documentation or here: enter link description here Pay attention in the link the shortcut is wrong, at least for me.


Indentation

For moving the text right and left you can use TAB key:

  • TAB move the selected text right
  • SHIFT + TAB move the selected text left

Upvotes: 65

apk
apk

Reputation: 1681

This feature has been implemented by a pull request some time ago.

To use it you need to bind the editor.action.moveCarretLeftAction and editor.action.moveCarretRightAction actions in the keyboard shortcuts editor.

Animation showing how the solution works

Upvotes: 69

Related Questions