I run vim scripts on a regular base. Is it possible to run a script e.g.
vim -s script.vim file.txt without opening vim and returning possible error messages to the terminal?
I've been working with a lot of C++ files that have no extensions and it's too annoying to have to type :set ft=cpp every time I open them, so I'm mostly just working without syntax highlighting. Is there a way to tell vim the file type in the command line? Something like:
$ vim --ft=cpp file_name
You can use the -c option when launching vim to execute commands after the first file has been read.
For your situation, you can simply use the standard set filetype command -
vim -c 'set filetype=javascript'
You could also use --cmd to execute the command after the first file is loaded.
Lifted from the vim man pages:
-c {command}
{command} will be executed after the first file has been read. {command} is interpreted as an Ex command. If the {command} contains spaces it must be enclosed in double quotes (this depends on the shell that is used).
Example: Vim "+set si" main.c
Note: You can use up to 10 "+" or "-c" commands.
--cmd {command}
Like using "-c", but the command is executed just before processing any vimrc file. You can use up to 10 of these commands, independently from "-c" commands.
I'm trying to use a vim script to clean up some subtitle files.
When I paste these commands into vim, they work fine. However the lines with ^M in them aren't working when being used by vim -s script.txt filename.srt.
This is the contents of script.txt (image, because ^M's break things):
Is what I'm trying to do possible? or should I attack it in a different way?
The solution was to replace the ^M with \r. This works with vim -s.
I am using a linux OS and attempting to find the ^M characters in a .vim file, specifically jcommenter.vim. I would like to find and replace the ^M characters but am unable to do so. I have tried
%s/^V^M//g
and
%s/\r/\r/g
Unfortunately neither are able to find ^M. I have read that when vim reads a file, it will not include ^M, is this true? How can I find and get rid of ^M?
Edit:
This is a sample of the error I recieve:
line 21:
E492: Not an editor command: ^M
line 31:
E492: Not an editor command: ^M
This occurs everywhere there is a blank line.
EDIT:!!!
I have found a clue!
/usr/share/vim/macros/jcommenter.vim"[converted][dos]
dos?????? How do I destroy [converted][dos]?
To convert your file from "dos" format to "unix" format, load the file in vim and do:
:set ff=unix
:w
:q
The ff is short for fileformat.
You can do like
$ dos2unix plugin/jcommenter.vim
Use fromdos file (in command line, not in vim). This usually gets rid of odd control characters.
If you don't have it you can get it from the standard repos
Is there a text editor on Linux that allows me to see line breaks and carriage returns? Does Vim support this feature?
To disagree with the official answer:
:set list will not show ^M characters (CRs). Supplying the -b option to vi/Vim will work. Or, once Vim is loaded, type :e ++ff=unix.
Assuming your vim settings for :set listchars=... is set to visualize the characters you are attempting to see, in this case the carriage return characters (typed with CTL + V, CTRM + M) —— otherwise, as reported in many of the comments on this answer, the ^M character will not show on :set list
:set list in Vim will show whitespace. End of lines show as '$' and carriage returns usually show as '^M'.
vi shows newlines (LF character, code x0A) by showing the subsequent text on the next line.
Use the -b switch for binary mode. For example , vi -b filename or vim -b filename --.
It will then show CR characters (x0D), which are not normally used in Unix style files, as the characters ^M.
Just to clarify why :set list won't show CR's as ^M without e ++ff=unix and why :set list has nothing to do with ^M's.
Internally when Vim reads a file into its buffer, it replaces all line-ending characters with its own representation (let's call it $'s). To determine what characters should be removed, it firstly detects in what format line endings are stored in a file. If there are only CRLF '\r\n' or only CR '\r' or only LF '\n' line-ending characters, then the 'fileformat' is set to dos, mac and unix respectively.
When list option is set, Vim displays $ character when the line break occurred no matter what fileformat option has been detected. It uses its own internal representation of line-breaks and that's what it displays.
Now when you write buffer to the disc, Vim inserts line-ending characters according to what fileformat options has been detected, essentially converting all those internal $'s with appropriate characters. If the fileformat happened to be unix then it will simply write \n in place of its internal line-break.
The trick is to force Vim to read a dos encoded file as unix one. The net effect is that it will remove all \n's leaving \r's untouched and display them as ^M's in your buffer. Setting :set list will additionally show internal line-endings as $. After all, you see ^M$ in place of dos encoded line-breaks.
Also notice that :set list has nothing to do with showing ^M's. You can check it by yourself (make sure you have disabled list option first) by inserting single CR using CTRL-V followed by Enter in insert mode. After writing buffer to disc and opening it again you will see ^M despite list option being set to 0.
You can find more about file formats on http://vim.wikia.com/wiki/File_format or by typing:help 'fileformat' in Vim.
Try the following command.
:set binary
In Vim, this should do the same thing as using the "-b" command line option. If you put this in your startup (i.e., .vimrc) file, it will always be in place for you.
On many *nix systems, there is a "dos2unix" or "unix2dos" command that can process the file and correct any suspected line ending issues. If there aren't any problems with the line endings, the files will not be changed.
I suggest you to edit your .vimrc file, for running a list of commands.
Edit your .vimrc file, like this:
cat >> ~/.vimrc <<EOF
set ffs=unix
set encoding=utf-8
set fileencoding=utf-8
set listchars=eol:¶
set list
EOF
When you're executing Vim, the commands in file .vimrc are executed, and you can see this example:
My line with CRLF eol here ^M¶
By using cat and -A you can see new lines as $ and tabs as ^I:
cat -A myfile
You can view break lines using the gedit editor.
First, if you don't have it installed, for Debian/Ubuntu/Mint based distros:
sudo apt-get install gedit
For Fedora/CentOS/RedHat based distros:
sudo dnf install gedit
or
sudo yum install gedit
Now, install gedit plugins:
sudo apt-get install gedit-plugins
or
Under Gnome2, user plugins were put into ~/.gnome2/gedit/plugins/
For Gnome3: ~/.local/share/gedit/plugins/
Download the plugins from: https://help.gnome.org/users/gedit/stable/gedit-plugin-guide.html.en#gedit-additional-plugins
and select Draw Spaces plugin, enter on Preferences, and chose Draw new lines:
Using Visual Studio Code, you can install the Line endings extension.
Sublime Text 3 has a plugin called RawLineEdit that will display line endings and allow the insertion of arbitrary line-ending type
Shift + Ctrl + P and start type the name of the plugin, and toggle to show line endings.
Add the following alias to your .bashrc or .bash_aliases:
alias vidos='vi -c ":e ++ff=unix" -c "set list"'
Then you can use vidos to edit the file and see newline as $ and carriage return as ^M.