How to map keys for IdeaVim's normal mode to editor's action? - vim

I am using JetBrains' phpstorm with the IdeaVim plugin.
I am wondering if I can bind keys in normal mode to editor actions.
For example, I used to have mapped Ctrl+B to Navigate > Declaration. Yet Ctrl+B is a vi motion to go one page backwards and that is ok.
I know I can configure a keyboard shortcut to a different one, e.g. Ctrl+Shift+B , yet to keep things simpler I want to have a key in ideavim's command mode mapped to that functionality, e.g. ;.
So that pressing ; in command mode would trigger the action of Declaration witin phpstorm.
How can I achieve this?

To give a specific answer for exactly what you asked to map: put this into your ~/.ideavimrc:
nnoremap ; :action VimGotoDeclaration<CR>
To find the action name, I typed :actionlist declaration which gives a subset of action names that include the word "declaration" in the action name.
As others have noted, you might also prefer to use one of the existing mappings rather than adding a new one.

what you wanted go to declaration is built in command in vim. You don't have to use IDEA's actions.
gd (goto declaration) is the thing you are looking for.
So you just press (normal mode) gd, to see what is gonna happen.
In a normal vim, do :h gd to check details.

You can use <C-]> (Ctrl+]) for following references (jumping to the declaration is an example of a reference) and <C-O> for going back. You can also map these Vim-style shortcuts using the map commands similar to the original Vim.

Related

Vim backslash in command

I feel like this is a very stupid question, but I can't seem to find the answer to this anywhere. I'm relatively new to vim. After a fresh install I installed the vim-LaTeX plugin, or the LaTeX-suite for vim. It works correctly apart for the fact that there are some commands I don't understand. It gives the following instruction:
To do this, visually select a portion of the text and press \ll while in visual mode. The visually selected portion will be saved to a temporary file with the preamble from the current document prepended. Latex-Suite will then switch focus to this temporary file and compile it
But if I press '\' it does nothing and double l moves the cursor to the right. I don't understand what I'm supposed to be doing, and I feel incredibly stupid. Sorry if off-topic.
The \ is the default value for the :help <Leader> key. Plugins are encouraged to start all default mappings with <Leader>. If you've redefined it, you need to use that key instead.
Otherwise, you need to press V \ L L (without Shift) in quick succession (by default within one second).
If you want to find out whether the plugin has been successfully installed and there's actually an action behind those keys, you can use either:
:verbose vmap <Leader>ll
:verbose vmap \ll
If Vim says No mapping found, or just lists other mappings starting with some of the keys, the plugin isn't installed properly, or you're not using it right.
Press v to go into visual mode. Then you can use your arrow keys to select text. After you've selected the text you want, type \ll.
That should be it.

Mapping TO ctrl+[something] doesn't work in gvim

