Suppose I editing a file that ends with a ".sh" (bash)
I select a region with ESC Shift-| which leads to the minibuffer
asking me "Shell command on region:". I don't want to say "bash" all the time but instead would like "bash" to be executed without prompt for user input. Is this possible?
Thanks
Related
i have 3-4 read command in sequence and i have to press enter after the execution of every read command. I want to automate that press keyboard enter key after a read command is executed in my shell script.
You mean something like that?
yes | your_script.sh
I am using emacs to edit bash code (file ending in .sh).
In many IDE's like R-studio, I can do simply do ctrl-enter and
the current line is executed in the shell (in the R-studio case it is executed in R).
Is there a way for me to set ctrl-enter in the .sh mode that will execute the current line in xterm (within emacs)?
Thanks
… try C-c C-n, it is bound to sh-send-line-or-region by default
– jenesaisquoi
This question already has answers here:
Execute current line in Bash from Vim
(7 answers)
Closed 7 years ago.
When writing bash scripts in vim, it would be useful to be able to selectively run only a few lines from the script, e.g. if I have some script
#! /bin/bash
# more commands
mkdir /tmp/test_dir
echo "some output to STDOUT"
touch /tmp/test_dir/new_file
# more commands
say I just want to execute this part of the script
mkdir /tmp/test_dir
echo "some output to STDOUT"
touch /tmp/test_dir/new_file
how can I highlight and execute it in vim?
I have tried
v, select target text, :
which gives a prompt with :'<, '>
then tried :! w
but its not working properly.
I dont need to write the STDOUT of the shell commands to a file or anything, but I would like to be able to at least see the output.
From the answer of pacholik, you can extrapolate:
in visual mode, hit : and write w !bash
so that your whole command is :'<,'>w !bash
You will get the output as a result (but it won't change the file
If you remove the w , it will instead replace the line by the output of the buffer.
I have for example mapped r to "run command" in visual mode.
Like this:
vnoremap r :w !bash<cr>
I also have this in normal mode (to run the current line), with yr (you run)
nnoremap yr :.w !bash<cr>
You can use Quickrun plugin : http://vimawesome.com/plugin/quickrun-vim
It can run many languages on the fly (bash, python, c++...), and you can run only a selected area.
Everything you need is :
Set the filetype of your file (normally it's automatically detected, unless you created a new file, in this case just do for example : :set ft=sh for bash)
Select a part of your file with V.
Run :'<,'>QuickRun
The output opens in a new window.
You can tweak the plugin on many points, see the help.
I wrote a script that creates a backup of a text file, and a second script that verifies some syntax in text file using SED.
In the middle, there is a manual process: Users edit the original file adding some strings. This process must remain manual.
I would like to merge my two scripts so the backup is created, vi is open for the user, when the user is done editing the file, the script resumes doing the syntax verification.
I am learning by doing, but really do not know how to code the "open vi, wait for the user to do his editing, take control over and resume with verification" part.
I read there is a function called system (in Perl) that could be used, but my code is in BASH.
Any suggestions on how to get this done in BASH? Thanks!
In bash, each statement is essentially like an implicit call to system (unless it's a builtin shell command) since shell scripts are designed to make it easy to run other programs.
backup some_file.txt
vi some_file.txt # The script blocks until the user exits vi
verify_syntax some_file.txt
The only difference between using vi and a command like ls is that ls will do its thing and exit without user intervention, while vi (or any interactive command) will run until the user explicitly exits.
What I want to do is simple: add a keybinding to one of my program using readline startup file inputrc but, in addition, as my program does not produce any output, I do not want the command name to appear on stdout.
What my problem is:
.inputrc content:
"\e[1;5A":'pipe_send\n'
When I hit ctrl+uparrow, on the command line appears "pipe_send":
[ alexkag#$$$$$:: / ]
$ pipe_send
What I'd like is not having pipe_send appear on the command line, just like the commands provided by readline such as history-search-backward, history-search-forward, etc.
Do you know any way to do that? Maybe shoudn't I use readline? Note: my keybinding must only be visible in bash, not to the whole system.
As mentioned in the comments by gniourf_gniourf the solution is:
bind -x '"\e[1;5A":pipe_send'
bind -x will tell bash to execute a command whenever a certain key is pressed:
-x keyseq:shell-command
Cause shell-command to be executed whenever keyseq is entered. When shell-command is executed, the shell sets the READLINE_LINE variable to the contents of the Readline line buffer and the READLINE_POINT variable to the current location of the insertion point. If the executed command changes the value of READLINE_LINE or READLINE_POINT, those new values will be reflected in the editing state.
\e[1;5A is the terminal code sent for CtrlUp