33

Selecting lines in nano can be achieved using Esc+A. With multiple lines selected, how do I then indent all those lines at once?

52d6c6af
  • 441
  • 1
  • 4
  • 6
  • 4
    Highlighting is working now by just using `Shift` + `Arrow-Up` or `Arrow-Down` at least since Nano version: `2.9.1` – nath Jan 09 '18 at 12:08

3 Answers3

30

Once you have selected the block, you can indent it using Alt + } (not the key, but whatever key combination is necessary to produce a closing curly bracket).

Stephen Kitt
  • 411,918
  • 54
  • 1,065
  • 1,164
Chris Down
  • 122,090
  • 24
  • 265
  • 262
  • 3
    I am trying this without success on OSX Mavericks. Instead pressing alt+} results in an opening (or closing depending on whether shift is held down at the same time) quote character being inserted. – 52d6c6af Dec 22 '13 at 15:56
  • @Ben It works for me, perhaps your terminal emulator or window manager is eating the meta key? I don't know about how the OSX terminal works, though. – Chris Down Dec 22 '13 at 15:58
  • 1
    @Ben From a quick Google it seems that the Mac terminal is the culprit, see here: http://blog.remibergsma.com/2012/01/30/alt-key-aan-de-praat-in-osx-terminal/ (for future readers, the solution: Terminal > Preferences > Settings > Keyboard > Check "Use option as meta key") – Chris Down Dec 22 '13 at 16:00
  • Thanks for your help. With the setting change for terminal, it now works as you described. – 52d6c6af Dec 22 '13 at 16:03
  • @Ben You can select lines with Alt+A now. Alt+x and Esc+x are the same thing. – Kartik Dec 23 '13 at 03:50
  • @Kartik That's not always true. Escape emulation is a function of some terminal emulators, but not all. – Chris Down Dec 23 '13 at 03:51
  • 11
    ```Alt``` + ```}``` didn't work for me, could be my putty settings, however ```Alt``` + ```Shift``` + ```{``` did! – jtzero Jun 12 '14 at 14:15
  • Is there an inverse equivalent to unindent a block of lines? – David Aug 03 '15 at 00:12
  • 2
    @David `Alt + {` :-) – Chris Down Aug 03 '15 at 07:22
  • none of these worked for me, am on linux debian jessie – AAI Jan 05 '18 at 21:17
  • For some reason on my keyboard, this only works with the left Alt key but not the Alt key on the right side. ¯\\_(ツ)_/¯ – Max Starkenburg Mar 03 '20 at 01:19
  • 1
    @52d6c6af: Default assignment of the "meta" key (`M`) in `nano` or mac is the `esc` key. – deWalker May 23 '22 at 04:45
10

So the full procedure to indent a bunch of lines:

  1. Go to the first line
  2. Alt-A or Option-A or EscA to start selecting - you'll see [ Mark Set ]
  3. Go under the last line
  4. Alt-Shift-} or Option-Shift-} or Esc} to indent
  5. Alt-Shift-{ or Option-Shift-{ or Esc{ to outdent
  6. Alt-A or Option-A or EscA to unselect - you'll see [ Mark Unset ]

By the way, if you think 4 spaces are beautiful and tabs are evil:

  1. nano ~/.nanorc or sudo nano ~root/.nanorc or sudo nano /etc/.nanorc
  2. set tabsize 4
    set tabstospaces
    
  3. Of course you have to close and reopen nano for these settings to take effect.

These settings will modify the behavior of alt-shift-}/{ to indent/outdent 4 spaces. Otherwise the default behavior is to insert/remove tabs.

Bob Stein
  • 211
  • 2
  • 5
4

If you're using macOS and haven't reset your meta key in the terminal, the command for this will be Esc+}

geher
  • 141
  • 4