VIM Unix commands printed in color - vim

I'm using MacVim and I would like to have ! commands printed in color. For example:
In bash, the following echo statement prints Hello World in green (as expected):
$ echo -e "\033[32m Hello World"
Hello World
However, in VIM the output is not color, and the escape codes are printed:
:!echo -e "\033[32m Hello World"
[32m Hello World
How can one have VIM (and MacVim build 57 in particular) print the output of ! commands and honour ANSI color escapes.

You can't. But you can suspend the editor and drop to a shell relatively quickly;
Or you can use Ansi Filter to remove the escape sequences so you will at least not see a mess.

this one:
:!echo $(tput setaf 1)Hello world$(tput sgr0)
will print Hello world in color.
Don't use escape sequences, but named tput entries. (all times, not only in this example). read:
man teminfo; man infocmp; man tput - for more information.
based on comments I found this question very interesting.
Still searching for the better solution, but for now find this one - http://code.google.com/p/conque/ .
Allow run colored commands inside MacVim's buffer.

Don't know if this would help, but running my RSpec tests inside vim gives me colored output using the --color option. I use the following command to run the current spec file inline:
:map ,t :w\|:!rspec --color %<cr>

If you run macvim in console mode (vim, not mvim) all :! commands are redirected to the shell and executed there. They take the whole window instead of 1/3 of it, and they use whatever theme your console happens to have.
But you get ansicolors.

Your question (and its pop up done by #avocade) addressed the issue I have with some printing in my aurum plugin thus I’ve wrote (started to write, but the most significant piece of functionality is already here) the ansi_esc_echo plugin. To use it in your one you must install it, install frawor and do
execute frawor#Setup('0.0', {'autoload/ansi_esc_echo': '0.0'})
call s:_r.ansi_esc.echo("\e[33mabc")
. Currently it deals only (speaking exclusively about special characters or sequences) with carriage return, backspace (untested), tab, newline, and CSI colors.

Related

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

Differences in running vim via command line vs. running it in the vim editor

I am trying to process a series of files. I have noticed that there are discrepancies in running a particular command from the command line (i.e. ex mode). E.g.
$cat poo.txt
big
red
dog
small
black
cat
$vim -c "2,$g/^dog/d|wq" poo.txt
$cat poo.txt
big
small
black
cat
It appears that 2,$g/^dog/d|wq has deleted the lines with red and dog. This confuses me because the command should : start on line 2 (going to EOF) and delete all lines beginning with dog. In this instance, I'd expect the output to be:
$ cat poo.txt
big
red
small
black
cat
In fact, if I try this in the vim editor this is the exact behavior that is observed.
QUESTION: What is cause of the discrepancy between the vim -c version and the vim version of running this command?
I think you need to replace the double quotes with single quotes to prevent your shell from expanding $g. From man bash:
Enclosing characters in double quotes preserves the literal value of all
characters within the quotes, with the exception of $, `, \, and,
when history expansion is enabled, !.
Currently, your shell expands $g inside your string, as if it was an environment variable. But it's probably not defined, thus expands into an empty string. So, even though you've typed:
vim -c "2,$g/^dog/d|wq" poo.txt
Vim doesn't receive the command:
2,$g/^dog/d|wq
... but:
2,/^dog/d|wq
This command deletes all the lines from the one whose address is 2, to the next one which starts with dog (in your case it's the 3rd line). Then, it saves and quit.
But even if you replace the quotes, there's still a problem in your command.
From :h :bar:
These commands see the '|' as their argument, and can therefore not be
followed by another Vim command:
...
:global
...
The bar is interpreted by :g as a part of its argument, not as a command termination. In your case, it means that whenever it finds a line starting with dog, it will delete it, then immediately save and quit. So, if there are several dog lines, only the first one will be deleted, because :g will have saved and quit after processing the 1st one.
You need to hide |wq from :g, either by wrapping the global command inside a string and executing it with :execute, or by moving wq in another -c {cmd}. All in all, you could try:
vim -c 'exe "2,\$g/^dog/d" | wq' poo.txt
or
vim -c '2,$g/^dog/d' -c 'wq' poo.txt
or
vim -c '2,$g/^dog/d' -cx poo.txt

Running arbitrary vim commands from bash command line to script vim

I want to script vim to edit files from the command line. For example I want to do something along the lines of:
vim -<SOME_OPTION> 'Iworld<Esc>bIhello <Esc>:wq helloworld.txt<CR>'
or:
echo 'Iworld<Esc>bIhello <Esc>:wq helloworld.txt<CR>' | vim
and have it save the file helloworld.txt with a body of hello world
Is this possible? I've tried a few different approaches but none seem to do it. I realize I can do things like vim +PluginInstall to run Ex commands from the command line, but I'd love to be able to string together arbitrary motions
This can be achieved with the + flag and the :normal command:
$ vim +"norm Iworld" +"norm Ihello " +"wq helloworld.txt"
I think what you are looking for is vim's -w/W and -s {scriptin} option. Well in your case you should make a scriptfile, and with -s file to let vim execute all your "key presses"
I think vimgolf has used these options too.

Typing an SSH command doesnt go to a new line

While typing a nice long command though SSH (ie. rsync copy), the line doesn't shift to a new line and just overlaps the current line when i hit the right edge of the screen. Any suggestion on why this is happening and how to fix?
The setup is below (and an example command).
Debian6 64bit - Its a DOMU VM (XEN)
/test/test-srv release]$ rsync -avzh --dry-run /test/long/source/path/blah/blah/blah/ /test/long/target/path/etc/etc/etc/etc/etc/etc/test
The bit in bold is the overlapping part which should just push the cmd prompt up a line.
Image Example:
This is because of your colored prompt. You neglected to mark ANSI escape codes as invisible.
Bash doesn't know what your terminal does, so it relies on you to tell it which parts of the prompt are non-printing and which are not. You do this by adding \[ \] around them.
# Example bad prompt which wraps lines incorrectly like you describe
PS1='\033[01;34m\w \$ \033[00m'
Here, \033[01;34m and \033[00m don't show up on the terminal (that is, they don't move the cursor), they just change the color of the following text. To show this, we wrap them in \[ \]:
# Prompt from above that wraps correctly
PS1='\[\033[01;34m\]\w \$ \[\033[00m\]'

Alternative to Up Arrow + Enter to run previous command?

Sometimes I have to run a command many times in succession, for example to see if a service has started, and it becomes tedious to move my hands away from my normal typing position to press the Up Arrow and Enter keys repeatedly. Is there a way to run the previous command without the Up Arrow and Enter keys, perhaps with an elaborate shell script?
I've tried the following, but it is unsatisfactory because it cannot execute aliases, and it is a little slow.
history | tail -2 | head -1 | cut -d' ' -f4- | cat > prev_command.txt
sleep .01
chmod 777 prev_command.txt
eval prev_command.txt
rm prev_command.txt
Ideally I'd have an alias to this script so I can type in something like "prev" in the command line and hit Enter to run the previous command again.
In bash, you can press ctrlp to go to the previous command -- that's a lot better than having to move to the arrow keys.
See also: https://github.com/fliptheweb/bash-shortcuts-cheat-sheet/
Use
!!
to run your previous command.
sudo !!
also works , for the record.
Instead of running the same command many times in succession, why not watch it instead? watch will run a specified command repeatedly and display the output in stdout so you can see it change over time.
watchcommand
I often use the "history expansion" feature in bash (usually activated with cntlR) -- it interactively searches through your history for the previous closest match.
See the bash manual section Searching for Commands in the History, and also Using History Interactively.
Are you an emacs or vi user? You can use
set -o vi
set -o emacs
to set emacs or vi keybindings. You can then use the emacs or vi key bindings in bash. I don't know if this should work for other shells. I believe the vi mode starts in insert mode, so you need to hit esc to enter command mode. In emacs mode (the default), you can use ctrl+p and then ctrl+j to move to the previous line and do a carriage return.
Otherwise, you can use !! as someone else suggested.
In bash:
$ help fc
fc: fc [-e ename] [-lnr] [first] [last] or fc -s [pat=rep] [command]
Display or execute commands from the history list.
fc is used to list or edit and re-execute commands from the history list.
FIRST and LAST can be numbers specifying the range, or FIRST can be a
string, which means the most recent command beginning with that
string.
Options:
-e ENAME select which editor to use. Default is FCEDIT, then EDITOR,
then vi
-l list lines instead of editing
-n omit line numbers when listing
-r reverse the order of the lines (newest listed first)
With the `fc -s [pat=rep ...] [command]' format, COMMAND is
re-executed after the substitution OLD=NEW is performed.
A useful alias to use with this is r='fc -s', so that typing `r cc'
runs the last command beginning with `cc' and typing `r' re-executes
the last command.
Exit Status:
Returns success or status of executed command; non-zero if an error occurs.
Note the suggestion for alias r; I use this frequently.
Depending on what terminal you're using, I know a lot used to have F3 as an option for repeating, but that's still outside the normal range for typing as well unless you have a special keyboard with more accessible function keys.
My keyboard makes the function keys easily accessible, but I don't do much command line work in unix any more, so I wouldn't be able to tell you for sure whether or not this is still possible.

Resources