Executing Shell command on current line without selection - bash

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

Related

A shortcut to fire up vim on the text typed on the command line

I typed a very long command on the command line – only to discover that Bash doesn't like embedded single quotes. It is a bare tty and my only way to save this command for running from within a file is to open vim and start typing this very long command again...
Isn't there a shortcut to fire up vim on the text typed on the command line – directly from the command line?
Sure there is. The following command will edit the current command-line in $VISUAL:
C-x C-e
I found the answer for my case here:
First off a warning from #marlar (I made myself a function to protect myself
from this, see below):
Be very careful with this feature. If you cancel the edit, the original
command line will be immediately executed. So if you are editing rm -rf
<forward slash> and invoke the editor and realize you are into something
dangerous and thus cancel the edit, your root file system will be deleted
without further questions asked.
From the OP #Kartik:
The bash man page says:
edit-and-execute-command (C-xC-e)
Invoke an editor on the current command line, and execute the
result as shell commands. Bash attempts to invoke $VISUAL,
$EDITOR, and emacs as the editor, in that order.
And from #Mark E. Haase
If you use Bash's vi mode, the short cut is Esc, V...
I actually also find that doing just v is sufficient to launch vim with the bash
command sitting there.
From #karlicoss:
if your editor is vim, you can use :cq, it makes it exit with non-zero code,
and prevents the command from execution
Because I'm paranoid about the rm -rf command (I once deleted 2Tb of brain
data with that - 10 days into switching to linux... lucky the sysadmin had a
backup... actual quote: "I've never seen anything that stupid in my 20 years
doing this job"... but I digress) I put the following in my vimrc to
immediately comment out the command if I accidentally bring up vim and
reflexively exit:
function! CheckBashEdit()
" if the file matches this highly specific reg exp, comment the line
"(e.g. a file that looks like: /tmp/bash-fc.xxxxxx)
if match(#%, "\/tmp\/bash-fc\.......") != -1
" comment out the command
silent! execute ":%normal! I# "
write
endif
endfunction
" if we ended up in vim by pressing <ESC-v>, put a # at the beggining of
" the line to prevent accidental execution (since bash will execute no
" matter what! Imagine rm -rf <forward slash> was there...)
autocmd BufReadPost * :call CheckBashEdit()

Jump to string in current line while using Bash in VI mode

While navigating in BASH using VI mode, I can jump back to a specific character (e.g. '-') of the current command line via the following command:
F-
How can I jump back to a specific string (e.g. '--path') in the current command line of BASH? I know navigating in VI but I did not understand how to perform regex search in current command line of BASH.
According to here, what you want doesn't seem possible. The ?word and /word bindings search in command history rather than in the current command line.
But in vi mode, you can press ESC v to open the current command line in an editor. Then you can edit & save the command and it will be executed (source).
Of course, as pointed out in nur-sh's answer, you can simply keep pressing B to get to the word.
You could use the find command which searches backwards from where you are
?word
or you could keep pressing B to get to the word
(this command goes back one Word at a time).

How can I visually select, then execute shell commands directly in vim? [duplicate]

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.

Bash and readline: how to bind key to a "silent command" of my own in bash?

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

Running shell commands in GVim without echoing the Vim command

When I use :! to run shell commands, like:
!echo hi
It prints both the VimScript command and it's output, so I get:
:!echo hi
hi
This is OK when I do it in command line mode, but when I run it via a .vim file I don't want to see it - I just want to see the result of the command.
Is there a way to disable the echoing of the VimScript command?
I know I can use
echo system('echo hi')
But that would prevent me from using it with interactive shell programs...
BTW, I'm using Linux - in windows this is not really a problem since shell commands run on a new console window anyways...
edit:
This is my small working example:
function! RunShellTask(cmd)
execute '!'.a:cmd
return v:shell_error
endfunction
call RunShellTask('echo hi')
I run it with :source %
You could try the :redir command:
*:redi* *:redir*
:redi[r][!] > {file} Redirect messages to file {file}. The messages which
are the output of commands are written to that file,
until redirection ends. The messages are also still
shown on the screen. When [!] is included, an
:
:
To stop the messages and commands from being echoed to
the screen, put the commands in a function and call it
with ":silent call Function()".
An alternative is to use the 'verbosefile' option,
this can be used in combination with ":redir".
I haven't tested, but you could try :redir #b, execute the shell commands from a function called with :silent call, read the output (from register b), filter out the vimscript commands, display it on the screen and then :redir end.
Another option is to try some plugins that provide similar functionality:
shellasync.vim : shellasync.vim plugin for asynchronously executing shell commands in vim
Conque Shell : Run interactive commands inside a Vim buffer
Screen (vim + gnu screen/tmux) : Simulate a split shell, using gnu screen or tmux, that you can send commands to.
Vicle : Vim - Interpreter Command Line Editor. Like Chimp or Slimv.

Resources