How make `reverse-i-search` history use fzf in irb or pry console - irb

I have looked around, can not find anyway to use fzf to search history in irb or pry console. Is there any way make it?

After a look around, I get it that command completion is associated with GNU Readline, and then I found a pure ruby implementation of Readline, in fact
It's quite easy to make fzf works in pry with RbReadline, what I have do is overwrite the RbReadline's class method rl_reverse_search_history, which is triggered when we hit Ctrl + R in pry console.
Summary of what I have done:
Install rb-readline
gem install rb-readline
Modify your .pryrc add this
require 'rb-readline'
def RbReadline.rl_reverse_search_history(sign, key)
rl_insert_text `cat ~/.pry_history | fzf --tac | tr '\n' ' '`
end
The rl_insert_text inserts the result you selected from fzf into the terminal after your cursor.

Related

In IPython every shell command is run by prefixing it with "!" but few commands run without that, What is the reason behind it?

In case of "ls" command it runs with and without the prefix "!". In case of "cat fileName" it's the same, but when you consider "wc -l fileName" it works only with "!" prefix.
When you combine cat and wc command "cat fileName | wc -l" executed successfully without "!" prefix.
I don't understand the logic behind this prefix "!" in ipython.
Thank you in advance
(I am new to python programming, if it sounds silly question please forgive me.)
IPython tries to make interactive programming as comfortable as possible. Some shell builtins like ls, cd or cat are basic commands to navigate in unix shells. IPython, as a "Python Shell" provides the same functionally for convenience. Along with features like colored output, etc.
The !command is for executing arbitrary shell code and is much more powerful. It can be used to run any command you can type in a normal shell and can also catch its output.
Compare ls with !ls. The former will print the content in your current directory with nice coloring. The latter will print the same list, but just plain text.
But note that you can do really cool things with !command:
files = !ls
for f in files:
print("I like this file:", f)
Which reads the output of ls into a python array files which you can use in your code just like any other array.
To sum up: if you just want to navigate, you usually use the standard commands, if available. If you need to capture the output or run programs you have to use the !command syntax.

Command to clear the Git Bash screen, including output buffer

