Insert Mode Mappings on a Mac

5 views
Skip to first unread message

Leandro N. Camargo

unread,
Dec 19, 2008, 6:26:48 PM12/19/08
to vim_use
I'm not having success to make these two key mappings to work:

noremap <silent> <C-,> <Esc>:call cursor(line('.'), 1)<CR>i
noremap <silent> <C-.> <Esc>:call cursor(line('.'), col('$'))<CR>i

Since my cmd+arrow(left/right) shortcuts doens't work inside my bash
terminal (I don't have Home/End keys on my macbook) I've tried to make
this workaround at least for vim.

I've put both inside my .vimrc (and loding into my vim using :source)
and I wasn't succeeded.
I've tried imap and inoremap there as well.

Anyone know what is the catch?

Best regards,
Leandro.

Marc Weber

unread,
Dec 19, 2008, 6:53:45 PM12/19/08
to vim...@googlegroups.com
Hi Leandro,

> I'm not having success to make these two key mappings to work:
>
> noremap <silent> <C-,> <Esc>:call cursor(line('.'), 1)<CR>i
> noremap <silent> <C-.> <Esc>:call cursor(line('.'), col('$'))<CR>i

What are you trying to do?
You're not using *i*noremap. Thus those mappings will only work in
normal mode?
Then your action is putting the cursor in first col and last col -1
because you're using i instead of a ?
Do you know the mappings gI and A which do exactly what you want (in
normal mode)

I've trouble understanding what causes trouble for you. I see two
sources of problems:
1) lhs side of the mapping <c-,> and <c-.>.
2) the rhs side of the mapping <Esc>:call ...
Have you tried using something else for 1) and 2) to see which of both
doesn't work?

Eg does
noremap A :echo<space>"B"<cr>
print B?
If you have trouble I'd remove the <silent> to get more error messages..

HTH
Marc Weber

Ben Schmidt

unread,
Dec 19, 2008, 7:24:53 PM12/19/08
to vim...@googlegroups.com
> I've trouble understanding what causes trouble for you. I see two
> sources of problems:
> 1) lhs side of the mapping <c-,> and <c-.>.

I reckon that'll be it. Vim only sees a few control keys: A-Z (with no
distinction between shifted and unshifted), @, ^, _, \, [ and ], plus
sometimes control with special keys, such as arrows or function keys. As
you can see, , and . are not among them. This is true even in the GUI
where the OS usually makes it possible to recognise more, because of
Vim's history and internal architecture. Until that's fixed, it simply
isn't possible for Vim to recognise these combinations. Sorry!

Ben.

Tony Mechelynck

unread,
Dec 19, 2008, 9:17:12 PM12/19/08
to vim...@googlegroups.com

Among all thinkable Ctrl-printable key combinations, only those for
"printable keys" in the range 0x40-0x5F are reliably mappable, plus
Ctrl-lowercase letters (because case doesn't matter) and Ctrl-? which
maps to 0x7F. The culprit is not Vim, not even vi, but ASCII which
defined only those combinations, so only these can be seen (as 0x00-0x1F
plus 0x7F) when using "cooked" keyboard input, as Vim does. Of the
Ctrl-unprintable key combinations, some of them can be seen, others not;
and some of them are ambiguous as to the presence or absence of the Ctrl
modifier; it all tepends on your keyboard interface.

On the Mac, there are both a Ctrl and a Cmd modifier; one of them is
represented as <C-something>, the other as <D-something>. If one of them
doesn't work, try the other.


Best regards,
Tony.
--
William Safire's Rules for Writers:

Remember to never split an infinitive. The passive voice should never
be used. Do not put statements in the negative form. Verbs have to
agree with their subjects. Proofread carefully to see if you words
out. If you reread your work, you can find on rereading a great deal
of repetition can be avoided by rereading and editing. A writer must
not shift your point of view. And don't start a sentence with a
conjunction. (Remember, too, a preposition is a terrible word to end a
sentence with.) Don't overuse exclamation marks!! Place pronouns as
close as possible, especially in long sentences, as of 10 or more
words, to their antecedents. Writing carefully, dangling participles
must be avoided. If any word is improper at the end of a sentence, a
linking verb is. Take the bull by the hand and avoid mixing
metaphors. Avoid trendy locutions that sound flaky. Everyone should
be careful to use a singular pronoun with singular nouns in their
writing. Always pick on the correct idiom. The adverb always follows
the verb. Last but not least, avoid cliches like the plague; seek
viable alternatives.

Leandro Camargo

unread,
Dec 19, 2008, 9:30:17 PM12/19/08
to vim...@googlegroups.com

Thanks you all for your thoughts.
Ben is right about the "," and "." keys over key mappings, the problem
was due these keys.
Even regarding Tony thoughts about non-printables chars I'd insist on
using <C-Left> and <C-Right> to do these my desired tasks.
But as I realized they are already created inside vim for tasks I
don't even use.
So my golden question is: can I over these key mappings <C-Left> and <C-Right> ?
Because what I really want is to make something like:

imap <C-Left> <Esc>bi
imap <C-Right> <Esc>wi
imap <C-S-Left> <Esc>:call cursor(line('.'), 1)<CR>i
imap <C-S-Right> <Esc>:call cursor(line('.'), col('$'))<CR>i

So the first two lines would jump words, and the last two lines would
go to line edges.
It's possible? I'd have to overwrite the original key mapping before
anything. So....? What do you think? Doable?

And I tried with the Command key (<D-Left/Right>) here as Tony
suggested me but it seems the vim container process (terminal) hooks
up any key binding starting with the Command key, so it won't work.

Tony Mechelynck

unread,
Dec 19, 2008, 9:56:46 PM12/19/08
to vim...@googlegroups.com
Vim probably won't see Ctrl-comma and Ctrl-dot, or not as distinct from
plain comma and dot. Of course this means you cannot remap them to
cursor movement functions in Insert mode, and that even in Normal mode
you should think twice about overriding some useful commands (such as
the dot for "repeat last change").

For the {lhs} of Vim mappings, I recommend the F keys, with or without
Shift, because they are least likely to collide with something else,
with a very few well-known exceptions, such as F1 = help, and, on some
platforms, F10 = Menu.


Best regards,
Tony.
--
ARTHUR: Well, it doesn't matter. Will you go and tell your master that
Arthur from the Court of Camelot is here.
GUARD #1: Listen, in order to maintain air-speed velocity, a swallow
needs to beat its wings 43 times every second, right?
ARTHUR: Please!
The Quest for the Holy Grail (Monty
Python)

Marc Weber

unread,
Dec 19, 2008, 10:02:08 PM12/19/08
to vim...@googlegroups.com

You still haven't corrected this i (should be a). Using <esc>A is
much shorter anyway :-) Thus just use:
> imap <C-S-Right> <Esc>A

