Quick method to create a file and open it
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?
command-line shortcut-keys files
add a comment |
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?
command-line shortcut-keys files
add a comment |
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?
command-line shortcut-keys files
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?
command-line shortcut-keys files
command-line shortcut-keys files
edited Jan 12 at 8:03
dessert
22.6k56398
22.6k56398
asked Jan 12 at 1:03
AliceAlice
400110
400110
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
There's no such shortcut, Apparently, one could use Alt+. according to Sebastian Stark's answer
touch ${filename}
xdg-open <alt>+<.>
If your shell uses libreadline ( which /bin/dash on Ubuntu does not, but bash does ), then you can use that shortcut.
You can also create a file with shorter number of characters via bash:
: > ${filename}; xdg-open ${filename}
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:
: > myfile.txt ; xdg-open !#:1
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 like mko() for "make and open":
# function definition to put in ~/.bashrc
mko(){ : > "$1"; xdg-open !#:1 }
# call it as so:
mko ~/Documents/myfile.md
Making a function also has advantage in adding a template to the file, if you need that:
mko(){
cat > "$1" <<EOF
### HEADING1
- bullet 1
- bullet 2
EOF
xdg-open "$1"
}
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.
add a comment |
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.:
nano ~/Document/Ubuntu/drafts.md
vim ~/Document/Ubuntu/drafts.md
leafpad ~/Document/Ubuntu/drafts.md
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.
That's what I'd do, might want to do something likenano $(mktemp -f XXXXXX.txt)to make a random file (or usedateto ensure no collisions). Set the one-liner as analiasin your~/.bashrcfile, AND/OR set a hotkey to the alias/oneliner in your desktop manager.
– pbhj
Jan 12 at 15:13
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1109023%2fquick-method-to-create-a-file-and-open-it%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
There's no such shortcut, Apparently, one could use Alt+. according to Sebastian Stark's answer
touch ${filename}
xdg-open <alt>+<.>
If your shell uses libreadline ( which /bin/dash on Ubuntu does not, but bash does ), then you can use that shortcut.
You can also create a file with shorter number of characters via bash:
: > ${filename}; xdg-open ${filename}
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:
: > myfile.txt ; xdg-open !#:1
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 like mko() for "make and open":
# function definition to put in ~/.bashrc
mko(){ : > "$1"; xdg-open !#:1 }
# call it as so:
mko ~/Documents/myfile.md
Making a function also has advantage in adding a template to the file, if you need that:
mko(){
cat > "$1" <<EOF
### HEADING1
- bullet 1
- bullet 2
EOF
xdg-open "$1"
}
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.
add a comment |
There's no such shortcut, Apparently, one could use Alt+. according to Sebastian Stark's answer
touch ${filename}
xdg-open <alt>+<.>
If your shell uses libreadline ( which /bin/dash on Ubuntu does not, but bash does ), then you can use that shortcut.
You can also create a file with shorter number of characters via bash:
: > ${filename}; xdg-open ${filename}
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:
: > myfile.txt ; xdg-open !#:1
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 like mko() for "make and open":
# function definition to put in ~/.bashrc
mko(){ : > "$1"; xdg-open !#:1 }
# call it as so:
mko ~/Documents/myfile.md
Making a function also has advantage in adding a template to the file, if you need that:
mko(){
cat > "$1" <<EOF
### HEADING1
- bullet 1
- bullet 2
EOF
xdg-open "$1"
}
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.
add a comment |
There's no such shortcut, Apparently, one could use Alt+. according to Sebastian Stark's answer
touch ${filename}
xdg-open <alt>+<.>
If your shell uses libreadline ( which /bin/dash on Ubuntu does not, but bash does ), then you can use that shortcut.
You can also create a file with shorter number of characters via bash:
: > ${filename}; xdg-open ${filename}
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:
: > myfile.txt ; xdg-open !#:1
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 like mko() for "make and open":
# function definition to put in ~/.bashrc
mko(){ : > "$1"; xdg-open !#:1 }
# call it as so:
mko ~/Documents/myfile.md
Making a function also has advantage in adding a template to the file, if you need that:
mko(){
cat > "$1" <<EOF
### HEADING1
- bullet 1
- bullet 2
EOF
xdg-open "$1"
}
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.
There's no such shortcut, Apparently, one could use Alt+. according to Sebastian Stark's answer
touch ${filename}
xdg-open <alt>+<.>
If your shell uses libreadline ( which /bin/dash on Ubuntu does not, but bash does ), then you can use that shortcut.
You can also create a file with shorter number of characters via bash:
: > ${filename}; xdg-open ${filename}
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:
: > myfile.txt ; xdg-open !#:1
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 like mko() for "make and open":
# function definition to put in ~/.bashrc
mko(){ : > "$1"; xdg-open !#:1 }
# call it as so:
mko ~/Documents/myfile.md
Making a function also has advantage in adding a template to the file, if you need that:
mko(){
cat > "$1" <<EOF
### HEADING1
- bullet 1
- bullet 2
EOF
xdg-open "$1"
}
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.
edited Jan 12 at 9:43
answered Jan 12 at 1:06
Sergiy KolodyazhnyySergiy Kolodyazhnyy
71.5k9147313
71.5k9147313
add a comment |
add a comment |
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.:
nano ~/Document/Ubuntu/drafts.md
vim ~/Document/Ubuntu/drafts.md
leafpad ~/Document/Ubuntu/drafts.md
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.
That's what I'd do, might want to do something likenano $(mktemp -f XXXXXX.txt)to make a random file (or usedateto ensure no collisions). Set the one-liner as analiasin your~/.bashrcfile, AND/OR set a hotkey to the alias/oneliner in your desktop manager.
– pbhj
Jan 12 at 15:13
add a comment |
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.:
nano ~/Document/Ubuntu/drafts.md
vim ~/Document/Ubuntu/drafts.md
leafpad ~/Document/Ubuntu/drafts.md
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.
That's what I'd do, might want to do something likenano $(mktemp -f XXXXXX.txt)to make a random file (or usedateto ensure no collisions). Set the one-liner as analiasin your~/.bashrcfile, AND/OR set a hotkey to the alias/oneliner in your desktop manager.
– pbhj
Jan 12 at 15:13
add a comment |
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.:
nano ~/Document/Ubuntu/drafts.md
vim ~/Document/Ubuntu/drafts.md
leafpad ~/Document/Ubuntu/drafts.md
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.
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.:
nano ~/Document/Ubuntu/drafts.md
vim ~/Document/Ubuntu/drafts.md
leafpad ~/Document/Ubuntu/drafts.md
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.
answered Jan 12 at 8:55
dessertdessert
22.6k56398
22.6k56398
That's what I'd do, might want to do something likenano $(mktemp -f XXXXXX.txt)to make a random file (or usedateto ensure no collisions). Set the one-liner as analiasin your~/.bashrcfile, AND/OR set a hotkey to the alias/oneliner in your desktop manager.
– pbhj
Jan 12 at 15:13
add a comment |
That's what I'd do, might want to do something likenano $(mktemp -f XXXXXX.txt)to make a random file (or usedateto ensure no collisions). Set the one-liner as analiasin your~/.bashrcfile, AND/OR set a hotkey to the alias/oneliner in your desktop manager.
– pbhj
Jan 12 at 15:13
That's what I'd do, might want to do something like
nano $(mktemp -f XXXXXX.txt) to make a random file (or use date to ensure no collisions). Set the one-liner as an alias in your ~/.bashrc file, AND/OR set a hotkey to the alias/oneliner in your desktop manager.– pbhj
Jan 12 at 15:13
That's what I'd do, might want to do something like
nano $(mktemp -f XXXXXX.txt) to make a random file (or use date to ensure no collisions). Set the one-liner as an alias in your ~/.bashrc file, AND/OR set a hotkey to the alias/oneliner in your desktop manager.– pbhj
Jan 12 at 15:13
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1109023%2fquick-method-to-create-a-file-and-open-it%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown