I am looking for a way to create a newline with an indentation equal to the current column position of my cursor.

Here is an example. Say I'm editing some line of code looking like this, with my cursor on the opening bracket [:

data Foo a = Bar a | Baz [a]

I'd like to have a command that act a bit like o but instead of creating an empty line, it would create an indented line and leave me right under the opening bracket [, allowing me to directly type e.g. (a,a) to obtain

data Foo a = Bar a | Baz [a]

Please note this is not something that can be automated via the smartindent option since there isn't only one interesting indentation (depending on what I want to do, the pipe or the beginning of the line could also be interesting positions to be at; forcing one of those via smartindent wouldn't solve my problem in a general way).


To do this manually you probably just open a new line below the current and insert a bunch of whitespace. You can create a command that does this, and a convenient map to execute the command. Try something like:

nnoremap <LocalLeader>o :OpenIndentToCursorCol<CR>
command! OpenIndentToCursorCol call append('.', repeat(' ', getcurpos()[2] -1)) | exe "normal j" | startinsert!

This calls the append() function to insert a line after some other line. This function is passed two arguments: 1) the line after which the new line should be inserted, 2) the content of the new line. The first argument used is '.', which is the current line. The second argument is the whitespace to achieve the indendation, and it is in turn built with a call to the repeat() function, which repeats an expression n times and returns the concatenation. The expression is a single white space and the number of times is the column in which the cursor resides, minus 1. The current column is retrieved as the third value in the list returned by the getcurpos() function (getcurpos()[2]). Next we use :execute to execute a normal mode command j, to move the cursor to the new line. (There is probably a less verbose way to do this, but none comes to mind at the moment.) Finally enter insert mode with :startinsert!. The bang (!) means after cursor: startinsert! is to startinsert what a is to i and A is to I.

This will leave you with a new line inserted containing white space to the number of the cursor column minus one, with the cursor after the last character of the new line, and in insert mode.

See help topics:

  • :help append()
  • :help repeat()
  • :help getcurpos()
  • :help :execute
  • :help :startinsert
| improve this answer | |

As a mapping:

:map ,, :let @"=repeat(' ',col(".")-1)."\n"^Mpi

^M is produced in vim by Ctrl-vCtrl-m, which is the same as the more usual left-angle cr right-angle which this cock-a-mamie editor refuses to show.

| improve this answer | |

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.