On linux you can do something like
xmodmap -e 'clear Lock' -e 'keycode 0x42 = Escape'
to map capslock to <esc>. Then typing <caps>b is even more convinient
than using the default ctrl-left and ctrl-right combos. Your <esc>
mapping stuff won't work with visual mode. So do yourself a favour,
read :h motion once to see what mappings are availible. Repeat this once
a month till you feel you know most mappings you really need. You'll
find mappings such as tT fF (and using ; and , (repeat last movement)
which are much better than ctrl- quirks. And you'll be able to use them
in visual mode as well.
You'll be more precise with vim mappings as well. Example:
Lookup the difference e E w W. Which behaviour do you want for c-right?

I know its hard to learn something you're not used to. Put it will pay off soon
if you spend some time within vim.

At least this is what I would do.. But it depends on how much time
you'll spend coding or editing text in your life.

my 2 cents again :-)

Sincerly
Marc Weber

Leandro Camargo

unread,
Dec 19, 2008, 10:22:43 PM12/19/08
to vim...@googlegroups.com
Yeah...following a simpler idea, it'd become something like these:

imap <C-Left> <Esc>bi
imap <C-Right> <Esc>wi

imap <C-S-Left> <Esc>A
imap <C-S-Right> <Esc>I

On Sat, Dec 20, 2008 at 1:02 AM, Marc Weber <marco-...@gmx.de> wrote:
> You still haven't corrected this i (should be a). Using <esc>A is
> much shorter anyway :-) Thus just use:
>> imap <C-S-Right> <Esc>A
>
> On linux you can do something like
> xmodmap -e 'clear Lock' -e 'keycode 0x42 = Escape'
> to map capslock to <esc>. Then typing <caps>b is even more convinient
> than using the default ctrl-left and ctrl-right combos. Your <esc>
> mapping stuff won't work with visual mode. So do yourself a favour,
> read :h motion once to see what mappings are availible. Repeat this once
> a month till you feel you know most mappings you really need. You'll
> find mappings such as tT fF (and using ; and , (repeat last movement)
> which are much better than ctrl- quirks. And you'll be able to use them
> in visual mode as well.

Yeah, believe me...I'm a shortcuts addict, so I try hard to learn the
most useful of them in vim.
But I don't know if you got the main idea on this, which is:
* having shortcuts in *insert mode* for jumping a word (forward and
backward) and going to the current line edges, without changing the
mode by hand (like typing ESC), because I want something with lower
cost to my hands like Ctrl + something in insert mode.

And mapping ctrl + arrows would give me a very good user experience
for its ease of use.

> You'll be more precise with vim mappings as well. Example:
> Lookup the difference e E w W. Which behaviour do you want for c-right?

Yeah, I understand.
That's why I really meant w for word forwarding and b for word backwarding.

> I know its hard to learn something you're not used to. Put it will pay off soon
> if you spend some time within vim.

Sure. I'm in that philosophy too. I use to spend a precious time
digging vim features and any other tools that could save me some
valuable time in long term.

Tony Mechelynck

unread,
Dec 19, 2008, 11:07:53 PM12/19/08
to vim...@googlegroups.com

IIUC, Ctrl-Left and Ctrl-Right to move left/right by words are among the
default key bindings of Vim. If they don't work when you hit them, not
even after loading Vim with "-N -u NONE" (without quotes) on its (shell)
command-line, then there is something wrong about how these keys are
passed to Vim. Maybe your terminal or window manager doesn't pass them
at all, or maybe it strips away the Ctrl modifier, or maybe it doesn't
respect its own termcap/terminfo table, I don't know. Here are a few
troubleshooting hints:

- What exactly happens when you hit Ctrl-Left (on some text page) after
starting Vim with -N -u NONE
- Does it work (or not work) the same way when you start Vim normally?
- What is the answer to :echo "\<C-Left>" (with the quotes)?
- What happens in Insert mode when you hit Ctrl-V followed by Ctrl-Left?
(If you have remapped Ctrl-V to Paste, use Ctrl-Q instead).
- The same with Ctrl-Right instead of Ctrl-Left
- All the above in gvim and in Console Vim (or if you have only one of
them, then which one?)
- What are the first five lines or so of the ":version" output? (until
the line ending "Features included (+) or not (-):"? (See :help :redir
about how to capture that output. In particular, ":redir @+" [without
quotes] starts redirection to the clipboard.)

Ctrl-Shift-Left and Ctrl-Shift-Right normally mean "the same as without
Shift, but also start Select mode". (This is under control of the
'selectmode' and 'keymodel' options.) Do they? If they do, you can remap
them, but of course if you do you'll lose the default bindings.


Best regards,
Tony.
--
The very powerful and the very stupid have one thing in common.
Instead of altering their views to fit the facts, they alter the facts
to fit their views ... which can be very uncomfortable if you happen to
be one of the facts that needs altering.
-- Doctor Who, "Face of Evil"

Andrew Long

unread,
Dec 20, 2008, 4:03:18 AM12/20/08
to vim...@googlegroups.com
On 20 Dec 2008, at 04:07, Tony Mechelynck wrote:

>
><snip/>


> IIUC, Ctrl-Left and Ctrl-Right to move left/right by words are among
> the
> default key bindings of Vim. If they don't work when you hit them, not
> even after loading Vim with "-N -u NONE" (without quotes) on its
> (shell)
> command-line, then there is something wrong about how these keys are
> passed to Vim. Maybe your terminal or window manager doesn't pass them
> at all, or maybe it strips away the Ctrl modifier, or maybe it doesn't
> respect its own termcap/terminfo table, I don't know. Here are a few
> troubleshooting hints:
>

On my Mac (Leopard 10.5.6), CTRL=Left, Right, Up or Down are grabbed by
Spaces to move the viewport between diffeent virtual screens...

Regards, Andy

--
Andrew Long
andrew dot long at mac dot com

Tony Mechelynck

unread,
Dec 20, 2008, 4:49:28 AM12/20/08
to vim...@googlegroups.com

Hm, I see. On my Linux system with KDE, Ctrl-Fn is grabbed by the KDE
window manager to move the focus to virtual desktop n. (I have 20 of
them but of course only the first 12 are accessible that way; for the
others, I can use Ctrl-Tab or Ctrl-Shift-Tab to move to next or
previous, or the mouse to click the appropriate desktop icon on the
taskbar.) Ctrl-arrow keys are not grabbed, and many applications use
them to move the cursor (Ctrl-Left and Ctrl-Right to move horizontally
by words, Ctrl-Home and Ctrl-End to go to top or bottom of a document,
etc.) -- lucky me!


Best regards,
Tony.
--
There is something fascinating about science. One gets such wholesale
returns of conjecture out of such a trifling investment of fact.
-- Mark Twain

sc

unread,
Dec 20, 2008, 11:14:33 AM12/20/08
to vim...@googlegroups.com

if your mouse has a wheel another method of changing
desktops is to roll the mouse-wheel while the pointer is on
the desktop

lucky us!

sc


Tony Mechelynck

unread,
Dec 20, 2008, 11:37:08 AM12/20/08
to vim...@googlegroups.com
On 20/12/08 17:14, sc wrote:
[...]

> if your mouse has a wheel another method of changing
> desktops is to roll the mouse-wheel while the pointer is on
> the desktop
>
> lucky us!
>
> sc

No, it's a plain 2-button mouse (with Emulate3Buttons set but not much
used). No wheel.


Best regards,
Tony.
--
"... one of the main causes of the fall of the Roman Empire was that,
lacking zero, they had no way to indicate successful termination of
their C programs."
-- Robert Firth

Reply all
Reply to author
Forward
0 new messages