5 ms·
I mostly use the terminal, but I usually follow the same pattern of first creating a blank file then opening the file in an editing program. $ touch myfile.txt
by fourseventy 5y ago
I mostly use the terminal, but I usually follow the same pattern of first creating a blank file then opening the file in an editing program.
$ touch myfile.txt
$ vim myfile.txt
- gruez 5y agoBut it's not necessary right? If you omit the first step, vim will still open, and the file will get created when you first save it.
- baud147258 5y agoMaybe it takes into account if the user doesn't have write access to the folder? If the user didn't have access, the touch command would fail, whereas going directly to vim, it would only fail when trying to save (potentially losing the work done in the file)?
- lupire 5y agovim will make a swap file for you and will stop you from quitting on an unsaved file.
- tshaddox 5y agoI don't use vim, but can't you just skip the first command? I would guess that the second command would work just fine. Presumably the only difference would be that the file wouldn't be created until you first save the file within vim.
- JadeNB 5y ago> I don't use vim, but can't you just skip the first command? I would guess that the second command would work just fine. You are correct.
- version_five 5y ago> Presumably the only difference would be that the file wouldn't be created until you first save the file within vim. And for me anyway, this is a benefit. When I used windows (and the same holds for google docs) I always had "New Folder"s everywhere that I'd created for whatever reason and then changed my mind or got distracted. Same with new documents. In vim, I do like to say, and it I change my mind and don't save it, I never see it again. If I touch'd it first, inevitably I'd have empty files everywhere.
- EarthLaunch 5y agoWhy I do 'touch' first: I'm in vim creating the file, then I try to save and discover I don't have write permissions [1]. With a touch first, saving the file always works. 1: https://www.cyberciti.biz/faq/vim-vi-text-editor-save-file-without-root-permission/ https://www.cyberciti.biz/faq/vim-vi-text-editor-save-file-w...
- koyote 5y agoThat's a good tip! Sadly it does not save me from going and editing a system file as non-root and then trying to save... :(
- michaelmrose 5y agoYou could have a function that writes contents of buffer to current path as root in your editor of choice.
- rootusrootus 5y agoI've done a similar thing, but not with touch. I open the file in vim and then save it immediately. Keeps me from being sad if permissions are going to stop me after I've put some work creating the file content.
- seanwilson 5y ago> $ touch myfile.txt If we're playing golf, this works (overwriting if the file exists): >myfile.txt
- jeramey 5y agoDepends a bit on your shell. That works great in bash. Other shells have different semantics. For example, zsh will wait for input until you type ^D or ^C and tcsh will complain about an invalid null command. The variant I know that works identically in pretty much every shell I've ever tried is: :>myfile.txt And if you don't want to overwrite the file if it exists: :>>myfile.txt
- ximm 5y agoWhat I find interesting about `vim /path/to/file` is that it is neither document-centric nor application-centric. Or in a way it is both: You need to know which application you want to use, but you select the location of the document before launching the application. I wonder why this feels so natural in the terminal but so annoying in GUIs. I cannot remember any specific examples, but I think there are some GUI applications that require you to select a location before you start editing a new document (maybe some audio workstations?). I remember feeling annoyed by that. Then again, the difference between "document-centric" and "application-centric" doesn't make a lot of sense in the context of the command line because 90% of files you work with are text files anyway. There is much less coupling between document types and applications than in the GUI world.