7

On Windows it's possible to create a *.ps1 (PowerShell) file where you can write one command per line or initialize variables etc. You then open it with VSCode and select the lines that you want to execute and press F8.

Is there something similar on Linux so that one doesn't have to type them in in the terminal everytime?

Additional clarification: I'm not looking for a way to write scripts and execute them. I know how to do this. I'm looking for a way to write some kind of a "favourite commands" file that I can open, select a line or two, press some keyboard shortcut and execute them as necessary.

New contributor
t3chb0t is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
8
  • 3
    As far as "favorite commands" go, many people (including myself) use aliases. Did you consider this? Commented Sep 4 at 7:39
  • @ArturMeinild I guess, it might (the title). However, I'm not sure what the actuall approach would be and whether it'd be a script or something completely different.
    – t3chb0t
    Commented Sep 4 at 7:39
  • @ArturMeinild it's an option (aliases), but still too much work. You have to remember all the aliases and if you've got three or four versions of the same command with different parameters or want to intialize different variables for each execution it quickly becomes not so practical.
    – t3chb0t
    Commented Sep 4 at 7:41
  • Maybe you could include a specific example of the commands you wish to run (and preferably, the 3-4 four commands that look like each other) in your question. As you say, there might be other options out there. Commented Sep 4 at 7:44
  • 1
    Along with aliases, similar features include the shell history (being able to press up-arrow a few times to return to a previous command, and then Enter to select it), shell functions (more flexible than aliases), and of course separate scripts (which are more flexible still).  A good shell will allow tab-completion on those, so you don't need to remember or re-type too much.
    – gidds
    Commented Sep 4 at 16:00

5 Answers 5

9

Using Visual Studio Code in Ubuntu as well, select the text to execute, then use the menu Terminal and click on Run selected text.

By default there isn't a shortcut for this function, but you can set one in File/Preferences/Keyboard Shortcuts.

5

I've made a small script for you that you can use.

Put a file (commands.txt) with your favorite commands in some location (/path/to/commands.txt).

Create the following shell script called favorites, and put it executable in your path:

#!/bin/bash

commandfile="/path/to/commands.txt"
totallines=$(cat "$commandfile" | wc -l)

cat -n "$commandfile"
echo ""

read -p "Which line do you want to run? " line
echo ""

if (( line <= totallines ))
then
  eval "$(sed -n "$line p" "$commandfile")"
else
  echo "Invalid number."
fi

Now, when you run favorites, it will display your favorite commands with line number, and ask which number you want to run.

When you input a line number and hit Enter, the line is run. With some additional work, this script could be made to take a comma-separated list of line numbers, and run them all.

I know this wasn't exactly what you asked, but maybe a solution you can work on nonetheless.

6
  • 2
    There is also the (kinda obscure maybe, I sometimes forget it's there) select option of Bash which could do the heavy-lifting here, something like mapfile -t commands <commands.txt; PS3="Select the command to run: "; COLUMNS=1; select command in "${commands[@]}"; do if [ -n "${command}" ]; then printf "Selected :'%s'\n" "${command}"; break; fi; done
    – kos
    Commented Sep 4 at 12:57
  • I'm sure you could improve a lot of things, since I made that in 5 minutes just as an example. 😉 Commented Sep 5 at 7:54
  • Is there any benefit in using eval there? Why not just "$(sed -n "$line p" "$commandfile")"?
    – terdon
    Commented Sep 5 at 12:53
  • @terdon I'm not sure if it makes a difference for running the command "as it's typed". You're probably right though. Commented Sep 5 at 13:16
  • 2
    @terdon With eval, the commands can have arguments, quotes, pipes, redirections, command substitutions, etc. Seems like it would be more useful.
    – JoL
    Commented Sep 6 at 9:48
3

Note: This answer depends on editor/terminal configuration, and it may not be for everyone. However, this is how I've been handling mouse input and cut/paste since SunOS 4.1, so it's typically possible to configure in most environments. Your mileage may vary, especially if you're used to Windows (which typically doesn't copy text automatically or paste on a middle-click) or Apple (which typically doesn't have a middle mouse button, meaning you must emulate it)

What I typically do is:

  • less commands.sh
  • Triple left-click on mouse the first line, holding the mouse down on the final click, dragging down to the final line.
  • You have now selected the lines; for me, selecting immediately puts them in the cut/copy buffer.
  • press q to quit less
  • middle-click to paste
  • enter to paste the text

Back in my days, we didn't have that final safety feature; it just committed the commands as soon as you pasted the text. However, I find it quite useful and tend to leave it on.

3

The Midnight Commander file manager (mc command in the terminal) has a "user menu" feature. When you press F2 it displays a menu of commands prepared in a text file. Pressing F1 when the user menu is invoked will give you more help how to configure it.

New contributor
woodengod is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • nice to see good old "mc" resurface (it is not forgotten!) Commented Sep 6 at 16:17
1

Yes, there are many many ways to do that. Roughly from most user friendly to most powerful:

  • write them in a file (.sh or whatever, it doesn't really matter), open in vscode and do the same. If there's no F8 mapping, use Ctrl+Shift+P to open the palette. Copy selection to terminal should be mostly what you want, Preferences: Open keyboard shortcuts to bind it.
  • VSCode has tasks, to emulate IDE's "press F6 to compile" and such. You do have to make the mapping yourself but you can map any and all of your "favourite commands". Or not, you don't have to use shortcuts, you can use VSCode's terminal menu and the mouse to select and run your tasks.
  • Copy paste in the shell. Because Ctrl-c is an interruption signal, the usual shortcut to copy from terminal to the system clipboard is Ctrl+Shift+c (and either or both Ctrl+v and Ctrl+Shift+v to paste). But there's also Ctrl+k, Ctrl+u and Ctrl+y (kill buffer, distinct from clipboard), and middle click (paste last selection).
  • Bash history. For one-liners, shell history completion is the idomatic way. Bash has some amazing features if you know how to dig them out. If you don't like to dig jump to the next point. One of those feature is history search. Instead of using arrows or history or history | grep to painfully retrieve your command back, press Ctrl+r, then start entering your command. The last command prefixed by your input appears. Press Ctrl+r again to go further back in history, and Ctrl+s to go forward. Press enter to run the command, or arrow keys (left|right) to select it, giving you a chance to edit. (up|down) arrow keys also work: you are actually navigating the history at the position of the matched command; for example, you can press 'up' to view the command you did just before your last git push origin master --force.
  • Install and use any shell other than bash. Really, any modern shell has essentially all the powers I've outlined above, but are intuitive to use before learning shortcuts. And then they do have hidden super powers on top of that (eg. fish pretty much does Ctrl+r without having to press Ctrl+r, but you can also press Ctrl+r and navigate a visual menu). Completion and history completion is only one of many areas in which they are superior to bash. Options include: fish, Nushell, zsh, Ion, csh... As for powerful and related shell features other than completion and history, there are also aliases, functions, abbreviation, and well, scripts.
  • write each scripts in different files. That one is for scripts that you'd rather not retype if you lost them in history, and is for when|if you get comfortable using a terminal. I know this is exactly what you exclude in your clarification, but once you do get familiar with your terminal and shell, it's often simpler to just skip the part where you open a file.
  • Use a power user text editor, such as kakoune(the least user unfriendly I know of), Neovim or Emacs. They all have shortcuts to execute arbitrary shell command including depending on selection.

To sum up, you can pretty much do exactly what you're already doing on Windows with a bit of VScode configuration. If you are to stay some for some time on linux, I strongly urge you to get a bit familiar with your shell, you'll find that just command completion and fast history search (Ctrl+r) alleviates a lot of the need to keep one liners around.

New contributor
4xel is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .