How to treat a Makefile with another name in vim? - vim

I am working in a library that has its own Makefile, so I am using my own Makefile called my_makefile and use make -f my_makefile
My problem is how to tell Vim that my_makefile is an actual Makefile and use color scheme that it use for a Makefile.
It is hard to read it now.

How about the
:set syntax=make
command

You could also use Modelines, which are comments in either the first 5 or last 5 lines of a file that vim reads, and are used to set the filetype. For example, in a makefile, it would be:
# vim: filetype=make

I also have found another way! You can use "my_makefile.mk" and vim automatically know it is a MakeFile and you do not need to change .vimrc either.

Related

Separate settings for Easy Vim (with -y flag)

Is there a way to have some settings which will work only for Easy Vim (Vim with -y flag, i.e. vim.exe -y), but not for "normal" Vim?
This works for me:
_vimrc file + _gvimrc file
However, this doesn't:
_vimrc file + _evimrc file
Probably I shouldn't try to use separate file for it, but incorporate such settings into _vimrc or _gvimrc instead? How is it possible?
As far as I can see, this isn't easy, but it's doable:
From within Vimscript (= in your .vimrc), you can call ps to get the arguments vim was called with
echo split(system("ps -o command= -p " . getpid()))
This prints ['vim', '-y'] and you could then go on checking if -y is in that list (with count) and then do different things depending on that.
I see that you're using Windows though, so you have to find an alternative to the ps solution. This answer on superuser makes me believe WMIC path win32_process get Caption,Processid,Commandline might be a start..
I would guess that those "settings" that break in Easy Vim is 'insertmode', as that is the defining option of it. You can check for it in your ~/.vimrc, like this:
if ! &insertmode
" Stuff that only works in default (non-Easy) Vim.
" ...
endif

javac current file code in vim file?

I want to source a vim file in vimrc file.
I wrote code in vim file.
set makeprg=javac %
and vim said he doesn't know that option.
how do i write code that javac current file?
what I want to do is compiling current file in vim while coding..
On the command-line a space is used to separate 2 arguments. So, when you execute:
set makeprg=javac %
The :set command assigns the value javac to the option 'makeprg', then it tries to enable the option '%', which doesn't exist, hence the error.
If you want the space to be included inside the value of the 'makeprg' option, you need to escape it:
set makeprg=javac\ %

make from within vim

I am using gvim for coding c++. Since my program involves cmake, my sources are located in a different directory than my build.
How can I still invoke :make from within vim so that the correct make file within the build directory is found?
how can I then subsequently start my application with the very same command line style and one keystroke?
TD;LR: just assign 'cd "compi/dir" && make $*' to &makeprg (with :let) and run :make youroptionaltarget -j 4.
In another Q/A, I've already criticised the :!make approach. This is what we had to do 20-ish years ago with vi. Vim has introduced integrated compilation through quickfix mode. Please learn vim way to compile code.
In CMake case, we can indeed compile with cmake --someoption compil/dir. I use it when I compile with VC++ compiler, piloted by CMake, from vim. The rest of the time, I'd rather avoid it as it messes compiler outputs by prepending each line with number>, which breaks vim quickfix feature. Unfortunately there is no neat way to ignore this noise by tweaking &errorformat. So far I postprocess cmake and ctest outputs to remove ^\d+>.
So. What can, and shall, we really do? We should take advantage of vim quickfix feature. This is done by tweaking &makeprg and &efm options. In your case, the first one should be enough. Use it to change directory and execute make. And finally compile with :make and navigate errors with :cn, :cp, :cc, etc.
If you want also to compile in background, you'll need a plugin that knows how to compile in background in a directory which is not the current one. This is where I advertise my build-tool-wrappers plugin that provides these features and a few more CMake related features.
PS: It's also possible to use make (and ninja) -c parameter.
The easiest solution I came up with is the following:
:! (cd /path/to/your/cmake/build/directory; make)
To execute the program at the same time, you can append commands with ; or &&:
:! (cd /path/to/your/cmake/build/directory; make && ./myProgram)
In this page, you can find a tutorial how to bind this in order to do this in one key stroke.
Explanation:
In vim, you can execute any command with :! command (for instance, :! ls).
With (cd [...]; [...]), you specify that the execution directory is different by creating a subshell and changing to this directory in the subshell.
You can use the following:
autocmd filetype cpp nnoremap <F8> :w<CR> :!clear<CR> :!make && ./%<<CR>
This will allow you to Compile/Run C++ using Makefile with <F8> and clear console

vim unmap everything (completely stripped down vim)

Love vim. Tried Janus. Like the idea, but it's too slow, 100 plugins competing for keybindings. I want to actually understand what every vim configuration I make does.
Is there a way to "unmap" all vim keys or as much as possible. Remove most commands, etc. The point of this is an exercise for myself. I want a super small subset of maps, settings, bindings, so I can add things one by one as I need them.
Have you seen efforts like this?
Try to run vim with the following command :
"vim -u NONE"
from man vim
-u {vimrc} Use the commands in the file {vimrc} for initializations.
All the other initializations are skipped. Use this to
edit a special kind of files. It can also be used to skip
all initializations by giving the name "NONE". See ":help
initialization" within vim for more details.
It should start vim without any plugin or customisation.
Aside from
:mapclear
:nmapclear
:vmapclear
:xmapclear
:smapclear
:omapclear
:mapclear
:imapclear
:lmapclear
:cmapclear
starting it like
vim -U NONE -C -Z
will start
-U without startupfile
-C in compatible mode
-Z in restricted mode
I tried to do something similar and posted that on reddit post.
I try to restrict the mappings I define in my vimrc and load only some
plugins that I find essential.

what is the load order of scripts when you start up vim?

If you start up vim with something like this:
vim -S myscript.vim file.txt
What is the load order of scripts? Does myscript.vim get loaded after or before ~/.vimrc.
If you pass in vimscript commands to vim directly on the command line, when do they get executed relative to sourced and default vimscripts?
I believe vimrc is always first. You can run :scriptnames to get a list of sourced scripts in order in which they were first sourced in your Vim instance.
The help entry is way too long to post here, but it lists the order of everything that vim does at initialization. See :help initialization.
The answer is myscript.vim gets loaded dead last.
The vim -V option is a lifesaver here. (Capital -V, because -v starts in vi mode.) Just ran across it, after searching further since although the other answers answered your question, they don't show what wasn't sourced because it wasn't found. If I could send it back in time, I'd save myself a lot of time banging my head against strace output.
This will not only show you all of the scriptnames that it did source in order, but also all of the scriptnames that it would have sourced if they existed in order. So, you can discover what files you can create to load at the appropriate time.
$ vim -V
Adding it to your vim arguments easily answers the question.
$ vim -V -S myscript.vim file.txt
It shows myscript.vim as dead last.
It prints a ton, and winds up at a "Press ENTER or type command to continue" prompt, which lets you step through Autocommands.

Resources