Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to go back to previous indentation level in insert mode?

Tags:

vim

Sometimes vim's smartindent does not pick the correct level of indentation for the next line once you hit enter and you'd like to go back to the indentation level of the previous line and just go from there. I know that you can hit ctrl-d a few times to achieve this but it would be more useful for a key that immediately goes to the indentation level of the line above.

like image 954
rgrinberg Avatar asked May 07 '13 05:05

rgrinberg


People also ask

How do I get the indent level of a given cell?

Just download it, activate it within Excel (the simple steps will be described after you click the download button) and type this formula: Let’s say you want to get the indent level of cell A1, just type =PROFEXIndentLevel (A1). Press F9 for refreshing. This function is included in our Excel Add-In 'Professor Excel Tools'

How to set indentation for a numbered/bulleted style?

You cannot define indents for a numbered/bulleted style through Modify Style | Format| Paragraph , and Modify Style | Format | Numbering has no indent settings. You have to go through Define New Multilevel List to link the style to the selected numbering/indents.

How do I backspace to the previous line?

In normal mode, the Backspace acts as h, it just goes to the left. By default, the backspace will go to the previous line if at the start of a line (as if eol was in backspace ); you can control this behaviour with the 'whichwrap' option through the b flag (enabled by default).

How do I find the previous line in gvim?

With these mappings, in normal mode, press Alt-, to find the previous line with the same indent as the current line, or press Alt-. to find the next such line. These mappings should work in gvim, but you may need to choose different keys if working with Vim in a terminal.


2 Answers

I don't know if it's pure coincidence, but Alexey Radev has just published the prev_indent plugin, which provides an insert mode mapping and :PrevIndent command to move the current line to the previous indentation level.

like image 101
Ingo Karkat Avatar answered Sep 18 '22 07:09

Ingo Karkat


If you can't be bothered to install a plugin for such a simple task (I couldn't), try this simple mapping:

:inoremap <C-D> <Esc>:call setline(".",substitute(getline(line(".")),'^\s*',matchstr(getline(line(".")-1),'^\s*'),''))<CR>I

Now CtrlD in insert mode will do the deed: indent the current line like the previous line.

This works best before you start typing on the new line, because it will reset the cursor to just past the indentation.

like image 42
glts Avatar answered Sep 19 '22 07:09

glts