Is there an editor that allows to evaluate the current line as a bash command? - linux

I'd like to evaluate a bash script line by line. I also might want to jump back and execute a previous line again.
As described in How execute bash script line by line? , one could use the built-in debugging option -x, but this is not very handy, since you don't have the overview of previous and future commands.
For writing software in R, I used RStudio. The editor allows to evaluate the current line as an R-Command by hitting Ctrl+Enter. Afterwards the result is shown in a built-in shell, and the Cursor jumps to the next command.
Is there a simple text-editor (like gedit) that allows to send the current line to a built-in shell/console (bash, zsh,...) and view the result of the evlauation afterwards in the shell?

It's not built in to Emacs, but it's easy to do.
(defun shell-eval-line (pos)
"Evaluate the line around position as a shell command.
In interactive mode, use the cursor's position."
(interactive "d")
(save-excursion
(goto-char pos)
(shell-command (buffer-substring
(line-beginning-position) (line-end-position))) ))
Bind to a key of your liking (C-c ! maybe?) and go.
Add a (next-line) outside the (save-excursion) to make it advance to the next line when it's done, or create a simple macro around it to invoke the function and jump to the next line.

You can also do it with the editor geany. In their Wiki they have a detailed instruction. In short:
Install geany
Open the file ~/.config/geany/geany.conf and set send_selection_unsafe=true
Restart geany
Set a key-binding Edit > Preferences > Keybindings (It is under Format / Send selection to terminal)
Actually you don't have to select the code you want to send. So far I couldn't find out how to instruct geany to jump to the next line afterwards.

Related

Ctrl Right and Ctrl Left doesn't move from a word to another in Fish Shell

