Eddie
Eddie

Reputation: 899

How can I create and open a file from terminal with a single command?

To create a file from terminal I type the following...

$ touch filename.py

To open the file I just created from terminal, I then type...

$ open filename.py

I'm curious to know if there is a terminal command that does both...create and then open (I'm super lazy).

Upvotes: 44

Views: 156103

Answers (7)

Andre GolFe
Andre GolFe

Reputation: 318

you can use:

cat -> youNewFile.someExtension

Example:

cat -> myNewFile.txt

After you are done press Ctrl + d to save or Ctrl + c to abort (but in this case it's gonna save an empty file. The redirection operator ( > ) will create the file if it doesn't already exists in your folder and you will be able to edit it right a way through the terminal.

Upvotes: 0

Kipt Scriddy
Kipt Scriddy

Reputation: 763

What I do when I want to create a file, edit it and just save it is I type vim at the terminal. vim is a text editor. If you just type in vim you would see the text editor.

But if you type for instance vim example.txt you open vim and from then on you are working in the file you created. The file does not get saved until you say so. So by pressing i you enter the edit mode of vim. Allowing you to put text in the file. If you want to save just enter escape followed by :w, meaning you are saving the file with the name you have it to it, so for this example it would be example.txt. After you saved it, everything you type after pressing Esc is shown left down in the screen, simple type :q to quite it.

If you realise you do not really want to save the file you can just type :q! and if you were currently in the editing mode, meaning you were typing something, you just press Esc once followed by :q!.

So short summary:

  • vim example.txt (opens the editor if saved it will use the given name)
  • s (will enable edit mode, you can write stuff)
  • Esc (when you want to stop editing)
  • :w (save the file)
  • :q (quit the file, only usable when saved!)
  • :q! (discard the save and just exit the file)

Upvotes: 5

Julian Tellez
Julian Tellez

Reputation: 852

This is as lazy as one can get:

$ echo "your text" > myfile.txt

Upvotes: 9

brokenrhino
brokenrhino

Reputation: 160

On a Mac to create a lazytouch function to create and open a file in one line you have to edit .bashrc. You might have to first create it. Beware if you are a novice programmer. Some of these commands might require you to prepend sudo for permission to create and save. Enter these commands in terminal.

$ cd ~

$ touch .bashrc

$ open .bash_profile

Enter this profile in .bash_profile to check for .bashrc

# To get aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

Remember to save .bash_profile. Then in bash do this.

$ open .bashrc

Enter this text in the .bashrc

# .bashrc

# User specific aliases and functions

lazytouch() {
    touch $1
    open $1
}

Remember to save .bashrc

Now you can cd to any folder then create and open a file with one line.

$ lazytouch anything.really

Upvotes: 0

Boshika Tara
Boshika Tara

Reputation: 274

Simplest way to do this is

touch filename; open filename

Example

touch myfile.py; open myfile.py

Upvotes: 8

Hamzeen Hameem
Hamzeen Hameem

Reputation: 2560

you can use the following to create a file named "filename.py", insert "Hello World" into the file and then open the file,

$ echo "Hello World" > filename.py && open filename.py

Upvotes: 4

nshy
nshy

Reputation: 1074

in .bashrc

lazytouch()
{
  touch $1
  open $1
}

then type

$ lazytouch anything.really

Upvotes: 27

Related Questions