1

I take notes in various textfiles and frequently do this:

cat >> notes.txt

better not forget this

<ctrl-D>

If it is only a small addition to the file, it would be good to save the extra keystrokes, pressing enter, ctrl-D

How do you do this in one line, just adding a few characters to your text file and pressing only enter afterwards?

3 Answers 3

3

You could use a function:

addnotes () {
    echo "$*" >> /path/to/notes.txt
}

and then just add your notes as command line arguments:

$ addnotes better not forget this
1
  • Is interesting to see what power users do.. Can see if you add this to .bashrc it is always there to use
    – cardamom
    Commented Dec 12, 2020 at 19:59
2

A simple bash script (or function) that handles either a single line or multiple lines:

note() {
    # If we have text on the line use it, otherwise read from stdin
    { [[ $# -gt 0 ]] && printf "%s\n" "$*" || cat; } |

    # Write current date/time, then append collected text indented by two spaces
    { date; sed 's/^/  /'; echo; } >> "$HOME/note.txt"
}

This allows either a simple reminder

note remember this

Or a more complex paragraph

note
Remember this
and this too

oh and this
<Ctrl/D>

The reminders are written to the file $HOME/note.txt, with each one prefixed by the current date/time

cat ~/note.txt
10 Dec 2020 16:03:59
  remember this

10 Dec 2020 16:04:05
  Remember this
  and this too

  oh and this

If you don't want the date and indentation, just remove that entire | { ... } segment:

note() { { [[ $# -gt 0 ]] && printf "%s\n" "$*" || cat; } >> "$HOME/note.txt"; }

Or as a script (remember to make it executable and put it somewhere in your $PATH):

#!/bin/bash
{ [[ $# -gt 0 ]] && printf "%s\n" "$*" || cat; } |
    { date; sed 's/^/  /'; echo; } >> "$HOME/note.txt"
1

Replace cat with echo:

echo "Better not forget this" >> notes.txt
1
  • This is really the purest answer, but the others are definitely interesting..
    – cardamom
    Commented Dec 12, 2020 at 19:58

You must log in to answer this question.

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