1

I want all users of nano to have tabsize 4 instead of the default 8. What is the best way to achieve this? I would prefer a file that overrides /etc/nanorc at the system level so I don't have to maintain separate user nanorc's for this purpose. In the simple case, my override would only need to contain:

set tabsize 4

Here's another way to state my question: Does nano recognize /etc/nanorc.d/ and config files placed therein? If so, what is the required naming and/ content of config files placed there?

What I tried so far was to create /etc/nanorc.d/ and place a file named tabsize.conf in that directory and put only the following contents in the file:

set tabsize 4

My naive attempt did not work, but I am hoping there is a way to use this config.d/ pattern with nano.

I will make my question even more specific. I am using Arch Linux. I have do do these steps when the package has a new nanorc:

mv /etc/nanorc.pacnew /etc/nanorc

Then edit /etc/nanorc, search for tabsize, uncomment the line, change the value from 8 to 4 and save the file.

My goal is to only have to do this step:

mv /etc/nanorc.pacnew /etc/nanorc

And to have a file similar to /etc/nanorc.d/tabsize.conf that contains my desired tab size. It's a small savings of time, but multiplied across a number of computers it adds up. This year it seems like I have gotten new /etc/nanorc.pacnew files about six times. It is very inefficient to keep editing tabsize over and over.

13
  • 1
    Isn't a solution modifying /etc/nanorc? Sep 27, 2018 at 0:51
  • 1
    @IporSircer no, because /etc/nanorc gets over-written on package updates and I am constantly having to edit it to once again set tabsize 4. I'm trying to avoid that.
    – MountainX
    Sep 27, 2018 at 1:49
  • Any custom settings to files in /etc won't be overwritten on upgrades by default, unless you have force it with switches. This is how every distribution works. Sep 27, 2018 at 1:51
  • @IporSircer of course. Sorry I wasn't clear. It's the manual work I am trying to avoid. I wish to be able to simply use the packager's version of the updated nanorc without having to repeat my customization every time. It's a common pattern for many packages, and I am asking if there is a way to achieve the same thing with nano.
    – MountainX
    Sep 27, 2018 at 2:46
  • 1
    Again: this is how a distribution works. Configuration of Apache, Proftpd, Squid, Mysql and any other programs store their huge config files in /etc, and sysadmins do upgrades without problems. In Debian you can set triggers which are activated after a specific package was installed. You can make a custom trigger to append one line to /etc/nanorc on every updates. This is the clean way. Sep 27, 2018 at 2:50

3 Answers 3

3

So /etc/nanorc.pacnew is the new rc file that came with the new distribution upgrade? How about

sed '/tabsize/ {s/^# *//; s/[0-9]*$/4/}' /etc/nanorc.pacnew > /etc/nanorc 

, then?

Another possible trick might be to have a symbolic link ~/.nanorc in every user's home dir pointing to a central file with the relevant commands.

on demand:

sed '/tabsize/              # if the line matches "tabsize"
{s/^# *//;                  # remove "#" and trailing spaces from begin-of-line (BOL)
s/[0-9]*$/4/                # substitute any sequence of digits at EOL by "4"
}' /etc/nanorc.pacnew       # input file
> /etc/nanorc               # redirection to target file
3
  • I like both suggestions. I'll probably use sed. Can you walk me through the sed command? Also, the .pacnew (the file that came with the new package upgrade) contains this line: #set tabsize 8 with an initial comment character and possibly variable white space. It needs to be transformed into set tabsize 4 without the comment.
    – MountainX
    Sep 27, 2018 at 10:04
  • UPDATE: I tested your sed solution and it works perfectly. Thanks. Just wish I understood it better.
    – MountainX
    Sep 27, 2018 at 10:08
  • added explanation.
    – RudiC
    Sep 27, 2018 at 14:56
1

To add to RudiC's answer, the link trick works! This is good for global overrides that you don't need to fix after e.g. a do-release-upgrade on Ubuntu (which may update the /etc/nanorc file).

Here's what I did.
Let's say the shared nanorc override file is in /home-shared/.config/nano/nanorc
Then just link it for each user (su $USER):

mkdir -p ~/.config/nano &&
ln -s /home-shared/.config/nano/nanorc ~/.config/nano/nanorc

You can do the same for root user as well, so it also applies when using sudo nano ...:

sudo mkdir -p /root/.config/nano &&
sudo ln -s /home-shared/.config/nano/nanorc /root/.config/nano/nanorc

In this case, it's probably best to make sure the linked file is only writable by root:

sudo chown root:root /home-shared/.config/nano/nanorc &&
sudo chmod 644 /home-shared/.config/nano/nanorc # 0644/-rw-r--r--

An example of a nanorc file which I like, to make it more like GUI editors:

## Forget the used search/replace strings on the next session.
unset historylog
## Display line numbers to the left (and any anchors in the margin).
set linenumbers
## Disable vim-style lock-files.
unset locking
## Make the Home key smarter, toggling line-start / first non-whitespace position
set smarthome
## Spread overlong lines over multiple screen lines.
set softwrap
## Disallow nano to be suspended (with ^Z by default).
unset suspendable
## Use this tab size instead of the default; it must be greater than 0.
set tabsize 2
## Make nano have keybindings that are more "usual"
bind ^Q exit all
bind ^S savefile main
bind ^W writeout main
bind ^O insert main
bind ^H help all
bind ^H exit help
bind ^F whereis all
bind ^G findnext all
bind ^R replace main
bind ^X cut all
bind ^C copy main
bind ^V paste all
bind ^A mark main
bind ^Z undo main
bind ^Y redo main
0

The Nano documentation for Nano 6.4 says that nano --rcfile=/pathtonanorcfile makes it so it doesn't load other nanorc files.

"During startup, if −−rcfile is not given, nano will read two files: first the system-wide settings, from /etc/nanorc (the exact path might be different on your system), and then the user-specific settings, either from ~/.nanorc or from $XDG_CONFIG_HOME/nano/nanorc or from ~/.config/nano/nanorc, whichever is encountered first. If −−rcfile is given, nano will read just the specified settings file."

To disable nanorc file reading altogether, do nano -I.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .