2

I'm coming to Vim from VSCode. So far, it's been great although I've noticed I have some muscle memory movements that I've had trouble moving to Vim.

When I write something like this (cursor is denoted by the underscore: _):

updateCollections: collectionMap => {_}

And I hit enter, in VSCode a new indented line is inserted and the closing bracket is shifted below:

updateCollections: collectionMap => {
  _
}

However, when I hit enter in Vim, the closing bracket remains on the right side:

updateCollections: collectionMap => {
  _}

It's not a big deal, but kind of forces me to hit enter, and use my arrow keys to go up which leads me to the beginning of the line, so I have to keep pressing the right arrow key until I'm in the right position.

Is there a setting or command to automate this? I already have smartindent and autoindent on.

Below are my vim settings:

" VIM General
" ==================================
let mapleader=","
" nmap <silent> gd <Plug>(coc-definition)
set smarttab
set cindent
set autoindent
set smartindent
set nocompatible
set relativenumber
set splitright
set splitbelow
syntax on
set number " show line number
set cursorline " highligh current line
highlight Normal ctermbg=None
highlight LineNr ctermfg=DarkGrey
colorscheme onedark " Set color scheme
set hlsearch " Highlight matching search patterns
set incsearch " Enable incremental search
nnoremap <Leader><space> :noh<Enter> 
set showmatch " Show matching brackets.
set ignorecase " Do case insensitive matching
set smartcase " Include only uppercase words with uppercase search term
set tabstop=2
set shiftwidth=2
set encoding=UTF-8
set showmode
set encoding=utf-8
set termguicolors
set t_Co=256
set background=dark
imap jj <Esc>
1

My recommendation to get this resulting effect is:

  1. Right after typing { at the end of the first line, press "Enter", you will get something like below, with the cursor on an indented line:
updateCollections: collectionMap => {
    _
  1. Then, and only then, press }. This will unindent the line and you'll get this, with the cursor on the }:
updateCollections: collectionMap => {
}
  1. Press "Esc" to leave Insert mode.
  2. Type O to start insert mode on a line above the current one. As long as 'cinkeys' (or 'indentkeys') includes O (it does by default), the inserted line will be indented, and you'll end up with the desired:
updateCollections: collectionMap => {
    _
}

If you already have the {} pair, then the auto-indentation will not always help you with the }.

You will normally get the } properly unindented to match the statement where the opening { is if you set a proper filetype and you use an indent plug-in for that language. (At least for filetype=c the indent plug-in will get this right.)

If that's not the case and the } is indented, then I'd use Ctrl+D (in Insert mode) to unindent it back to match the opening line.

Then, I'd use the same "Esc", O technique to insert an indented line inside the block.


One more handy shortcut here is that instead of "Esc", O to insert a line above the }, you can also use Ctrl+O followed by O. The Ctrl+O key allows you to enter a single normal mode command from insert mode. But if that command (in this case O) is bringing you back to insert mode, that's the same thing as the original sequence.

You might find typing Ctrl+O, O easier than the sequence with "Esc", since you're pressing the same key twice, only with different modifiers (first Ctrl, then Shift.) If you don't find that easier, then just use the original sequence "Esc", O.


If you want to consider using a plug-in to help you here, then take a look at auto-pairs.

With auto-pairs, when you type {, you'll get {} with the cursor inside the pair. If you then type "Enter", you'll get the closing bracket on a line of its own, with the right indentation, and the cursor on an indented line inside the block.

In other words, when using that plug-in, just typing "Enter" on your original situation will get you to your desired result right away!

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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