php[architect] logo

Want to check out an issue? Sign up to receive a special offer.

What’s the big Idea? PHPStorm for Vim Users

Posted by on August 31, 2024

I started my IT career in system operations. I spent many days in a cold data center, racking and stacking servers, installing operating systems, and configuring routers. That’s when I started using Vim. It was nice to sit in my cubicle outside the data center and access my servers to do everything I needed in a terminal, including editing files. I didn’t put any real thought into why Vim. It was just an option available on the command line to allow me to edit files. There were others—Nano, Emacs—and today, I couldn’t honestly tell you why I didn’t opt for one of them instead of Vim. But Vim did the job well, so I stuck with it.

I was content with my remote access and using Vim to edit config files. I didn’t realize there was much else to it. However, I had never considered using Vim for other tasks, especially development work. That changed when I observed a good friend of mine coding using Vim. It was one of those “aha” moments for me. I realized that Vim could be utilized for much more than modifying config files. That realization kick-started my journey.

I should take a moment. This article is not written to convince you to try Vim. I am happy to share the benefits I have experienced using Vim if you ever run into me and are curious. But if you are content with the tools you currently use and they are effective for you, don’t worry about what others use. So, if you were expecting this article to be a Vim 101 lesson, it is not. You may want to consider not reading this article further. This article is intended for a particular group of people, such as Vim users, who comprehend how to use Vim but wish to take advantage of the IDE PHPStorm.

So let’s get into it.

An Idea Is Born

In this article, I’ll explain how I set up my PHPStorm to mirror my Vim configuration closely. For the sake of clarity, I prefer using NeoVim, but I will refer to it as Vim in this article. Everything I share here should apply to both NeoVim and Vim.

For me to use any text editor, it almost always has to have some Vim emulation, and many of them do, such as VSCode. I have used many different tools with various implementations of Vim movements or emulations, and I can unequivocally say that the Ideavim emulator for PHPStorm and other JetBrains products is hands down the best Vim emulator I’ve ever used out of the box. By itself, it will probably get you about 80% of the way to having the feeling of a true Vim experience.

It also contains a secret superpower that I’ll discuss in a minute. But first, let’s discuss the basics. IdeaVim has nearly 100 contributors and is continuously being updated. Installing IdeaVim is pretty straightforward. It’s just like installing any other plugin in a JetBrains product. You open up your settings panel, go to plugins, and look for IdeaVim. Click install and enable. That’s it! You’re 80% to your authentic VIM experience.

If you’re a vanilla theme user, there is little more you need to do to start coding. But where’s the fun in that? Besides, this would be a really short article if that’s all we had to do.

No configuration is required at this point. However, you can enable an Ideavim Mode Widget under View -> Appearance -> Status Bar Widgets to see which Vim mode you’re currently in.

 

At this stage, you have pretty much all the essential Vim features you would expect from an emulator. You have normal mode, insert mode, visual mode, all the Vim motions, change command, and window management. You even have marks, macros, and registers. You have a full Vim regex search and replace, to name a few.

IdeaVim also comes with many common plugins that Vim users use, such as EasyMotion, NerdTree, Surround, Which-Key, and Commentary. I could probably write another article about any of these, so I won’t get into them or how to use them now. Below is a complete list of plugins as of this writing.

  • easymotion
  • sneak
  • NERDTree
  • surround
  • multiple-cursors
  • commentary
  • ReplaceWithRegister
  • argtextobj
  • exchange
  • textobj-entire
  • highlightedyank
  • vim-paragraph-motion
  • vim-indent-object
  • matchit.vim
  • IdeaVim-Quickscope
  • Which-Key

There are a few other plugins you can install through the JetBrains Plugins Marketplace to get more of that (n)Vim experience, such as IdeaVimMulticursor if you are into that sort of thing, or Harpooner, which is meant to emulate the Harpoon plugin in NeoVim.

Okay, I brushed over a small detail about the built-in plugins with IdeaVim. Although they are part of the Ideavim IDE plugin, they aren’t enabled by default. If you’ve used Vim for a long time, you’re familiar with the .vimrc file. The Ideavim Plugin for IntelliJ IDEs has a similar concept, which, as you probably guessed, is called the .ideavimrc file. Like the .vimrc file, the .ideavimrc file is stored in your home directory.

If you have an existing .vimrc file, you can source it in the .ideavimrc file using the following command.

bash
source ~/.vimrc

Personally, I wouldn’t recommend doing this. It’s just as easy to copy the file from your .vimrc to your .ideavimrc and keep the files separate. I’m uncomfortable with the idea of that one config file impacting multiple applications like PHPStorm and Vim. So, do with that information as you please, but from where I sit, I recommend starting with a fresh .ideavimrc file and putting everything you need in it.

Fresh Starts

Now that we have a fresh .ideavimrc file, you can enable these plugins using the set command as shown below;

bash
set surround
set multiple-cursors
set commentary
set argtextobj
set easymotion
set textobj-entire
set ReplaceWithRegister

The .ideavimrc file uses the same syntax as the vimrc file. Since we’re here, I have a few more things I would like to set up. This is how I typically set up my .vimrc and .ideavimrc files because it’s what I feel comfortable using after years of using Vim. None of these are required, and you should feel free to mix and match your own settings.

" I've always been told I am insensitive,
" and its how I like to do my searches
set ignorecase smartcase

" Match as I type while searching
set hlsearch

" Just like to know what mode I am in
set showmode

" Personal preference when scrolling
set scrolloff=5

" What can I say? I am a history buff, and I like to keep mine
set history=1000

" I'm not totally sure how this is different from the hlsearch,
" but I've always used it
set incsearch

" I like knowing what line I'm on in the code
set number

" One of the more controversial settings I use.
" This shows my line relative lines above and below my current line.
set relativenumber

" If I include an uppercase character in my search,
" then make the search pattern case-sensitive
set smartcase

You can also add your keybindings to the .ideavimrc file. Here are some examples of the keybindings I use. Again, it is not required, and I will include a version of my entire .ideavimrc file at the end of the article.

" Set the leader key to 
let mapleader = "\"

" clear the search buffer
nnoremap h :nohlsearch

" Shortcut for escape when in insert mode
imap jj 
imap jk

" Splitting Windows
nmap V :vsplit
nmap H :split
nmap \ :vsplit
nmap - :split

" Quick navigation split windows
map  j
map  k
map  l
map  h

" Open/Close File Explorer "NERDTREE"
nmap e :NERDTreeToggle

" When next'ing through search results, place them in the middle of the screen
nnoremap n nzz
nnoremap N Nzz

Ready And Action!

Superpowers time, you thought I had forgotten. Buckle up; it’s about to get exciting—well, as exciting as using an IDE can get, I guess.

It goes without saying that PhpStorm is a very powerful IDE with many features that a standard Vim setup would not have. So, how do you hook into that? Welcome to IdeaVim’s action command. With the action command, you can pretty much do anything you can do by clicking around the menus in PhpStorm. Let me show you some examples of how I use it. Notice the :action command in these bindings.

" When in normal mode and I hit Enter,
" format and save the file, please
nmap  :action ReformatCode :write

" My Ctrl-P simulation
nnoremap  :action GotoFile

" Mapping to mimic BufferExplorer
nmap bf :action RecentFiles

" Other different GotoFile mappings
nmap f :action GotoFile

" Find Usage
nmap u :action FindUsages

" Search for something in the entire project
nmap st :action FindInPath

" Create a new file or directory
nnoremap nf :action NewFile
nnoremap nF :action NewDir
nnoremap ns :action NewScratchFile
nnoremap np :action PhpNewFile
nnoremap nc :action PhpNewClass

"Code Formatting
nnoremap cf :action ReformatCode

" XDebug Shortcuts
nmap xl :action PhpListenDebugAction
nmap bp :action ToggleLineBreakpoint
" Jump to Source
nmap ] :action XDebugger.JumpToSource

You can get a complete list of all the Action commands available to you by entering command-line mode in the IDE and typing actionlist.

bash
:actionlist

You can even search for a particular action by passing a pattern to the actionlist command.

bash
:actionlist refactor

I would be remiss if I did not mention that PhpStorm comes with many of its own key bindings, and they are highly configurable through the IDE itself. This is not to say one solution is better than the other or that any particular key binding is superior to another. However, if you’re like me and have been using Vim for a long time, you likely have specific muscle memory when performing actions while you are in your codebase.

I have one more “pro tip” for all of you who have stuck with me through the article. In PhpStorm, you can create a “Quick List” under Appearance & Behavior > Quick Lists. You can create as many as you would like and add any actions to it. You can make a context-aware quick list, such as one for your Git command. Or you can be like me and create one for a lot of actions you don’t use enough to remember keybindings for but have a need for them. Then you can make a .ideavimrc keybinding to that quick list.

bash
" Open my quick list
nmap <Leader><Leader>l :action QuickList.MyQuickList<CR>

That’s pretty much it. You might have to tinker with some settings to get them to work correctly. IdeaVim is a fantastic solution if you are accustomed to using VIM motions and key bindings when working in a text editor.

