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.
Related
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
Consider the following directory tree:
root/include/file.hpp
root/source/file.cpp
root/images/file.png
The command line is inside the directory root.
In the vimrc file, there is set wildignore=*.png.
If you open Vim in the folder root and run :next */file.*, it opens only file.hpp and file.cpp.
However, if you launch Vim from command line with vim */file.*, it opens all three files.
So, when feeding it a filename, it first loads the files, then vimrc? Is there a way to ignore extensions when opening files with Vim through the command line? Or to make Vim load vimrc first?
In the first scenario, the glob expansion is done by Vim and thus obeys the rules in your vimrc.
In the second scenario, the glob expansion is done by your shell and there's no reason to expect it to obey the rules in your vimrc.
You can do something like $ vim -c "next */file.*", which essentially opens Vim without a filename and executes next */file.*.
Or you can exclude the pattern directly in your shell. Assuming you have extglob set, this can be done in bash with $ vim !(file.png).
When doing :next */file.* from within Vim, vim expands the wildcard and filters by wildignore. When doing vim */file.* from your shell, the shell expands the wildcard, and passes all three files to Vim.
Depending on your shell, this will probably work instead:
vim +"args */file.*"
This question already has answers here:
Indenting in VIM with all the files in Folder
(4 answers)
Closed 8 years ago.
I want to indent all my files using the command gg=G of VIM. Is there anyway to write a script to do that ?
I imagine it can be something like
find . | xargs -n 1 | vim [ with some option to indent ]
I am quite sure vim -c may help, but not sure what is gg=G equivalent..
vim has two options you could take a look: (from man vim)
-s {scriptin}
The script file {scriptin} is read. The characters in the file are interpreted as if you had typed them. The same can be done with the command
":source! {scriptin}". If the end of the file is reached before the editor exits, further characters are read from the keyboard.
and
-w {scriptout}
All the characters that you type are recorded in the file {scriptout}, until you exit Vim. This is useful if you want to create a script file to be
used with "vim -s" or ":source!". If the {scriptout} file exists, characters are appended.
that means, you could record your key sequence by vim -w script for example gg=GZZ then you could vim -s script file
I think vimgolf uses this mechanism too.
You can use bufdo to apply a command to all buffers, and normal gg=G to run the normal mode command gg=G. And wqall to save them all.
You can do the following all within vim.
:args `find .`
:argdo normal gg=G
:argdo w
This builds up the argslist and then runs the normal command gg=G on each file in the argslist. Then we save each file in the argslist. Note: this requires set hidden.
Drew Neil over at Vimcast has some nice screencasts about this subject:
Meet the arglist
Populating the arglist
Using :argdo to change multiple files
Project-wide find and replace
For more help see:
:h argslist
:h 'hidden'
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().)
I know that using VIM I can format C++ code just using
gg=G
Now I have to format 30 files, so doing it by hand becomes tedious. I had a look how to do it passing external commands to VIM, so I tried
vim -c gg=G -c wq file.cpp
but it does not work.
Can you give me a hint?
Thanks
Why not load all the files up in buffers and use bufdo to execute the command on all of them at one time?
:bufdo "execute normal gg=G"
Change -c gg=G to -c 'normal! gg=G'. -c switch accepts only ex mode commands, gg=G are two normal mode commands.
I prefer a slight change on the :bufdo answer. I prefer the arg list instead of the buffer list, so I don't need to worry about closing current buffers or opening up new vim session. For example:
:args ~/src/myproject/**/*.cpp | argdo execute "normal gg=G" | update
args sets the arglist, using wildcards (** will match the current directory as well as subdirectories)
| lets us run multiple commands on one line
argdo runs the following commands on each arg (it will swallow up the second |)
execute prevents normal from swallowing up the next pipe.
normal runs the following normal mode commands (what you were working with in the first place)
update is like :w, but only saves when the buffer is modified.
This :args ... | argdo ... | update pattern is very useful for any sort of project wide file manipulation (e.g. search and replace via '%s/foo/bar/ge' or setting uniform fileformat or fileencoding).