edit-and-execute-command bash with sublime text - linux

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

Related

Custom command line editor in 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

Are there any Linux text editors that understands bash colors?

I am going through some logs written by a program that normally just logs to the console. Since it outputs to bash, I see a bunch of characters which are used for coloring in bash.
Is there any text editor out there that can interpret these character sequences and display the lines in color as bash does? Would be nice to be able to search through these logs without seeing a bunch of otherwise garbage characters.
If you don't want to use an editor you could use:
echo -e $(cat colorfile)
less -r colorfile
Or use vim addon AnsiEsc:
Download the AnsiEsc.vba.gz vim scripts file from (https://www.vim.org/scripts/download_script.php?src_id=14498)
Install details:
Open the AnsiEsc.vba.gz vim-scripts file in vim:
vim AnsiEsc.vba.gz
:so %
:q
Then open your file with vim and type
:AnsiEsc

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'

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