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?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityIn insert mode you can indent the current line with Ctrl-T and dedent with Ctrl-D. Refer to :help i_ctrl-t
inoremap <C-]> <C-T>
in his vimrc
to achieve the desired effect.
Sep 19, 2017 at 20:27
<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
Sep 19, 2017 at 20:35
<A-]>
or <D-]>
.
Sep 19, 2017 at 21:03
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>
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
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.
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-]/[
.