cursor not going to end of line in vim - linux

I installed Ubuntu recently and fiddled around with profile preferences in terminal. Now when I edit in vim, the cursor doesn't go to the end of a line using navigation keys (In normal mode. '$' doesn't work either). However, it does show itself correctly in insert mode. This is merely a nuisance, but I'd rather be not having it. How to correct this?

Virtualedit permits the cursor to move past the end of line. Try :set ve+=all or :set ve+=onemore. For more information see:
:h ve. These settings can be made default by adding corresponding command without : to your ~/.vimrc file.

This is the correct behavior in vim. The cursor will only go to the last character unless in insert mode.

I added this to my vimrc and it seems to work.
set selection=exclusive
" allows cursor to go to the end of line with g$
set virtualedit+=onemore
noremap $ g$
" Remap VIM 1 to first non-blank character and 2 to the last non-blank character
nnoremap 1 ^
nnoremap 2 g$
" mapping for <End>
map <Esc>[4~ g$

Related

vim move a block of selected code up or down

There is a way to move highlighted code up or down with arrow keys. Even the indentation would be correct on dropping the block.
For the life of me I do not remember how to do it.
Can someone please tell me how to do it.( suitably embarrassed asking this).
Here's the link that'll help you
link
You can move a line, or a block of lines, with the :m command. Examples:
:m 12 move current line to after line 12
To move a block of lines, use the same command but visually select the lines before entering the move command. You can also use arbitrary ranges with the move command. Examples:
:5,7m 21 move lines 5, 6 and 7 to after line 21
why not use d+p if you had chosen the block code
If you want to do it with arrow keys, you can try Tim Pope's vim-unimpaired plugin, which you can find on Github.
Then you can use mappings similar to these (except map them to whatever keys you like).
" ^U moves lines up, ^D moves lines down.
" Works in normal or visual select modes.
nmap <C-u> [e
nmap <C-d> ]e
vmap <C-u> [egv
vmap <C-d> ]egv
Cutting a block of text in visual mode and then pasting:
Go to command mode by pressing Esc if you're not already in it
Move to the start or end of where you want to go
Press V to switch to visual mode
Move with arrows or H,J,K,L to the other end of the block you want to move
Press D to cut it, it will switch back into command mode
Move to where you want to put it and press P to paste what you cut
It seems like a lot of steps but it's just a verbose explanation.
Here is my configurations, There is a nice configuration that I am using to map Ctrl+J to move a block of code up and Ctrl+K to move a block down. I am using currently NVIM but I think the same configuration setting is applicable for VIM as well.
Setting on NVIM
The location that I am using on macOS Catalina for the NVIM is located at:
.config/nvim/init.vim
nnoremap K :m .-2<CR>==
nnoremap J :m .+1<CR>==
vnoremap K :m '<-2<CR>gv==gv
vnoremap J :m '>+1<CR>gv=gv
for vim I think you can change the setting on
.vimrc located under the home directory.

VIM: Inserting New Line then Pressing Escape Moves Cursor to Beginning of Line

When I am editing a python file, after I insert a new line then press <ESC>, the cursor moves all the way to the beginning of the line (ie. column 0). How do I stop this behavior? It's really annoying when I want to paste something on the new line.
Here are my relevant .vimrc settings:
set softtabstop=2
set expandtab
set shiftwidth=2
set smartindent
If your goal is to paste something on the new line after just starting it using o or O, you do not have to enter normal mode for it.
Just type Control-R " to paste from the default register directly from insert mode.
More generally, you can type Control-R <RegisterName> to paste from that register from insert mode.
If you edit the line (e.g. by pressing SpaceBackspace) before pressing Esc then Vim will leave the indentation intact.
From normal mode, a quick == will indent the line to whatever position it should be at using your indentation settings. At least, that's how it's working for me. My relevant indent section in my vimrc:
if has("autocmd")
filetype plugin indent on
endif
You can add mapping so when the auto indentation is triggered a single character is inserted and then deleted:
:nnoremap o ox^H
:nnoremap O Ox^H
:inoremap <enter> <enter>x^H
^H must be introduced by pressing ctrl+vctrl+h.
So do not copypaste.
If you already pressed Esc or are coming back to the line later, it is very easy to get the indent back for editing or pasting.
Just use cc to edit the line, re-applying the automatic indent and landing you in insert mode. Then you can start typing or use CTRL+R+ for example to paste from the system clipboard from the current cursor position.
Note Vim will remove the indent again if you stop editing the line without entering any text. This is intentional as it prevents wasteful trailing whitespace littering the file.

move the text after the cursor to a new line

I am Vim newbie, and I'm using MacVim on OSX Snow Leopard. One of the most common actions I have to take is to move the cursor to a new line but also move the text after the cursor to the new line. I know that pressing 'o' in normal or visual mode moves the cursor to a new line and switches the mode to insert.
What I'd like to do is move the cursor to a new line, and move the text after the cursor to that new line as well, preferably staying in the normal mode? Is this possible? How can I accomplish this task?
If the cursor is on a <space> as in ([] marks the cursor):
lorem ipsum[ ]dolor sit amet
the simplest is to do r<CR>, that is "replace the current character with a linebreak".
Otherwise, use #knittl's solution.
So you want to move everything in the current line, which comes after the cursor to the next line? Read: insert a line break??
(move cursor)
i (or a)
<return>
<esc> (or ^C)
To map this sequence of keystrokes to a single key, follow #thb's suggestion and use the :map command:
:map <F2> i<CR><ESC>
:map <F2> i<CR>
This keeps vi in insert mode.
As I answered in this post, How do I insert a linebreak where the cursor is without entering into insert mode in Vim?.
Please try Control + j.
The code below achieves the same behavior as "normal" editors (for the lack of better terms on the top of my mind) except that you'd have to press "enter" twice instead of once.
I also wanted to get rid of the space if it's right before my current character.
There might be an easier way and I totally welcome edits :-)
" in ~/.vimrc or ~/.vimrc.after if you're using janus
nnoremap <cr><cr> :call ReturnToNewLine()<cr>
function ReturnToNewLine()
let previous_char = getline(".")[col(".")-2]
" if there's a space before our current position, get rid of it first
if previous_char == ' '
execute "normal! \<bs>\<esc>"
endif
execute "normal! i\<cr>\<esc>"
endfunction
This remaps pressing enter twice to going to insert mode, placing a carriage return and escaping.
The reason I'm using this mapping (enter twice) is because I was used to this functionality with other text editors by pressing a enter; also, typing enter twice is fast.
Another thing that I found useful in this context was allowing vim to move right after the last character (in case I wanted to move the last character to a new line). So I have the following in my ~/.vimrc as well.
set virtualedit=onemore
Note that I'm using nnoremap (normal mode non-recursive) instead of map (which is VERY dangerous) (check this out for more information on the differences http://learnvimscriptthehardway.stevelosh.com/chapters/05.html)
You need to map some keys to do a line break at the cursor,
I found the following mapping easy to use, just go to your vimrc and add this line:
:map <silent> bl i<CR><ESC>
to assign a line break at cursor to "bl" combo

