Input <M-e> with vim - vim

I'm on a mac terminal using the auto-pairs plugin. I'm trying to use the feature to wrap some text in parentheses. The manual says to use <M-e> but I do not know what this corresponds to on my keyboard. I've tried just about everything but nothing works. Google turns up nothing.

In Terminal.app…
open the Preferences window,
choose the Profiles tab,
choose your current profile,
under Keyboard, check "Use 'Option' key…".
In iTerm.app…
open the Preferences window,
choose the Profiles tab,
choose your current profile,
under Keys, choose which option key you want to use as Meta and check +Esc.
In your vimrc, add the line below to make it understand <M-e>:
set <M-e>=^[e
The ^[ is a literal <Esc> that you insert by typing <C-v> then <Esc>.

I think "M" normally refers to either "Mod" (alt) or "Meta" (the apple key in your case). So it would either be alt-e or apple-e

Related

How to efficiently copy code from editor to search field in VS Code using vscodevim?

This is currently how I copy code from an editor to a search field in VS code using vscodevim.
Select text in editor somehow
Right click to open up the contextual menu (since pressing Ctrl+C does not seem to work on Ubuntu, even when in input mode, and 'p' does not work in the search field) and click copy
Press Ctrl+Shift+F to open the search field
Press Ctrl+V
I'm pretty sure this is not how copying from an editor to search field is intended to work. It it the steps 1 and 2 I would like to change to something better.
What is a more efficient and vim-like sequence?
If you want to search for the word under the cursor
Ctrl-F will do the trick.
Or you can use Vim's * command, which effectively does the same, but jumps to the next occurrence right away by default.
Otherwise
If you need to use the search field for whatever reason, then the standard Vim way to copy stuff to the clipboard works, so you can yank into the * or + registers. The steps will then be:
Select text
"+y (you can create a shortcut for this combination if you want)
Ctrl-Shift-F, Ctrl-V
See also: How to make vim paste from (and copy to) system's clipboard?.
Having said that, the more obvious approach might be to use Vim's built-in search features, so after selecting the text, the remaining steps would be y: (yanking selection to the default register and opening the command-line) then / or ? (search forward or backward), then <C-v> (pasting the yanked selection to the command-line - this works only in the VSCode plugin, while in Vim you should use <C-r>").
I'm new to VIM so there might be a better way using VIM commands but for now this works pretty well.
Go into insert mode with i than select the word you would like to search for (I'm using the mouse) and than just press ctrl + f. Your search window will open as usual containing the selected word in it.

What's a common use case of "selection mode" in vim?

It's the first time i noticed about the selection mode in vim when i accidentally triggered it from visual-line mode by <c-g>.
Vim already has visual mode for text selection what's the use case of selection mode, can anyone give me a hint on this?
(note: i've checked the manual page which describes it as
a resemblance of MS Windows text selection
but i still cannot quite understand why do we need any mouse actions in vim)
Select-mode is commonly used in snippet plugins such as vim-snipmate. Mappings can be created that are unique to select-mode so as not to interfere with regular visual mode behaviour.
Here is an excerpt from Practical Vim by Drew Neil:
If you are happy to embrace the modal nature of Vim, then you should
find little use for Select mode, which holds the hand of users who
want to make Vim behave more like other text editors. I can think of
only one place where I consistently use Select mode: when using a
plugin that emulates TextMate's snippet functionality, Select mode
highlights the active placeholder.
As the documentation suggests, select mode is a bit different from visual mode. Here's the commands you can do in it:
Commands in Select mode:
- Printable characters, <NL> and <CR> cause the selection to be deleted, and
Vim enters Insert mode. The typed character is inserted.
- Non-printable movement commands, with the Shift key pressed, extend the
selection. 'keymodel' must include "startsel".
- Non-printable movement commands, with the Shift key NOT pressed, stop Select
mode. 'keymodel' must include "stopsel".
- ESC stops Select mode.
- CTRL-O switches to Visual mode for the duration of one command. *v_CTRL-O*
- CTRL-G switches to Visual mode.
Otherwise, typed characters are handled as in Visual mode.
When using an operator in Select mode, and the selection is linewise, the
selected lines are operated upon, but like in characterwise selection. For
example, when a whole line is deleted, it can later be pasted halfway a line.
You can see the docs here.
What they mean by "resembles MS Windows selection" is probably that you can extend the selection with Shift+Arrows, and also that any printable characters entered will substitute the selected text and enter insert mode.
Also, see this question/answer at the vi.SE.

edit gvim mouse buttons

I have a two part question regarding mouse clicks on gvim.
1.) I would like to change my gvim double left mouse click into something similar to the windows environment (ie Notepad, Microsoft Word), wherein it: highlights the current word, and replaces it with whatever I type. This is akin to pressing 'cw' for changeword in vim.
2.) I would like my single mouse click to place a cursor upon the selected text and enter insert mode. Again like how a mouse normally behaves in Windows.
1) nmap <2-LeftMouse> ciw
You could use viw here, which will visually select the current word, but then you will still have to press c to replace it.
2) nmap <LeftMouse> <Leftmouse>i
Or replace the i with an a if you would prefer to append, rather than insert.
You can use behave mswin or so $VIMRUNTIME/mswin.vim so set up a lot of stuff so that it works more like a regular windows program.
The specific setting you are looking for are:
set select=mouse
this causes the mouse to start select mode instead of visual mode. Select mode causes the selection to be deleted and insert mode to be entered when a printable key is pressed.
As in Prince Goulash's answer
nmap <LeftMouse> <LeftMouse>i
will turn on a sort of click-to-type mode.

