210

When I press TAB in nano editor, the cursor will jump with 8 spaces like this:

def square(x):
        return x * x
def cube(y):
        return y * y * y

how can I set the tab stop width to 4 spaces to display like this:

def square(x):
    return x * x
def cube(y):
    return y * y * y
Chris Page
  • 18,263
  • 4
  • 39
  • 47
Fallen Satan
  • 2,105
  • 2
  • 13
  • 4
  • Nano is a pretty basic editor. There's a good chance it just doesn't support this. – millimoose Jun 23 '12 at 23:20
  • umm not only in nano, but it also indent by 8 spaces when I use python interpreter in Terminal. – Fallen Satan Jun 23 '12 at 23:25
  • 2
    This is answered on SuperUser, and should be closed as duplicate: http://superuser.com/questions/110421/tab-character-width-in-terminal – millimoose Jun 23 '12 at 23:28
  • 7
    @millimoose, changing the tab stops in the terminal doesn't affect nano. This question should be specifically about nano or about setting the tab stops for Python, but not about both and not about interactively setting the tab stops with the `tab` command, which doesn't affect all programs. – Chris Page Jun 25 '12 at 05:54

8 Answers8

338

If you use nano with a language like python (as in your example) it's also a good idea to convert tabs to spaces.

Edit your ~/.nanorc file (or create it) and add:

set tabsize 4
set tabstospaces

If you already got a file with tabs and want to convert them to spaces i recommend the expandcommand (shell):

