I want to create a markdown file and open it simultaneously
$ touch ~/Documents/Ubuntu/drafts.md; open ~/Document/Ubuntu/drafts.md
The solution is cumbersome, is there a shortcut to handle such a task?
I want to create a markdown file and open it simultaneously
$ touch ~/Documents/Ubuntu/drafts.md; open ~/Document/Ubuntu/drafts.md
The solution is cumbersome, is there a shortcut to handle such a task?
There's no such shortcut,Apparently, one could use Alt+. according to Sebastian Stark's answerIf your shell uses
libreadline
( which/bin/dash
on Ubuntu does not, butbash
does ), then you can use that shortcut.You can also create a file with shorter number of characters via
bash
:In this case,
:
command is built-in no-op command, which only returns exit status of 0 for success, but the trick here is that shell creates new or truncates existing file designated by${filename}
.xdg-open
opens the said filename in default editor, since it will be just a plain file.If you want to go even shorter, you can use
bash
history expansion feature to reuse command parameters:If portability is a concern, you should use
$_
for other shells. Of course, you can turn these into function or alias and call the alias or function name; call it something likemko()
for "make and open":Making a function also has advantage in adding a template to the file, if you need that:
Sidenote: in case you also need to create directories along the path, you should use
mkdir -p
, see the related question.Sidenote No.2: dessert's answer actually addresses a very fair point: most text editors do allow specifying a pathname for new file as one of their arguments. However, in cases where file has to exist - otherwise you get
No such file or directory
error - well, you can use this answer. Consider also that>
truncates an existing file, so this can be useful where you want to quickly clear the file and start adding new content.Let the editor create the file
Most (if not all) editors automatically create the file if you open them with a filename of a nonexistent one. You can save the
touch
and simply open the new file directly, e.g.:This will create the new empty file and open it. Like your approach it will fail if a directory needs to be created in the process.