3

This is a usual shortcut in other editors: indent the current line to the right with CMD+].

How can achieve this in MacVim without leaving the insert mode?

3 Answers 3

4

In insert mode you can indent the current line with Ctrl-T and dedent with Ctrl-D. Refer to :help i_ctrl-t

4
  • 1
    And if OP wan'ts to use his old mapping he can use inoremap <C-]> <C-T> in his vimrc to achieve the desired effect.
    – Tumbler41
    Sep 19, 2017 at 20:27
  • 1
    However if you're thinking of also mapping <C-[> don't. <C-[> is unfortunately what Vim sees when <ESC> is pressed so if you remap that, you wouldn't be able to get out of insert mode. :P
    – Tumbler41
    Sep 19, 2017 at 20:35
  • The OP wants to map Cmd (on a mac) not Ctrl. Sep 19, 2017 at 20:52
  • Bleh, I can never keep which key the Cmd key maps to on Windows straight. It's alt right? So it would be <A-]> or <D-]>.
    – Tumbler41
    Sep 19, 2017 at 21:03
2

The rhs of that mapping is difficult because there's no easily reliable way to jump back to the right column.

Two backticks returns to the previous jump point, so that might work, although it's not behaving reliably for me at the moment.

inoremap <D-]> <Esc>>>``i

Alternatively, you could mark your current spot, but that mark stays in the same column, so once you indent, that column is no longer correct (it's off by whatever you have shiftwidth set to). If you always set tabstop and shiftwidth to the same thing (e.g. you always do 2 spaces), you could get around that with something like

inoremap <D-]> <Esc>ma>>`alla

You'd have a similar problem with gi (return to previous insert point) but even worse, because you'd actually be back in insert mode shiftwidth characters before where you want to be, so you'd have to have multiple <Esc>s.

I think a better solution is to properly configure your indenting and then you won't even need to do this. E.g. :h autoindent, :h smartindent and :h cindent

Note that these mappings won't work in terminal vim since Cmd is consumed by the terminal itself and not passed to vim.

EDIT:

Per @NathanWilson's response, this would work:

inoremap <D-]> <C-t>
inoremap <D-[> <C-d>
0

IMHO the correct answer is (as already mentioned)

inoremap <D-]> <C-t>
inoremap <D-[> <C-d>

Of course, you can define the CMD+] mappings also for normal and visual mode:

nnoremap <D-]> >>
nnoremap <D-[> >>
xnoremap <D-]> >
xnoremap <D-[> <
" If you prefer to keep the visual selection (not the vim way)
" xnoremap <D-]> >gv
" xnoremap <D-[> <gv

Default Mappings of Vim

However, note that vim already provides builtin mappings

CTRL-T
CTRL-D

which obviously work everywhere (Linux/Windows/MacOS, gui/cli, etc.).

Secondly, vim is a modal editor where many changes are meant to be done in normal mode and not in insert mode. I consider increasing or decreasing indentation a change most often done in normal mode. Vim provides mappings to change indentation in normal mode with

>> or >{motion} 
<< or <{motion}

Combing > or < with a text-object (:h text-objects) such as ip you can indent larger parts of text with a single command.

Another important vim feature here is the dot-command (:h .) to repeat it: >ip.. means indent paragraph three times.

Transition to Vim's Way

I would use CMD+][ mappings to ease your transition to vim. If you know the vim mappings by heart, you could or should consider removing them. MacVim provides other CMD+ mappings which follow the macOS human interface guidelines (HIG) :h macvim-shortcuts (most importantly CMD+s for saving, CMD+f for finding, CMD+b for building and CMD+x/c/v for cut, copy, paste). However, in my experience when you have become familiar with vim, you do not need them anymore and use vim consistently between platforms. Even though I admit that "+p is not as convenient as CMD+v, but in this case IMHO CTRL-t/d are as good as CMD-]/[.

2
  • If you're just going to add a note, it should probably just be a comment.
    – Tumbler41
    Sep 21, 2017 at 20:51
  • Generally, I agree. However, I prefer to have all important information in answers not in comments or in addendums of answers which do not provide a short and consice answer. What do you think?
    – Hotschke
    Sep 25, 2017 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.