Custom command line editor in Linux - linux

I would like to invoke my own command line editor in bash or zsh when Alt+Enter is being pressed. It should do some editing and submit result to the shell on Enter. So basically my editor takes current command line content and returns a modified one. Any ideas how to approach integration? I do know how to work with ANSI terminals, just wondering how to integrate my editor console app to the shell in this way.

For Bash:
There is a Readline command that opens the current command in an editor, edit-and-execute-command. By default, it is bound to C-x C-e, and it opens the command in what $VISUAL is set to, or $EDITOR, or with Emacs.
You can set $VISUAL to your editor by exporting it into the environment, for example in ~/.bashrc:
export VISUAL=youreditor
and bind it to Alt+Enter with
bind '"\e\C-m": edit-and-execute-command'
on the command line, or
"\e\C-m": edit-and-execute-command
in ~/.inputrc.

Almost the same for zsh:
export VISUAL=youreditor
autoload -Uz edit-command-line
zle -N edit-command-line
bindkey '\e\C-m' edit-command-line

Related

How do I run a shell command from vim in the existing terminal SCM session?

Here's the problem statement:
I open a GNOME terminal and run a startup script setup_myproject_view1 which sets up an SCM session and sets some enviroment variables.
I open terminal vim and start browsing through some files often in multiple split panes (:sp, :vsp)
I realize that I need to edit and save one or more of these files. But before I can do that I need to check out the file from the SCM server/depot.
Now I have multiple options:
Close all my vim panes and run the checkout command in the shell and then open one or all of the files I had open before. Very tedious.
Open a new terminal window or tab and run the startup script and checkout the file. Then return to the original terminal, run :e to load the file again with write permission enabled and proceed to edit it. Still tedious because I have to run the startup script (so I have to remember which project view I'm working in) and switch through windows/tabs.
Run the shell check out command from vim using :shell or :!. This would be ideal because I'm quickly able to return to editing without breaking my train of thought. But the shell inside vim again does not have the startup session setup_myproject1_view1. Nor do I want to put setup_myproject_view1 to run automatically in my shell's .cshrc file because I have different projects and views: setup_myproject3_view2, etc.
So how can I run a shell command conveniently from vim without having to setup the SCM project view?
you can pause vim using ctrl+z, do your checkout and then resume it with fg if you invoked vim from a bash shell. For more information see bash job control
An alternative, you should modify your setup script to export variables to underlying shells. For example if your script is like this:
# script.sh
MEH=4
you will get this result when you use it:
source script.sh
vim
:shell
echo $MEH
exit
but if you change it like this:
# script.sh
export MEH=4
you will get this result when you use it:
source script.sh
vim
:shell
echo $MEH
4
exit

p4 change not working with gvim

I use perforce as source code repository. p4 change command is used to create a changelist of opened files. If I set setenv EDITOR gvim and then run this command then gvim opens and I add some description and then save and quit. I get below error. Same error does not come if EDITOR is not set, I mean in that case vim opens. Any idea to fix this issue?
sachina#inn-sachina-vm[285] p4 change
Error in change specification.
Error detected at line 29.
Change description missing. You must enter one.
Hit return to continue...
Applications that invoke EDITOR assume that the command blocks until editing is done and the editor was closed. While true for vim, the GUI version gvim launches in the background; i.e. the command returns immediately.
You can avoid this via the :help -f command-line option:
setenv EDITOR 'gvim -f'

edit-and-execute-command bash with sublime text

In a bash terminal session, I notice that the edit-and-execute-command C-xC-e does not work with Sublime Text 3.
I've set EDITOR=subl, but when I try to edit a command line from bash, sublime-text opens an empty window.
Do I miss something ?
You should export the EDITOR as:
EDITOR="subl -w"
as:
To use Sublime Text as the editor for many commands that prompt for
input, set your EDITOR environment variable:
export EDITOR='subl -w'
Specifying -w will cause the subl command to not exit until the file is >closed.
Full explanation here

text editor mode for calling bash command

suppose I want to enter a multiline command via bash I know that I can append \ in the end of the line to enter a new line
however is it possible to enter a legitimate "text editor mode" where you don't even have to enter \ and simply press enter would suffice
eg..you type in the command into the command line then before entering the parameters you press some magic button which allows you to enter a vi like mode then you enter stuff into the "vi mode" then you exit and then the text you entered in the "vi mode" turns into the parameters of the command then you press enter then the command executes
is it possible to do that in bash command line? if so, how do I do it?
See man bash:
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.
Per default bash is configured for emacs mode, hence the emacs like C-xC-e command.
If you really like vi you can also set your bash into vi mode: set -o vi. This allows you to do normal line editing the vi way without invoking an explicit editor.
Bash can emulate vim mode (though not very well) with:
set -o vi
You can edit the previous command in vi or your default editor by using the fc command. This pops open an editor window and when you exit it executes the edited command. That mode could bed used repeatedly to edit a complex command.

adding a shell script to a configuration file

I'm pretty new to shell scripting and linux in general. Basically, I need to change the configuration file for logging out so that when a user logs out, a certain shell script is run.
Now, I've located the logout configuration file and opened it with vi using this command
$ vi ~/.bash_logout
At this point, I'm experiencing some very weird behavior. When I try to type a character, the cursor jumps around seemingly erratically. What could this be due to? I'm running the latest version of ubuntu.
And once I get that figured out, what's the command to run a .sh file from within this configuration file?
If you're having trouble with vi, try using nano instead. nano .bash_logout
If you do need to use vi for some reason, "i" will put the editor into insert mode, and ESC will take it out of insert mode when you're done. ":wq" will write and quit the editor.
To run a command, just put it in the .bash_logout file as you would type it on the commandline.
Some other useful commands:
a insert after selected character
o insert at next line
O insert at previous line
r replace a single character
R replace mode
:q! quit without saving
:w save
:wq save and quit
To get familiar with Vi and its brother Vim ("VI improved") I recommend the book "A Byte of Vim", you can read it online or download for free at http://www.swaroopch.com/notes/Vim
You can permanently change your editor option. To find out what your current one is, type this:
export | grep -i edit
To change it on Ubuntu:
sudo update-alternatives –config editor
On any other BASH prompt, just do this:
export EDITOR="nano"
Replace 'nano' with 'vi', 'emacs', or any other preferred editor. You can also add this to your .bashrc by typing the following:
echo 'EDITOR="nano"' >> ~/.bashrc

Resources