Is there any command in Git, that clear the screen. for example in window command line after execute a lot of code, if you type cls, then it will clear all the previous code. so i want the same type of functionality in Git. so can anybody will tell me the command name.
Actually you are looking for a Unix user environment command
clear
or you can use the keyboard shortcut
ctrl+l
http://en.wikipedia.org/wiki/Clear_(Unix)
To clear entire command history in Git Bash.
history -c
try using reset command, it will absolutely clean your screen but you will still have access to previous commands
reset
Neither clear nor history -c does the work actually.
Scroll up, all commands will be visible.
Solution:
If you are in Windows 10, and using mintty 2.7.9 (or above ?) for git bash,
use Alt + F8 ... this will work.
Best of luck.
Happy coding.
Reference: here (Perhaps it didn't work for Windows 7)
Neither clear nor history -c was clearing the history permanently.
All commands will be visible when scrolled up.
So, I solved the issue by:
In my instance the path for bash history was:
/c/Users/<your_username>/.bash_history
I removed the file by the following commands:
rm ~/.bash_history
After that, I restarted the terminal. the commands were gone.
CTRL + L
search for more shortcuts in: here
Another option is modify (or create in your user folder) your .bash_profile and add this:
alias cls='clear';
With this you can clear the bash with a 'Windows' command.
At the moment I use
clear;reset;clear
(in one line) and it sort of works (git version 2.32.0.windows.1).
Most times clr, clear and cls doesn't work use ctrl c to continue writing commands
use clear only without git command
" clear "

How to use ack with M-x find-grep?

Leaving question here for reference. The Lord alone knows why, but whereas once upon a time this didn't work for me, prompting me to ask this question, today it does. Typical bloody emacs. Also typically, it is an utter joy once you've wrestled with it a bit. YMMV.
These two commands:
find ~/myco -type f -print0 | xargs -0 -e grep -nH -e "setjmp"
and
ack --no-heading --no-color "setjmp" ~/myco
Seem to me to produce very similar results at the terminal.
And yet when I try to use the second as the command with M-x find-grep in emacs, it just prints the results, rather than hyperlinking and highlighting them as it would with the first.
Anyone know what is happening? Initial experiments with ack at the command line indicate that it blows find and grep out of the water, so it would be nice to be able to use it from emacs too.
On windows I call ack from find-grep e.g. like this:
c:/xampp/perl/bin/perl.exe c:/bin/ack-standalone.txt --type-set java=.java --type=java -i information
and it's working perfectly. The results are linked and colored in the grep buffer. I don't know why it doesn't work for you, but I can say it does work here.
emacs version: GNU Emacs 23.2.1
ack version: 1.94
Emacs tries to show the output of find-grep in a buffer in grep-mode. Apparently grep-mode (surprise, surprise) doesn't understand the output format of ack. Use a dedicated ack-mode.
I found this ack-mode to work better, and out of the box. Some more coloring would have been nice, but at least it's readable:
https://github.com/sudish/ack-mode.el

Auto-complete command line arguments

In bash, executables such as mplayer and imagemagick's "convert" have a cool auto-complete functionality on their command line arguments. For instance, if I type
mplayer <tab><tab>
in one of my video folders, then mplayer will list all media files located in that folder, and only the media files.
Similarly, if I type
convert -<tab><tab>
then I will see all the possible options of the convert script, which is great.
My question is how to achieve a similar functionality, using bash, ruby or python scripts?
This is an example of BASH's smart completion. A basic description is here, a guide to writing your own extensions is here and another (Debian-based) guide is here. And here's a fuller featured introduction to the complete command (the command that facilitates this behaviour).
The link to writing your own extension in the accepted answer has gone dead. Quoting here from http://web.archive.org/web/20090409201619/http://ifacethoughts.net/2009/04/06/extending-bash-auto-completion/
Bash provides you a way of specifying your keywords, and using them to
auto complete command line arguments for your application. I use vim
as a wiki, task managemer and contacts. The vim helptags system lets
me index the content instead of searching through it, and the speed
shows it. One feature I wanted to add to this was to access these tags
from outside vim.
This can be done in a straight forward way:
$ vim -t tagname
This takes me directly to specific content marked using this tag. However, this will be more productive if I can provide
auto-completion for the tags.
I first defined a Bash function for the vim commandline. I added the
following code to my .bashrc file:
function get {
vim -t $1
} Now I can use get tagname command to get to the content.
Bash programmable completion is done by sourcing the
/etc/bash-completion script. The script lets us add our
auto-completion script /etc/bash-completion.d/ directory and executes
it whenever it is called. So I added a script file called get with the
following code in that directory.
_get()
{
local cur
COMPREPLY=()
#Variable to hold the current word
cur="${COMP_WORDS[COMP_CWORD]}"
#Build a list of our keywords for auto-completion using
#the tags file
local tags=$(for t in `cat /home/anadgouda/wiki/tags | \
awk '{print $1}'`; do echo ${t}; done)
#Generate possible matches and store them in the
#array variable COMPREPLY
COMPREPLY=($(compgen -W "${tags}" $cur))
}
#Assign the auto-completion function _get for our command get.
complete -F _get get Once the /etc/bash-completion is sourced, you will get auto-completion for the tags when you use the get command.
Along with my wiki I use it for all the documentation work and at
times the code too. I also use the tags file created from my code. The
indexing system lets me remember the context instead of the filenames
and directories.
You can tweak this system for any of the tools you use. All you need
to do is get a list of the keywords for your command and give it to
the Bash programmable completion system.
This functionality in bash is provided by bash-completion and similar functionality is included in zsh. If you want to add support for some program not currently supported by one of these tools, you need to write your own extensions for them.
How do I enable bash completion on Ubuntu?
sudo apt-get install bash-completion
source /etc/profile.d/bash_completion.sh
sudo apt i[TAB]

edit commandline with $EDITOR in tcsh

Today's Daily Vim says this:
Assuming you're using the bash shell, the following can be helpful when composing long command lines.
Start typing on the command line and then type Ctrl-x Ctrl-e, it should drop you into your system's default editor (hopefully Vim) and allow you to edit the command line from there. Once finished, save the command line, and bash will run the command.
Is there any way to do this in tcsh?
A little explanation for the uninitiated.
bindkey -v
puts you in vi-mode (oh yeah!)
and hitting v from there would take you to $EDITOR -- and all is good with the world from there on.
Hmmm... IIRC, tcsh uses a command called bindkey. Try bindkey -v at the command line. Then hit escape followed by v. It's been a while since I used tcsh so the details are a bit fuzzy. When in doubt, Google it.