When I use my Fish Shell on Linux Mint, using the Ctrl+Left or Ctrl+Right keys isn't moving the cursor to the previous or next word. It switches between an I and an N instead:
Here is the I and then the N:
I cannot do partial completion then, so it's really boring.
How can I fix this?
Glenn Jackman's comment is correct - you are using vi-mode.
Some third-party prompts (e.g. from Oh-My-Fish or similar) enable it for some reason.
To switch back, usually executing fish_default_key_bindings once interactively should suffice once you have deleted the offending line or package (search for fish_vi_key_bindings).
Or, if you like vi-mode, you can add a binding. Create a function called fish_user_key_bindings (e.g. with funced).
The content should look like this
function fish_user_key_bindings
bind -M $mode $sequence $command
end
where "$command" here would be "backward-word". $mode would be the vi-mode you want the binding to be valid for, e.g. "insert" or "default" (what vi would call "normal" mode).
"$sequence" would be the text sequence that the terminal sends to fish whenever this key combination is pressed. Unfortunately they aren't standardized, so you need to figure out which it is on your system.
fish_key_reader is useful here - execute it, press the combination and use what it tells you. On my terminal ctrl+left sends \e\[1\;5D (and ctrl+right sends the same with C instead of D).

History/partial search with up-arrow on scala shell

How can I make the scala interactive shell program behave this way (using the up-arrow to go to the last command if I haven't typed anything or go to the last command given in the shell beginning with what I typed) instead of the reverse-i-search?
The Scala REPL uses JLine for readline functionality, and current versions support these .inputrc mappings:
## arrow up
"\e[A":history-search-backward
## arrow down
"\e[B":history-search-forward
and they work as you describe, enter a line prefix then up-arrow.
Probably the clunky :h? command can be retired. Or I guess that finds text anywhere on a line. :history line numbers are still useful for :edit 2463+10.

Cycle Through Syntax Coloring in Vim

I want to write a syntax coloring script for a programming language I'm writing a compiler for, but to minimize the work required (as I barely know vimscript) I would like to find a coloring script that produces colors that are close enough to what I want to be able to just edit that script.
What is the best way to have Vim show me a specific code file and then apply every single different syntax coloring script it knows, so that I can determine which is closest? So far I've been using
:set syntax=<the next syntax script>
but is there anything easier, or less repetitive?
You can cycle through all your syntax scripts by applying the following steps:
1: Find all your installed syntax scripts and store them in a variable
:redir #a
:echo glob($VIMRUNTIME . '/syntax/*.vim')
" Hit G
" Hit Enter
:redir END
2: Open a new buffer in vim and paste your scripts from the register 'a' by typing in command mode:
"ap
3: This will output all your syntax scripts (excerpt from the first lines of my 1205 installed ones)
/usr/local/Cellar/macvim/7.4-72/MacVim.app/Contents/Resources/vim/runtime/syntax/2html.vim
/usr/local/Cellar/macvim/7.4-72/MacVim.app/Contents/Resources/vim/runtime/syntax/a2ps.vim
/usr/local/Cellar/macvim/7.4-72/MacVim.app/Contents/Resources/vim/runtime/syntax/a65.vim
/usr/local/Cellar/macvim/7.4-72/MacVim.app/Contents/Resources/vim/runtime/syntax/aap.vim
/usr/local/Cellar/macvim/7.4-72/MacVim.app/Contents/Resources/vim/runtime/syntax/abap.vim
4: Cut these by using Visual Block Mode, so that the result looks like this (again omitting ~1200 lines):
2html.vim
a2ps.vim
a65.vim
aap.vim
abap.vim
abaqus.vim
abc.vim
abel.vim
5: Now strip away the ".vim" ending by applying the following Regular Expression:
%s/.vim//g
Now they will look like this:
2html
a2ps
a65
aap
abap
abaqus
abc
abel
6: Save the result into a buffer. From there you can use this script to cycle through all of them:
v$h"*y:set syntax=^R*^Mdd
Or, in order to color all the open windows, use :windo set syntax=... instead of :set syntax=....
Explanation:
mark the first syntax name
v$h
yank/save the name under the register *
"y
set the syntax in command mode
:set syntax=
delete the last checked syntax name
dd
If you save this script in a macro, you can cycle through them using ##
Enjoy(;
I recommend using colorschemes (read more about them using ":h colo"). You can list all your installed color schemes by using this command:
:colorscheme <ctrl+d>
Those you can copy to a buffer and cycle through them. If you want to go the easy way, there is a script ready that provides that functionality: CycleColor, a script to cycle through (almost) all available colorschemes: https://github.com/vim-scripts/CycleColor

Using bash history to get a previous command, copy it and then 'run' it but with the command commented

Just a question to improve my bash skills. I always do this:
$ history | grep some_long_command
...
...
123 some_long_command1.........
124 some_long_command2.........
...
I can then run the command the command I found by doing:
!123
However, I often want to do this:
some_long_command1foobar
I.e. change the command before I run it. Can you use bash to run this command instead:
#some_long_command1
so it gets commented.
Then I don't have to use my mouse to highlight the command, edit it and then run it (I can just use the keyboard - faster).
I suppose I could write a script to do it but there might already be functionality built in somewhere....?
I'd suggest instead of using the history command, you use ctrl+r and start typing that command. When you press an arrow key as if to go to modify it, it will drop out of autocomplete recognition, and will let you edit before running.
UPDATE: also, if you want to cycle through the different commands that contain the string you just typed, keep on pressing ctrl+r
Actually, you can just append :p to the command to print it without actually running it. For example:
$ ls -la
$ !!:p
Will print out ls -la as the previous command without running it, and you can just press ↑ (up) to find it and edit it.
You can also do
!123:p
to print out the 123rd command as your previous command.
You can also try fc command to edit the command in the history.
WIKI says,
​fc​ is a standard program on Unix that lists or edits and reexecutes,
commands previously entered to an interactive shell. fc is a built-in
command in the bash shell; help fc will show usage information.
Apart from reverse-incremental search(Ctrl+R), we have some more bash shortcuts:
From man bash:
previous-history (C-p)
Fetch the previous command from the history list, moving back in the list.
next-history (C-n)
Fetch the next command from the history list, moving forward in the list.
beginning-of-history (M-&lt)
Move to the first line in the history.
end-of-history (M->)
Move to the end of the input history, i.e., the line currently being entered.
reverse-search-history (C-r)
Search backward starting at the current line and moving 'up' through the history as necessary. This is an incremental search.
forward-search-history (C-s)
Search forward starting at the current line and moving 'down' through the history as necessary. This is an incremental search.
non-incremental-reverse-search-history (M-p)
Search backward through the history starting at the current line using a non-incremental search for a string supplied by the user.
non-incremental-forward-search-history (M-n)
Search forward through the history using a non-incremental search for a string supplied by the user.
yank-nth-arg (M-C-y)
Insert the first argument to the previous command (usually the second word on the previous line) at point. With an argument n, insert the nth word from the previous command (the words in the previous command begin with word 0). A negative argument inserts the nth word from the end of the previous command. Once the argument n is computed, the argument is extracted as if the "!n" history expansion had been specified.
yank-last-arg (M-., M-_)
Insert the last argument to the previous command (the last word of the previous history entry). With an argument, behave exactly like yank-nth-arg. Successive calls to yank-last-arg move back through the history list, inserting the last argument of each line in turn. The history expansion facilities are used to extract the last argument, as if the "!$" history expansion had been specified.
shell-expand-line (M-C-e)
Expand the line as the shell does. This performs alias and history expansion as well as all of the shell word expansions. See HISTORY EXPANSION below for a description of history expansion.
history-expand-line (M-^)
Perform history expansion on the current line. See HISTORY EXPANSION below for a description of history expansion.
insert-last-argument (M-., M-_)
A synonym for yank-last-arg.
operate-and-get-next (C-o)
Accept the current line for execution and fetch the next line relative to the current line from the history for editing. Any argument is ignored.
edit-and-execute-command (C-xC-e)
Invoke an editor on the current command line, and execute the result as shell commands.
!123:gs/old/new/
Will run command 123 replacing the string 'old' with the string 'new'.
You can get to edit mode by hitting M-^ (option-shift-6 on a mac).
Type this:
!123M-^
And you'll be editing command #123. It's sort of like using ctrl-r, but starting with exclamation-point syntax.
Instead of using the history command, bind history-search-backward/history-search-forward to key shortcuts which can be remembered easily (I prefer PgUp/PgDown). To do that, put this into your .inputrc file:
"<key code>": history-search-backward
"<key code>": history-search-forward
To get <key code>, type Ctrl-V <key> in the shell, and replace the starting ^[ with \e in whatever was output.
After this is set up, you can just type some and press PgUp to get some_long_command. If you need some_long_command with_some_arg but there is a similar command some_long_command with_some_other_arg later in the history, you can cycle through until you reach it by typing some and then hitting PgUp repeatedly, or you can type some, hit PgUp, move the cursor to where the two commands start to differ, type a few characters and hit PgUp once more. This ability to quickly page through / differentiate between similar commands makes it in my opinion a much more comfortable tool than Ctrl-R.
You can also put
shopt -s histverify
in your .bash_profile, which causes any history expansion to appear on your command line without running it, allowing you to edit before doing so.
You may wan to try "suggest box"-like history https://github.com/dvorka/hstr - it reads Bash history and allows for quick navigation.
To get the last command simply type hh, navigate to the command and use right arrow to get it on command line (where you can edit it and/or add comment).
^p to get the last typed command in unix/solaris
Put
alias r='fc -s'
in your .bashrc (home dir)
then you can just type in
r <whatever>
at the command prompt and you will execute a copy of the last <whatever> command (same params) that is in your history. just hit up arrow to see what you have executed if you feel the need.

How do I insert a tab character in Iterm?

Simply put, I know you can do ctrl+v+tab to insert a physically real tab character in a bash statement. But how do I do the same for iTerm?
The answer was to hit control+v, then tab afterwards, not all together! Hope this helps someone.
It's not iTerm, but your shell that affects how you''re able to insert a tab.
First, make sure you're in BASH shell: Type the following command:
$ echo $RANDOM $BASH_VERSINFO
23714 3
The first is a random number, and the second should be the BASH Version number. If you get a blank line or just a random number, you're not in the BASH shell, and that's probably one of your issues.
Another thing is to execute this command:
$ set -o
allexport off
braceexpand on
emacs on
errexit off
errtrace off
[...]
privileged off
verbose off
vi off
trace off
The two lines of interest is the emacs and the vi lines. One of those should be on. If they're both off, you can't do the Ctrl-V-Tab to insert a tab character.
When the vi mode is on, it should be Ctrl-V-Tab like you said. With emacs mode on, it is either Ctrl-V-tab, or possibly Ctrl-Q-tab.
However, this isn't an iTerm thing, this is your shell that's doing it.
If by a "physically real tab character" you mean sending the tab hex code (0x09) to the shell, then you can do this in iTerm by pressing Ctrl + Tab (⌃ ⇥).
This is the default setting, although you can change it (as well as add other hex code values to send) in iTerm > Preferences > Profiles > Keys.
I'm not certain why you're comparing a "bash statement" with iTerm. You write bash scripts in iTerm. Which means, assuming you're writing your scripts in iTerm, you're already doing what you want.
I'll assume you mean scripting in vi vs command line. The way I get literal characters on the command line is by using vi editing mode. Do set -o vi. Then you can use ctrl+v followed by tab in the manner that you're used to.
IMO, using vi editing mode comes with a slew of other pluses like searching your history, faster navigation, etc. So you could just add it to your .bashrc if you wanted and use it all the time.
One should also try Ctl + V Ctl + I. It is working in konsole where Ctl+V+Tab deosn't work.

Resources