Oh, and Pro Tip 2! You can edit your .ideavimrc file and hot-load it into PHPStorm the same way you would in VIM by going into command-line mode and sourcing the file.

bash
:source ~/.ideavimrc

There’s so much more we could explore with Ideavim that we don’t have time for, such as IDE-specific options in your .ideavimrc or .vimrc file to customize behaviors and keybinding per different JetBrain IDEs like PyCharm or GoLand. You can also explore all the different plugins. I encourage you to check all these things out.

Q&A Time

For those of you who may not have heeded my warning that this is not a VIM 101 article, here are some questions you might have.

Is it hard to learn Vim?

I honestly don’t know. To me, using Vim has become second nature. I really don’t think about it too much anymore. I’m probably not the best person to explain how to learn Vim to a non-Vim user, mainly because not understanding how to use Vim, how Vim works, or its benefits is what’s odd to me.

What are the benefits of Vim? And do I still have them if I use Ideavim and PHPStorm?

I’m a horrible typist. I’ve always been a horrendous typist. I’ve never been trained on how to type. For a very long time, I was a hunt-and-peck typist. Being a lousy typist and a full-time developer is not a good combination. For me, the most significant benefit of Vim is time. I don’t lose time or rhythm by stopping typing, removing my hand from the keyboard, and reaching for a mouse. Vim allows me to keep my fingers on the keyboard and continue typing. Vim has other benefits, such as the power of some of its plugins and its extendability. But for me, it’s time. And that benefit transcends.

Is using Ideavim with PhpStorm a good way to learn Vim motion?

Short of actually using (n)Vim in the terminal, I think using PhpStorm is a fine way to learn vim movements and here is why. If you’re already a PhpStorm user, everything stays the same for you. You can turn the Ideavim plugin on and off on the fly without closing your project and reopening it. If you do go this path, I wouldn’t worry about plugins. Not just yet. First, you need to understand how to get into the different Vim modes. Mainly the normal mode, insert mode, and visual mode. Understand when to use which mode and how to transition from one mode to the other. Learn your basic Vim movements—mainly the h-j-k-l, which translates to moving left, down, up, and right. Once you get that to a point where you’re comfortable, you can continue to extend fundamental Vim functionality without enabling any Vim plugins. This includes things like how to jump to the end of the line, how to jump from word to word, or how to find something. And if you get too lost or frustrated, you can turn the Ideavim plugin off, and you’re still in the same PHPStorm IDE you know and love.

If you like Vim so much, why bother using PhpStorm?

If I had to go back to using Vim full-time as my primary tool for coding in a PHP codebase, I could do that. Heck, I did it for years. But I’m a professional developer who focuses on PHP development. As such, I wanted to ensure I had the best tool for that job, which has support in case of a problem. I am a tinkerer, and I’m constantly changing my NeoVim setup. Occasionally, I’ll mess it up and have to roll back or start over. Sometimes, I enjoy exploring other NeoVim implementations and configurations. I’ll try them out and evaluate what I like and dislike, what feels good, and what doesn’t. Eventually, I move on to something else. I don’t want to risk any of that experimentation regarding my income. If I couldn’t maintain that level of customization with my Vim configuration, I would be disappointed. For a significant portion of my PHP development work, I use PhpStorm. The team behind PhpStorm is very involved with the PHP community, understands the language well, and consistently improves the tool.

What are your thoughts on the Vim emulator in VS Code?

I haven’t touched VSCode in several years. From what I remember when I used it, the Vim emulation was pretty good. But the reality is, I don’t need VSCode. For my PHP work, I use PhpStorm. For anything else, I use NeoVim. There’s no reason to introduce VSCode into that mix. So, I am not saying it’s bad or good; I am just saying I haven’t used it in a while, so I couldn’t tell you.

If I install the Ideavim plugin, will I still be able to exit PHPStorm?

(⚈_⚈) Please leave.

Promises Kept

Okay, as promised, here is most of my .ideavimrc file. I redacted things I’m still experimenting with or still need to figure out if it’s a good implementation. But as I said earlier, you should go out and find what works for you—what feels good to you when you’re working.

It’s a little long for the magazine, so here is a Gist link https://gist.github.com/ericvanjohnson/9976e3e8a28a5af544e37da8d2e1e035

If you have any questions, feel free to hit me up on X (https://x.com/shocm) or Mastodon (https://phparch.social/@eric).

Helpful Links

Files
ideavimrc


Tags: , ,
 

Leave a comment

Use the form below to leave a comment: