Move to the beginning of New Line - VIM - vim

while in insert mode in vim how do i move to a new line correctly indented.
In many editors this action would be CTRL+ENTER
There is a similar stackoverflow question here however this answer takes you back to the start of the current line SO start of current line
So assuming this is my code, my cursor is just at the T in POST and i want to go to the start of the next line or next line with correct indentation if its a function(using snippets for function so now great concern).
#app.route('/add', method=['POST | '])
expected result
#app.route('/add', method=['POST'])
|

I would use <esc>o. Assuming you have filetype plugin indent on in your vimrc o will automatically go to the correct indent level.

Search for indentation and map to some keys, so you cannot search for every time.
/^^I
^I is tab

Related

Move to the last character of previous line vim

I have started using vim recently and was wondering if there's any keyboard shortcut to move the the last character of previous line( line number l-1 if I am currently on line number l) ?
One of the ways is to use the up arrow to move to the same column of previous line and then use $ to move to the end of the line.
I am looking for some shortcut to do that in one command.
UPDATE:
Here's also another solution to move to the beginning of the next line / the end of the previous line without the need to set any mappings. Add these three to your .vimrc:
set whichwrap+=<,h
set whichwrap+=>,l
set whichwrap+=[,]
(Credit to: Kevin H. Lin and garyjohn)
Original Answer:
I've not memorized all Vim shortcut combinations, so there might be one shortcut for what you're asking for, but I usually define a mapping whenever I need something I don't know how to do.
For what you need, you can simply define it with this:
nnoremap <S-L> <Up><S-4>
Just add it to your ~/.vimrc(if this file doesn't exist yet, create it yourself), then restart vim. Then the next time you open up your Vim, the "Shift-L" shortcut will do the job.
You can go straight into the insert mode as well and append characters after the last character of the previous line with this rule instead:
nnoremap <S-L> <Up><S-A>
Also in case you don't understand the structure of the above rules, you can read more about it here:
Understand Vim Mappings and Create Your Own Shortcuts.
There is a corner case: If your cursor is on the first line, pressing this mapping should not move the cursor.
Therefore, we can use the <expr> mapping:
nnoremap <expr> <F6> line('.')==1?'\<F6>':'k$'
In the example above, I used <F6>, you can choose the short-cut key you like.

Make sure all indents in a block of text are the same in vim

I am having the hardest time indenting things consistently in vim, and get no end of IndentationError in python. Even setting all these tab-related settings: https://stackoverflow.com/a/234578/651174 makes things no better.
How would I do the following in vim to normalize all indentation for a block of text? https://gyazo.com/a333f05e8b4d8034967029005f77ea27
I've tried doing the ctrl v visual mode, but can't seem to figure out how to use that for my purpose, or if there's something else I should be doing here. What would be the best way to do the above?
use visual mode to select a block of text and hit '=' to indent the code correctly. To effect the whole file use gg=G, which translates to go to the beginning of the file and indent correctly to the bottom of the file.
Put the following in your vimrc and use :ReformatFile for easy re-indenting.
function ReformatFile()
exec "normal! mqHmw"
exec "normal! gg=G"
exec "normal! 'wzt`q"
endfunction
command ReformatFile silent call ReformatFile()
mqHmw marks your current position and and the top of your current window see :h marks
"'wzt`q" moves your cursor to the mark w, moves the line to the top of the file and than to the line and column of mark q
Sry for my bad english, but try to type out the commands in vim and read the docs. The marks q/w are chosen arbitrary.

Vim: Smart indent when entering insert mode on blank line?

When I open a new line (via 'o') my cursor jumps to a correctly indented position on the next line. On the other hand, entering insert mode while my cursor is on a blank line doesn't move my cursor to the correctly indented location.
How do I make vim correctly indent my cursor when entering insert mode (via i) on a blank line?
cc will replace the contents of the current line and enter insert mode at the correct indentation - so on a blank line will do exactly what you're after.
I believe that the behaviour of i you describe is correct because there are many use cases where you want to insert at that specific location on a blank line, rather than jumping to wherever vim guesses you want to insert.
Well this actually wasn't as bad as I thought it would be. One way to enable this is to add the following to your ~/.vimrc
"smart indent when entering insert mode with i on empty lines
function! IndentWithI()
if len(getline('.')) == 0
return "\"_ccO"
else
return "i"
endif
endfunction
nnoremap <expr> i IndentWithI()
It simply checks for an empty line when you hit 'i' from insert mode. If you are indeed on an empty line it will delete it and open a new one, effectively leveraging the working 'open line' behavior.
Note: "_ before the cc makes sure that your register doesn't get wiped
On an empty line, to enter insert mode correctly indented, you can simply use s.
Note that s is a synonym for cl, so if you're not actually on an empty line, it'll end up deleting a single character and not indenting. In that case, you're better off using cc, as sml suggested some 18 months ago. But I've frequently improved my score at VimGolf by using this shortcut, so thought I'd mention it. ;)

how to put the current line under edit on the top of the screen in vim

It is very inconvenient when the line I'm editing is at the bottom of the screen . Because you can't see any following lines and that prevent you from take any "eye refernce" to the content of the file following the current line .
so my question is there a vim command can refresh the display and put the current line under edit to on the top of the screen ? thanks in advance.
See this reference.
z<return>
:h scroll-cursor
Jonathan's answer is valid, however I prefer zt (as "top") that keeps the cursor where it was. In the same idea, there is zb (as "bottom") and zz (not as "middle", but that puts the current line at the center of the window).
There is also an option to make vim always show context around the current line:
:set scrolloff=5
(or, :se so=5) will show +/−5 lines around your current line. From vim's help about 'scrolloff':
Minimal number of screen lines to keep above and below the cursor. This will make some context visible around where you are working. If you set it to a very large value (999) the cursor line will always be in the middle of the window (except at the start or end of the file or when long lines wrap).

vimrc configuration

I'm sorry if my title seems vague. I wasn't sure how to make a succinct title.
I have 2 questions:
1) For tab movement in vim, when I map ctrl+pgup to ctrl+p and ctrl+pgdown to ctrl+n, it works fine moving between tabs but each time I move from a tab to different tabs and come back to said tab, the cursor moves one space to the right (it goes to the next line if it meets the end of the line)
The mapping looks like this in my .vimrc file
map ^N ^[[6;5~
map ^P ^[[5;5~
2) I want to check if the colorscheme is a certain one and if so then do sth. eg I want to do this:
if &colorscheme == desert256
highlight String ctermfg=217 ctermbg=235
endif
The answer is on https://superuser.com/questions/103293/vimrc-configuration

Resources