Indenting entire file in Vim without leaving current cursor location

I already know that gg=G can indent the entire file on Vim. But this will make me go to the beginning of the file after indent. How can I indent the entire file and maintain the cursor at the same position?
See :h ''
This will get you back to the first char on the line you start on:
gg=G''
and this will get you back to the starting line and the starting column:
gg=G``
I assume the second version, with the backtick, is the one you want. In practice I usually just use the double apostrophe version, since the backtick is hard to access on my keyboard.
Add this to your .vimrc
function! Preserve(command)
" Preparation: save last search, and cursor position.
let _s=#/
let l = line(".")
let c = col(".")
" Do the business:
execute a:command
" Clean up: restore previous search history, and cursor position
let #/=_s
call cursor(l, c)
endfunction
nmap <leader>> :call Preserve("normal gg>G")<CR>
You can also use this on any other command you want, just change the argument to the preserve function. Idea taken from here: http://vimcasts.org/episodes/tidying-whitespace/
You can set a bookmark for the current position with the m command followed by a letter. Then after you run the indent command, you can go back to that bookmark with the ` (backtick) command followed by the same letter.
In a similar spirit to Alex's answer I use the following mapping in vimrc.
nnoremap g= :let b:PlugView=winsaveview()<CR>gg=G:call winrestview(b:PlugView) <CR>:echo "file indented"<CR>
by pressing g= in normal mode the whole buffer is indented, and the scroll/cursor position is retained.
Following on top of Herbert's solution, the reader can also use <C-o>
In vim script
exe "norm! gg=G\<C-o>"
Or mapping
:nnoremap <F10> gg=G\<C-o>

Vim oddities in keymapping

I like to insert blank lines without entering insert mode and I used this keymapping:
nomap go o <esc>
This does create the blank line but introduces some weird behaviour. I have smart indent and autoindent set. The new line follows the indents but doesn't remove them even though doing so manually automatically removes the redundant whitespace. It also adds a single whitespace where the cursor is each time.
Anyone have any insights as to explain this behaviour?
Vim is very literal with how you write your mapping commands - it's actually processing the space in your mapping before it does the <ESC>. In other words, your mapping does this:
nnoremap go o<SPACE><ESC>
You should change it to:
nnoremap go o<ESC>
And make sure you don't have any extra spaces in the mapping!
I agree with "too much php".
This is the relevant section from my .vimrc
nnoremap <A-o> o<ESC>k
nnoremap <A-O> O<ESC>j
I think it's faster since you get the cursor back at the original line (Although not on the original character).
As usual, the vim wiki has a useful tip: Quickly adding and deleting empty lines. The trick is to set paste before adding the new line and afterwards set nopaste. Additionally, this will set a mark to remember the cursor position and jump back to where you were.
nnoremap go :set paste<CR>m`o<Esc>``:set nopaste<CR>
nnoremap gO :set paste<CR>m`O<Esc>``:set nopaste<CR>

Resources