1

How to input text into a new text file using nano from command line?

I would like the same as with the following, but using nano:

echo 'Hello, world.' >foo.txt

Result:

  1. nano is not capable of handling non-interactive text input.
  2. echo is available in every Linux/Unix system, while nano is not installed by default in every Linux/Unix system. Echo can be also used in shell scripts, too.

Conclusion: The most compatible solution is to use

echo 'Hello, world.' >foo.txt

as solution to create a file and fill with input text non-interactively.

3
  • nano is text editor, and you insert text by typing it in. Why not just take extra step and do something like echo "Hello World" > foo.txt && nano foo.txt. This can even be simplified to an alias. I personally don't see the point of it ,though. Jan 12, 2017 at 10:04
  • @Serg: Yes, I think you are right, I have to stick with echo "Hello World" > foo.txt kind of solution.
    – klor
    Jan 12, 2017 at 17:52
  • Do not put answers in questions. That's not how this is supposed to work.
    – JdeBP
    Jan 13, 2017 at 0:17

3 Answers 3

1

You can use a here document but with this way it is not possible to provide a special output document.

$ cat | nano <<-EOF
one
two
three

EOF

Received SIGHUP or SIGTERM

Buffer written to nano.save

This behaviour is mentioned in the man page under notes

In some cases nano will try to dump the buffer into an emergency file. This will happen mainly if nano receives a SIGHUP or SIGTERM or runs out of memory. It will write the buffer into a file named nano.save if the buffer didn't have a name already, or will add a ".save" suffix to the current filename. If an emergency file with that name already exists in the current directory, it will add ".save" plus a number (e.g. ".save.1") to the current filename in order to make it unique. In multibuffer mode, nano will write all the open buffers to their respective emergency files.

So i think nano is not the best choice for non interactive texting. If you only want to input multi line text to a file you can also use a here document as well without nano.

cat > foo.txt <<-EOF
> one
> two
> three
> 
> EOF
cme@itp-nb-1-prod-01 ~ $ cat foo.txt 
one
two
three

Maybe this is what you need.

3
  • Your solution was the closest one to what I wanted to do. Unfortunately it seems nano doesn't have capability to input text & create file non-interactively. I have to stick with echo "Hello World" > foo.txt kind of solution.
    – klor
    Jan 12, 2017 at 17:55
  • Although not a real solution, but you pointed out, that non interactive text input into nano is not possible, and gave proof about is. Accepting your answer as solution. Sticking with the usual echo "Hello World" > foo.txt kind solution.
    – klor
    Jan 12, 2017 at 18:17
  • @klor what is you intention to use nano i such a case? I don't understand you needs for this. Jan 14, 2017 at 14:20
0
  1. Open nano:

nano

  1. Write your text: "Hello, world"

  2. Press Ctrl+O (Save file)

  3. It will ask for File Name to Write, introduce it.

  4. Exit with Ctrl+X

1
  • Sorry, but your solution is not done completely from command line. Thus I can not use is a shell script.
    – klor
    Jan 12, 2017 at 17:46
0
nano foo.txt

Press Hello, world.^O^X, where ^ is ctrl button. ^O is Write, ^X is Exit.

3
  • Sorry, but your solution is not done completely from command line. Thus I can not use is a shell script.
    – klor
    Jan 12, 2017 at 17:46
  • Why do you want use nano in shell script? There are better ways in many cases, I think.
    – ValeriyKr
    Jan 12, 2017 at 17:58
  • I will not use nano for this goal, as it seems doesn't have the built possibility to input non-interactive text.
    – klor
    Jan 12, 2017 at 18:18

You must log in to answer this question.

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