expand -4 input.py > output.py
Sven Rojek
  • 5,476
  • 2
  • 35
  • 57
  • I'm using mint and when I set tabsize from 8 to 4 in /etc/nanorc and go back to the file, I'm still getting 8 spaces in the tab, I even tried to copy that nanorc file to ~/. but that doesn't work, closed and reopened terminal, but still I can't get 4 spaces on the tab unless I use nano -T4. Thanks – Alex May 30 '15 at 22:48
  • 1
    @Alex double check that your file starts with a dot `.nanorc` not `nanorc`. This must be placed in your users home-directory, i.e. `/home/your-username/.nanorc`. – Sven Rojek May 31 '15 at 09:24
  • 2
    Thanks, I ended up creating another .nanorc different from the one in /etc, placed it in the home dir and that worked. This are the only 3 lines I included in the new .nanorc for anyone interested: set nowrap, set tabsize 4 and set tabstospaces – Alex Jun 01 '15 at 13:19
  • 14
    If this is your toy, you can make this change available system wide, just edit the global file `/etc/nanorc`. There are a few other options there that you may enjoy. – fcm Dec 30 '15 at 12:11
  • Hi @Alexey . This Linux Shell command works for all files, but make sure to specify another output-file, otherwise your file will be emptied. – Sven Rojek Oct 09 '17 at 13:42
  • Sorry, i meant to ask if the indentation could be set exclusively for `*.py` files. – Alexey Oct 09 '17 at 13:57
  • I'm really glad you mentioned that `.nanorc` can be in the home directory instead of `/etc/...`, which doesn't seem to exist on Termux. – Brōtsyorfuzthrāx Feb 22 '21 at 19:32
  • No it's not a good idea to convert tabs to spaces in Python. Enjoy reading someone else's code indented by 1 space... or by 13 spaces... – Jeyekomon Feb 07 '22 at 11:42
  • 1
    @Jeyekomon check out the python [PEP 8 Style Guide - Tabs or Spaces](https://www.python.org/dev/peps/pep-0008/#tabs-or-spaces). A tabsize of 4 is the most common indentation for python. You should avoid ppl who are writing python code with 1 space or 13 space indentation anyway. – Sven Rojek Feb 07 '22 at 16:09
  • Don't do `expand -4 input.py > input.py` or your code will disappear. Probably some peculiarity with streams. – KTibow Aug 06 '23 at 20:51
120

Command-line flag

From man nano:

-T cols (--tabsize=cols)
    Set the size (width) of a tab to cols columns.
    The value of cols must be greater than 0. The default value is 8.
-E (--tabstospaces)
    Convert typed tabs to spaces.

For example, to set the tab size to 4, replace tabs with spaces, and edit the file "foo.txt", you would run the command:

nano -ET4 foo.txt

Config file

From man nanorc:

set tabsize n
    Use a tab size of n columns. The value of n must be greater than 0.
    The default value is 8.
set/unset tabstospaces
    Convert typed tabs to spaces.

Edit your ~/.nanorc file (create it if it does not exist), and add those commands to it. For example:

set tabsize 4
set tabstospaces

Nano will use these settings by default whenever it is launched, but command-line flags will override them.

Apples
  • 3,135
  • 1
  • 21
  • 26
14

In nano 2.2.6 the line in ~/.nanorc to do this seems to be

set tabsize 4

Setting tabspace gave me the error: 'Unknown flag "tabspace"'

Harry Detering
  • 141
  • 1
  • 2
5

Setting the tab size in nano

cd /etc
ls -a
sudo nano nanorc

enter image description here

Link: https://app.gitbook.com/@cai-dat-chrome-ubuntu-18-04/s/chuaphanloai/setting-the-tab-size-in-nano

4b0
  • 21,981
  • 30
  • 95
  • 142
4

For future viewers, there is a line in my /etc/nanorc file close to line 153 that says "set tabsize 8". The word might need to be tabsize instead of tabspace. After I replaced 8 with 4 and uncommented the line, it solved my problem.

caleb
  • 41
  • 1
4

It's easy to set tab size 4 at nano. There are some easy steps to do that:

  1. Go to the 'etc' directory(folder) from the home directory.
cd /etc
  1. There is a file name 'nanorc', need to edit it. You can see the lists of the files by running ls -a. Enter the file with your favorite editor like nano or gedit etc.
sudo nano nanorc
  1. You need to find out a line set tabsize 8.By default, the tab size is set to 8 spaces. There are around 300 lines. You can run this command to search the position of the line without scrolling. To do that exit(ctrl+Z) from nano and run
grep -n "set tabsize" nanorc

in my case, it's the 159th line. The output is:

159:# set tabsize 8
  1. Now open the 'nanorc' file again(shown in step 2). Go to the line number which was found in step 3. In nano to see line numbers in editor press alt+shift+3. You may find a line like this: # set tabsize 8

Uncomment the line(remove #) and put 4 instead of 8 and save it(ctrl+o then press enter). enter image description here

And then you are done. -- It's helpful to set tab size 4 if you use nano for python scripts.

In case it doesn't work perfectly, uncomment the next line also. In my case line 162, which is ' # set tabstospaces '

7-zete-7
  • 712
  • 4
  • 12
2

For anyone who may stumble across this old question ...

There is one thing that I think needs to be addressed.

~/.nanorc is used to apply your user specific settings to nano, so if you are editing files that require the use of sudo nano for permissions then this is not going to work.

When using sudo your custom user configuration files will not be loaded when opening a program, as you are not running the program from your account so none of your configuration changes in ~/.nanorc will be applied.

If this is the situation you find yourself in (wanting to run sudo nano and use your own config settings) then you have three options :

  • using command line flags when running sudo nano
  • editing the /root/.nanorc file
  • editing the /etc/nanorc global config file

Keep in mind that /etc/nanorc is a global configuration file and as such it affects all users, which may or may not be a problem depending on whether you have a multi-user system.

Also, user config files will override the global one, so if you were to edit /etc/nanorc and ~/.nanorc with different settings, when you run nano it will load the settings from ~/.nanorc but if you run sudo nano then it will load the settings from /etc/nanorc.

Same goes for /root/.nanorc this will override /etc/nanorc when running sudo nano

Using flags is probably the best option unless you have a lot of options.

MILO
  • 195
  • 1
  • 2
  • 11
1

For MAC Nanos, you should edit the "~/nanorc" configuration file in preference to the "~/.nanorc" configuration file.

Aksel Limmes
  • 391
  • 3
  • 4