I am using GVim 7.4 and I would like to do some really simple mapping to use CtrlP fuzzy matching of tags when a key combination is used.
I tried 2 approaches and they all seem to fail when vim calls Control + [x] combination. While I do understand that there are restrictions when it comes to mapping Ctrl+[x] codes, I haven't found any information on why ctrl mapping wouldn't work.
noremap \t :CtrlPTag<CR><C-\>w
This one enters CtrlP tag mode but then it doesn't enter word from under the cursor.
noremap \t <C-p><C-\>w
Here we don't even get to CtrlP window (I even omit going into tag mode here for simplicity).
As far as I understand (I'm no CtrlP user), the plugin is triggered via some command / key combination, and then presents interactive selection and filtering. It even has different "source" modes.
Now, this is a pretty heavy integration into Vim, probably using scratch buffers and its own input loop. That's why you cannot simply append keys to the mapping and get them interpreted by the plugin "as typed".
Typically, these plugins offer mode selection and so on via (optional) command arguments. Check the plugin's help, and if you cannot get the plugin into the state you need, best contact the plugin's author and ask for such enhancement.

How to map cmd+s to save all?

I'm developing some front-end project with the help of Yeoman.
I run the developing webserver by grunt serverand, because there is livereload.js in the project, for watching files change and refreshing the webpage, sometimes, I need to modify multiple files for one purpose(.html, .css, .js ...) and it's kind of waste for browser when each file saves.
So is there a way for MacVim, map cmd + s to :wa ?
nnoremap <D-s> :wa in your .vimrc should do the trick.
nnoremap means this mapping is only available in Normal mode, and is not recursive.
:wa means write all, i.e. save all the current open buffers.
If you don't want to use a new mapping, you can simply type :wa to achieve the same things, but this is a matter of preferences only.
I know this is old but I came searching with the same question.
tl;dr: ⌥+⌘+S to save all.
You can, supposedly, map <D-s> to :wa but it's not as simple as adding the mapping to your .gvimrc file. See the third bullet below.
From the MacVim docs:
cmd-key cmd-shortcuts
Creating key mappings that involve the Cmd key ( in Vim notation) can
sometimes be slightly involved. Here are all the things you need to consider:
Make sure the shortcut is not used by a menu item by looking through the
menus. If it is then you need to unbind it before you can map to it. This
is described under the help for the |:macmenu| command.
Bindings to are case sensitive: is not the same as . If
you want to map something to Cmd+Shift+d, then you need to use , not
or .
Some command key shortcuts are reserved by Mac OS X and cannot be mapped to
(e.g. ). However, some of these shortcuts can be freed up in the
System Preferences under Keyboard (e.g. Cmd+Space).
The good news? In trying to sort this out I realized that MacVim is a very courteous MacVim indeed and implements, by default anyway, many standard Mac shortcuts including: ⌥+⌘+S for "Save All". Open MacVim, click the file menu and then hit the option key to see it in action.

how to add vim keymap

While programming I am regulary using the following two lines:
sprintf(buff,"%s", __func__);
putrsUART(buff);
Is it possible to set any keyboard shortcut to insert these two lines?
E.g. when I type \sp in command mode, these functions get added at the cursor position in my file. Is this possible? And if so, how do I map my keys?
Thanks in Advance.
You can use abbreviations, which are designed for this.
:abbr spb sprintf(buff,"%s", __func__);
:abbr uart putrsUART(buff);
Use :help abbr for the gory details. Note that you need to type another character after the abbreviated form for vim to recognize them. This comes naturally for these as you will type ENTER as the next character. It is also possible to enter more than one line with abbreviations. Simply use <CR> where you want a new line.
Here's an easy mapping for normal mode that lets you hit \sp (unless you've remapped leader, in which case use that instead of \) in order to insert the sprintf statement.
map <Leader>sp isprintf(buff,"%s", __func__);<Esc>
That being said I think abbreviations are the way to go here
As already mentioned, abbreviations (which I would limit to insert mode (:iabbr), because you probably won't need them in the command-line) are best for simple expansions; you can also define them only for certain filetypes only (via :iabbr <buffer> ...).
Your __func__ looks like a template parameter that you need to adapt each time. You cannot do this via abbreviations, but there are various plugins (many inspired from functionality in the TextMate editor) that offer template insertion with parameter expansion and several advanced features. Check out one of snipMate, xptemplate, or UltiSnips.
Try snip-Mate for inserting regularly used codesnippets. http://www.vim.org/scripts/script.php?script_id=2540
Wrong answer, Sorry:
Try this in your vimrc:
map <c-w> :sprintf(buff,"%s",func)<cr>
This means mapping to Ctrl-W.

VIM Auto Complete Navigation

I've disabled the arrow keys on my .vimrc, but...
How do I navigate up and down in autocomplete pop-ups? j and k doesn't work as I am in the Insert mode.
You can use ctrl+n and ctrl+p (which are conveniently close to everything else). Incidentally, these also work in bash and other useful places, so are good to get used to.
You may also like other popupmenu commands: http://vimdoc.sourceforge.net/htmldoc/insert.html#popupmenu-keys

Resources