8

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.

andrew.46
  • 39,359
t3chb0t
  • 189

5 Answers5

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.

Artur Meinild
  • 31,035
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.

rln
  • 81
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.

woodengod
  • 131
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.

4xel
  • 11