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

后端 未结 8 1289
野性不改
野性不改 2020-12-24 06:56

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...

相关标签:
8条回答
  • 2020-12-24 07:14

    in .bashrc

    lazytouch()
    {
      touch $1
      open $1
    }
    

    then type

    $ lazytouch anything.really
    
    0 讨论(0)
  • 2020-12-24 07:18

    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)
    0 讨论(0)
  • 2020-12-24 07:21

    To create a file from terminal I type the following... $ touch filename.py but not able to create the file

    To open the file I just created from terminal, I then type... $ open filename.py but not able to open the file

    0 讨论(0)
  • 2020-12-24 07:28

    Simplest way to do this is

    touch filename; open filename
    

    Example

    touch myfile.py; open myfile.py
    
    0 讨论(0)
  • 2020-12-24 07:31

    This is as lazy as one can get:

    $ echo "your text" > myfile.txt
    
    0 讨论(0)
  • 2020-12-24 07:32

    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
    
    0 讨论(0)
提交回复
热议问题