1

I have some plugins I would prefer load by default in the terminal, and cause performance issues or UX glitches in Jetbrains' IDEs running IdeaVim. How can I detect IntelliJ IDEs in Vim?

I have used this in the past to detect the name of the terminal tmux is running in:

echo $(basename "/"$(ps -f -p $(cat /proc/$(echo $$)/stat | cut -d \  -f 4) | \
    tail -1 |\ sed 's/^.* //'))

I'm looking for a Vim equivalent of the above pipeline using Vim built-ins for querying the state of the environment. Is there an api for that?

1
  • Off topic but I couldn't help but notice that shell command. Way too long. There's "useless use of cat" (and echo) and other redundancies. I have to guess a bit what you're doing since there are different versions of some of these commands but just paring it down in a tokenized fashion I get basename "/"$(ps -f -p $(cut -d \ -f 4 < /proc/$$/stat) | sed -n '$ s/^.* //p') But then I looked at it a little closer...are you trying to parse ps output for the parent of the current process? Then for that part, depending on environment, you may just need ps -f -p $PPID !
    – B Layer
    Commented Feb 2, 2019 at 3:00

3 Answers 3

3

Things have evolved quite a bit since this was posted and there is apparently now support for vimscript parsing. I'll update this once I learn the details. In the meantime, see other answers.

Generally speaking ideavim can handle consuming of native vimrc. That's because it doesn't actually parse it as vimscript. It just does pattern matching so anything non-compatible is skipped over. Most people get away with this just fine...

source ~/.vimrc

And that command is recommended on ideamvim's github homepage.

Unfortunately, if you want to pick and choose parts of vimrc to include there's no proper way. But there is a workaround based on the aforementioned fact that ideavim isn't a vimscript parser....you can source things in vimrc that you don't want sourced by ideamvim using this:

exe "source ~/someconfig.vim"

Ideavim will skip over such lines so you could have common stuff in vimrc and source vim-only stuff by way of the above command. Then you can safely source vimrc in ideavim.

Update: To reiterate, ideavim does not have a vimscript/VimL interpreter. It does a simple pattern-based scan of any sourced vimrc file, recognizing a few directives/commands...

Currently IdeaVim parses several .vimrc commands (:set, :map, :source, etc.) via pattern matching, i. e. it doesn't fully understand syntax of Vim configuration files.

The above quote is from an issue in the ideavim bug tracker that is requesting more expansive capabilities in this area.

Until/unless there are any such enhancements made, the things OP would like to do (e.g. conditional sourcing based on environment via something like if has('win32') ... endif) are not possible except to the limited extent that I mention above and which is also discussed on Stack Overflow How To Include Only Some of Vimrc in IdeaVimrc

4
  • I guess I'm asking how in vimrc to selectively load settings in a specific environment. Say, detect whether running on windows, Mac, or Linux and load a plugin or change from tabs to spaces. Using similar concepts I was wondering if it would be possible to detect some jetbrains software and change settings based on that. I do not know how much of vimscript ideavim implements. Hopefully, enough to accomplish what I am attempting to do.
    – ZeroPhase
    Commented Feb 22, 2019 at 22:52
  • My answer directly addresses the question of how the Vim emulator in Intellij processes vimrc files. It doesn't work anything like native vim. It just looks for basic patterns (e.g. set some-setting=foo) and ignores everything else including system type directives. So while native vim can conditionally execute vimscript only on a Windows system with if has('win32') <vimscript> endif Intellij can't execute vimscript at all!. You can only do basic configuration conditionally on Intellij vim vs native vim using the hack I describe.
    – B Layer
    Commented Feb 23, 2019 at 10:35
  • @ZeroPhase Thanks! Cheers.
    – B Layer
    Commented Mar 31, 2019 at 6:17
  • Down voting because this answer is no longer true.
    – jbyler
    Commented Mar 8 at 21:49
2

Since IdeaVim 1.8.0 you can use the following condition: has("ide"). This works because IdeaVim supports VimScript for several years since 1.7.0.

Wrap the lines you want to make conditional in your ~/.vimrc like this:

if !has("ide")
  "Settings to hide from IdeaVIM go here
endif
2
  • Can you edit to explain how this is compatible with the existing answer, which says that IdeaVim does not support conditionals? That would make branching on has('ide') useless, if still true.
    – D. Ben Knoble
    Commented Sep 16, 2023 at 15:20
  • IdeaVim supports VimScript for several years since 1.7.0. Commented Sep 17, 2023 at 16:46
0

Not really sure why you want to check that, since IdeaVim uses .ideavimrc and not .vimrc or init.vim.

And so you can not have single common rc file for both IntelliJ and Vim. Contents of .vimrc and .ideavimrc can be same but the files have to be different.

2
  • Yeah, my .ideavimrc just sources my .vimrc. I still need to detect IdeaVim to keep config files maintainable. This will end up with .vimrc refactored to .ideavimrc just including common functions through a shared .sh script, included in each. The solution is Jetbrains deprecating IdeaVim and replacing it with the neovim engine so everything just works. But, that's probably five years off, and a .vimrc customized for Vim causes issues in Rider, in particular; it would be nice to track the particular variable down to make Rider usable in long files. (different issue from CLion)
    – ZeroPhase
    Commented Jan 28, 2019 at 19:35
  • I do agree that Neovim engine would be solution to all issues with Ideavim but yeah as you should there is no time line on that. What i would recommend is to break down your vimrc into multiple files, like one for settings, mappings, plugins etc, and then just source the specific ones in ideavimrc and in vimrc just source them all. As you might know there are number of things that don't work in Ideavim, few to name are commnad mode mappings, Autocommands, and some G commands in visual mode. Commented Jan 29, 2019 at 5:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.