I am a newbie of Vim and I am using MacOS. In MacOS we usually use cmd and \ to comment the code block and I also want to achieve this in my Vim setting. Following to this post, I know how to modify the ~/.vimrc to comment different languages like C++ and Python. The remaining part is setting a shortcut key for commenting which I have no clue on that. Is it possible to set cmd and \ as a shortcut key for commenting in Vim? Or is it possible to use non-alphabetic symbols to form shortcut keys?
First…
In MacOS we usually use cmd and \ to comment the code block
No, this has very little to do with macOS. Beyond the basics like "open file", "copy", etc. what sequence or combination of keys does what is decided at the application level. The only OS-specific part, here, is the use of the command key but macOS editors/IDEs can use whatever they want (IntelliJ uses cmd+/, for example).
Second…
When Vim is executed in a terminal emulator on macOS, it is incapable of seeing the cmd key or any combination that includes the cmd key so you can forget about mapping anything to cmd+\ in that context.
When using the MacVim GUI, the cmd key can be used in mappings as <D-: cmd+\ would be <D-\>. While that would work in the MacVim GUI, I would advise against doing it because it is not portable.
Related
So it seems like ctrl-space is being incorectly translated into ctrl-# by the Windows Terminal and I was wondering if there's any workaround for this? I've seen answers to this similar problem for other terminals but they don't seem to work.
This is fundamentally a limitation of the way that most Unix terminals work. The control keys map onto a limited set of characters, so Ctrl-#, Ctrl-Space, and Ctrl-` all map to the same thing and work the same way. This occurs in terminal emulators and the VT running on real Linux systems as well.
It is possible that other terminal emulators have an option for this, but the Windows Terminal attempts to emulate the xterm-256color terminal type, which implements the behavior you're noticing. Even if other terminal emulators do support it, that doesn't mean that programs running in them do; they might receive such a sequence and not know what to do with it, especially if TERM is set to a terminal type that doesn't support the distinction.
According to the Windows Terminal issue tracker, they are aware of this and there is a possibility they may pass these keys in the future using some xterm-specific sequences, but currently this doesn't work. I'm not sure that even if they did implement it that it would work as you expect in Vim, although it's possible it might.
You can either remap Ctrl-# if you aren't using the existing functionality or just use another key.
Autohotkey is a good company for vim in Windows. It can map ctrl-space to any other keys and send them to vim by running scripts.
https://www.autohotkey.com/docs/Hotkeys.htm#Intro
AHKscript like(not tested)
^Space::
send {Ctrl}g
return
Then in vimrc
nnoremap <c-g> ...
I am trying out VimWiki. So far so good, but on Ubuntu 18.04, the Shift-Enter and Ctrl-Enter do not work [1]. Search on the net says Gnome Terminal interprets these key combos as the regular Enter key. So I believe I have to remap these keys to something else or find another terminal program. I have tried Terminator, but it works the same as Gnome Terminal.
[1] https://github.com/vimwiki/vimwiki
You can change the key mappings, or you can run Vim as a graphical application (gvim). You may have to install the relevant distribution package (such as vim-gtk3), it is sometimes split from the pure terminal application.
I suspect the author of this extension is using Vim as a GUI application because I don't think there are many terminals which preserve these key combinations.
I am a vim user.
I like to use CLI vim rather than GUI MacVim.
I usually use vim in iTerm2 terminal which has already a lot of shortcuts using Command Key.
I want to map my custom shortcuts but it's impossible because of those already mapped keys.
For example,
I want to map NERDTreeCommenterToggle command to 'Command-/'.
So I wrote in my .vimrc like below.
map <D-/> <Plug>NERDCommenterToggle
But it doesn't work because "D-/" key is already mapped to 'Find Cursor' in iTerm2.
Does anybody know how to fix these problems? Thanks in advance.
No.
It's impossible to map the command key in CLI Vim because the only Vim that supports it is MacVim in its GUI form. Whether iTerm uses up all the Cmd+… shortcuts or not is irrelevant.
You must find other keys, preferably some that work everywhere (CLI, GUI, Mac, Win, Linux).
:h mapleader is a good start.
I am trying to use the command button in MacVim instead of Ctrl and other buttons.
I tried something like :imap <D-space> <Esc> to override the normal Command-space functionality while in MacVim but it still performs the spotlight.
Any ideas?
In these cases its usually the window manager intercepting the key-stroke before the application ever sees it. There may be an option to turn off certain global key-bindings when a specified app is active but I've been unable to find it on 10.5 - linux window managers have this functionality, as does windows, so I'd expect its hiding somewhere in the OS WM..
The Spotlight shortcut takes precedence over your own shortcut.
To be able to use it in MacVim you must change Spotlight's shortcut in its preferences.
However, the Command key won't work in Terminal.app and won't be sent to remote servers connected by SSH. If you use Vim on a server you won't be able to use it and you will be forced to either create and learn lots of machine-specific mappings or simply use the defaults.
Another possible issue, one I encountered at the beginning, is that shortcuts with multiple modifier keys (like <D-M-something> or <C-S-something>) don't work in MacVim.
Because of that and the number of native shortcuts using these keys you won't have many possibilities.
When I decided to learn Vim (with MacVim), I tried a lot of things to make it (and Vim) more "Mac-like" or "TextMate-like" to no avail.
Instead i suggest you to do things the Vim way. It may seem weird but it's worth it.
In your case, the "Vim way" would be to use mapleader, it's a regular key on your keyboard that is used for custom mappings. The default key is \, if you want to change it (to , for the example, that's my settings but YMMV), add this line to your ~/.vimrc:
let mapleader=","
After that you can create mappings like this:
inoremap <leader><Space> <ESC>
My job involves working on Windows, *ix and with variety of IDE's like Eclipse, Visual Studio. Sometimes I really don't want to open Eclipse for little editing so I would prefer simple text editor like Vim, but the keys for the commands are horrible to me, I would like to setup Vim command keys to simple and modern style.
Can someone guide me how to configure Vim commands to simple keys like Ctrl+C or Ctrl+V for copy paste (through clipboard) or Shift+→, Shift+← for selecting text.
See this question: How do I configure VIM for using Ctrl-C Ctrl-V as copy paste to and from system buffer in ubuntu?
and the over all help on how to do key mapping here: How to make a keymap
It's not the answer you're looking for, but I'm with hammar. For example the Mac version of vim has the standard Mac-style copy/paste shortcut keys "baked in" yet I use the default vim keybindings now, since, like yourself, I hop about Linux/Windows/Mac, and having the same vim keyboard shortcuts.
Conversely, you'll find managing your personalized, if unorthodox, key bindings more onerous than simply "going vim."
I presume you've "worked" the vimtutor?