vim -s in command line without opening vim - vim

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?

Related

Open vim file with automatic replacement of ^M

I want to be able to open a file using vim but immediately have it execute this command %s/^M/\r/g
I am trying to do this with the:
nvim path/to/file -c "%s/^M/\r/g"
nvim says that the pattern ^M is not found but they are in the file. How can I pass the correct character in the command line?
I realise that c-vM works if you are in vim but I am trying to replicate the c-vM character in the terminal.
nvim path/to/file -c "set fileformat=unix"
As quoted here https://stackoverflow.com/a/26903948/2544873

Specify vim filetype on command line

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.

How to run Vim commands from Terminal

My questions is one that I haven't seen answered, the usual question is how to run certain commands to a file.
My question is how to run Vim commands or functions, from outside Vim (i.e. Terminal), these wouldn't affect any file, just Vim itself. Is this is even possible? If so, how?
If this is not possible like this, is there a way to go into Vim, run a command automatically, and then exit when that ends? Or run another command and then exit?
Thanks for your help!
Use
vim --cmd 'Command launched before vimrc' \
-c 'Command launched after vimrc' \
-c 'qa!' # Quit vim
. For running a command in an existing vim session you have to use +clientserver feature: run one vim with
vim --servername vim
and others with
vim --remote-send '<C-\><C-n>:Command<CR>'

What options are set when exim commands are given through a heredoc?

I have a bash script that runs (something like) the following command:
vim -E <<EOT
call Myfunc()
EOT
where Myfunc() is defined in my .vimrc. I've discovered that using the heredoc (but not simply calling it with -E and entering commands manually) causes vim to skip loading my .vimrc, consequently setting compatible mode (causing other problems down the road).
I can load my .vimrc manually if I have to, but I'm assuming I don't know a priori where it is, so I'd like to let vim do the work.
Does using the heredoc make vim set some other option (-u NORC, say), which I can just unset to get normal behaviour?
I'm in Vim 7.3, if it matters.
Giving arguments to vim through a heredoc is equivalent to taking input from stdin:
vim -E -
call Myfunc()
qa!
This starts vim in silent mode (see :help -s-ex) and only loads plugins specified by the -u argument. To load all plugins, write the ex command to a file and use that file for input:
cat <<EOT > input.vim
call Myfunc()
qa!
EOT
vim -E input.vim
(I added qa: to force vim to exit after running Myfunc().)

Troubleshooting vim configuration warnings

I've been messing with vim configuration recently, and now when I open the first file in gVim, I see some warnings that disappear quickly. Where else can I see them and see what's wrong?
Try one of the following:
After starting vim, use :messages command. It will show you all messages (except that were echoed with echo or echon).
Redirect vim output to some file: vim -c 'qa!' > messages.log.
Start vim using vim --cmd 'redir! > messages.log' -c 'redir END' -c 'qa!', then observe messages.log.

Resources