Different show between cat and vim
If you're asking why "garbage" characters are showing up at the beginning and end of each line in Vim but not with cat, the reason is probably that they are formatting escape sequences that the shell uses to color text. Since cat sends its output directly to the shell, the escape sequences are interpreted as formatting commands, producing the colored text. Vim is not designed to use these formatting sequences, so it just displays them as part of the text.
To get rid of the escape characters, you could delete them manually, or you could use a tool like sed to filter them out as outlined here.
difference between 'cat -A' in bash and 'set list' in vim
In vim, type
:set ff?
I suppose it will respond with
fileformat=dos
That means that the end of line is ␍␊
(^M^J
, \r\n
) rather than just ␊
(^J
, \n
). This is autodetected by vim when opening the file if all newlines are consistently the same two-byte sequence.
To re-open the file in unix mode, just type:
:e ++ff=unix
now it will show the ^M
characters. It will show them even without list
option, because they now are in the buffer as regular characters.
Linux cat command output with new lines to be read using vim
You could use xargs to line upp the arguments to vi:
vim $(cat 1.t | xargs)
or
cat a.lst | xargs vim
If you want them open in split view, use -o (horizontal) or -O (vertical):
cat a.lst | xargs vim -o
cat a.lst | xargs vim -O
Differences in running vim via command line vs. running it in the vim editor
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
Syntax highlighting/colorizing cat
cat
with syntax highlighting is simply out of scope. cat
is not meant for that.
If you just want to have the entire content of some file coloured in some way (with the same colour for the whole file), you can make use of terminal escape sequences to control the color.
Here's a sample script that will choose the colour based on the file type (you can use something like this instead of invoking cat
directly):
#!/bin/bash
fileType="$(file "$1" | grep -o 'text')"
if [ "$fileType" == 'text' ]; then
echo -en "\033[1m"
else
echo -en "\033[31m"
fi
cat $1
echo -en "\033[0m"
The above (on a terminal that supports those escape sequences) will print any text file as 'bold', and will print any binary file as red. You can use strings
instead of cat
for printing binary files and you can enhance the logic to make it suit your needs.
Can linux cat command be used for writing text to file?
That's what echo
does:
echo "Some text here." > myfile.txt
Related Topics
Bash Script to Install Postgresql - Not Working
Yocto: How to Install Header Files Along with Kernel Module in Sdk
Openldap Naming Context Issue with Apache Directory Studio
How to Enable or Configure Ftrace Module
What's The Relation Between 32/64-Bit Application, Os and Processor
Putting Extensions in a Certificate in Openssl
How to Know If I Can Compile with Fma Instruction Sets
Change The Rlimit_Nproc in Linux
Ftrace: System Crash When Changing Current_Tracer from Function_Graph via Echo
Use Awk to Print $0 Using The Same Format for All Columns
Command and Script to Re-Read a File in Gnuplot
How to Programmatically Know If I Am in a Vm
Grep If The Next X Lines Doesn't Contain a Specific String