How to map Menu key ("Application key") to Escape key in vim?

I think that using Menu key to quit vim's insert mode would be a great thing. It would be also nice to use Super key for that, but I'm not sure if it possible since Super key is a modifier.
Anyway, I couldn't find anything related to this. Looking for your help and thanks in advance!
I don't think there's any way you can configure Vim to pay attention to the Menu key as such,
but depending on your system there are various ways to turn the Menu key into an Escape key.
If you're using X11 on Linux:
The command xmodmap -e 'keycode 135 = Escape' will turn your Menu key into an Escape key for the current session, but is not permanent. To make it permanent under Gnome, you might try adding it under System → Preferences → Startup Applications.
The xkeycaps program will give you a GUI for similar remappings.
For more information:
http://46dogs.blogspot.com/2008/05/remap-keys-in-ubuntu-804-hardy-heron.html
http://ubuntuforums.org/archive/index.php/t-106209.html
I haven't found a way to map it in gVim yet, but I was able to successfully map the Menu key in a urxvt+screen+vim stack by the following method:
In a terminal, type Ctrl+v and press Menu. This is mapped to the quoted-insert function in Zsh and in Readline-based tools like Bash.
It will generate an escape sequence like ^[[29~ at the prompt. (The initial ^[ must be translated to <Esc> for use in .vimrc.)
Open up .vimrc and add a line like this:
imap <Esc>[29~ <Esc>
(or imap <Esc>[29~ <Esc><Esc> if you don't want it to wait for further input like the Escape key does.)
Note that not all keys return something usable from Ctrl+v. This is a limitation of terminal emulators and can be remedied by remapping the key. You may be able to do that at the level of the terminal emulator rather than for all X apps.
For example, for urxvt, I had to add the following lines to ~/.Xresources and run xrdb -merge ~/.Xresources to apply them:
! Unbreak zsh keys in URxvt
URxvt*keysym.Home: \033[1~
URxvt*keysym.End: \033[4~
(\033 is <Esc> in ~/.Xresources syntax.)
Just try using ctrl+[ instead of binding another key. This combination is a standard one in vim, btw.
This is even easier when having rebound capslock into an additional ctrl.

How to avoid constant switching to and from English keyboard layout to type Vim commands when writing in non-Latin language (e.g., Greek)?

I am giving Vim a serious try for coding my Python apps.
However, Vim is proving to be so flexible, I was thinking to use it as my main editor at work for writing lawyer/legal documents. The problem is that my mother tongue is not English but Greek. So, I have mapped Alt+Shift to change between English and Greek keyboard layouts.
The issue I am experiencing is that I have to press Alt+Shift each time I want to enter a Vim command (to return back to English). So it is Alt+Shift when I type my document, then Alt+Shift again to enter Vim commands. This defeats the purpose of using Vim, at least in terms of speed of use.
So my question is simple: Is there any way to avoid those frequent Alt+Shift keyboard layout changes just for entering Vim commands when writing in a non-Latin language (e.g., Greek)?
This problem can be solved with the help of the keymap option.
It allows to define an alternate keyboard mapping to use in modes
requiring text input.
To switch between the default and alternate keymaps while in Insert,
Replace, or Command-line mode (but not Normal mode), use
Ctrl+^ (Ctrl+6).
Changing the keymap affects text input only; keyboard behavior in
Normal mode stays the same regardless of the current keymap setting.
One can leave Insert mode writing in Greek and immediately use
Normal-mode keybindings without switching to a different keyboard
layout. If one then returns to Insert mode or, for example, starts
a search by typing /, Vim switches the keymap back to Greek
automatically.
The current keymap used in those text-entering modes is remembered
between switchings to other modes. The only exception from this
behaviour is made for Command-line mode which always starts with the
default keymap, since most of the time it is required to type an Ex
command (in ASCII). With the keymap option set, user is supposed
to work in Vim keeping system keyboard layout set to English while
switching Vim keymap with Ctrl+^ (instead of
the system-wide layout switch).
To enable, say, the UTF-8 Greek keymap permanently, add the following
line to your .vimrc file.
:set keymap=greek_utf-8
There are many predefined keymaps for a large set of languages, you
can browse them all in Vim itself using :e $VIMRUNTIME/keymap. Note
that usually there are several keymaps provided for one language which
differ only by character encoding, so that anybody could choose one
that suits their configuration.
I also recommend setting the options below to specify whether the
keymap should be enabled by default in Insert mode and when entering
a search pattern:
:set iminsert=0 imsearch=-1
See :help iminsert and :help imsearch for the detailed explanation.
There is also a special language mode that, if I am not mistaken,
was introduced in Vim earlier than keymap. It allows to achieve
the behaviour similar to the one provided by keymap through manually
specifying letter pairs that correspond to the keys on keyboard in
a long string to be saved in the langmap option. Personally—my
native language is not English, too—I prefer (and recommend) using
the keymap way instead.
In conclusion, I should emphasize that all of the above is equally
applicable to any other language Vim has (or can be configured to
have) a keymap for.
See also my answer to a similar question ‘Vim “annoyance”
with keyboard layouts’ that has been asked since I originally
gave this answer.

Resources