I've added the following line un my .vimrc to add a line break when I press enter in normal mode:
"" insert line break in normal mode on Enter
nmap <S-Enter> O<Esc>
nmap <CR> o<Esc>
This works fine except when I want to comment the current line in normal mode by pressing cmd+/ where it comments the current line and add a line break which is also commented.
How can I fix this?
Many thanks
The comments together give the answer; here's the summary:
You see the default formatting behavior when inserting a new line after a commented one. It's caused by the o value in 'formatoptions'. You could modify your mapping to
set formatoptions-=o
But there are alternative approaches for inserting a new empty line:
nnoremap <silent> <S-Enter> :put! _<CR>
nnoremap <silent> <CR> :put _<CR>
(PS: You should use :noremap; it makes the mapping immune to remapping and recursion.)
Also, there are plugins that provide this (and several related mappings):
unimpaired.vim - Pairs of handy bracket mappings
my LineJuggler plugin
Related
In my vimrc, I have a fold marker, a function, and a mapped macro:
set foldmarker=----------,++++++++++++
"Function to switch INTO visual line mode, not just toggle
function SwitchToVisLine()
if visualmode()!="V"
execute "normal! V"
endif
endfunction
"Macro to wrap and fold visually highlighted lines
xnoremap <F3> :call SwitchToVisLine()
\dO
\----------<CR>
\++++++++++<ESC>
\k0pzako
The point is to be able to go from any of the three visual modes and tap <F3> to have the active lines wrapped in the fold markers, folded, and the cursor returned on a new line above the whole mess.
PROBLEM: When using the macro as written, I get the error E488: Trailing characters: dO----------
What's confusing is that when I walk through each keystroke manually, even calling the function to switch from visual or visual-block mode into visual-line mode, I DON'T get this error. It only happens when I run it as a mapped macro.
Any help is much appreciated!
In its current form, your attempt at making a multiline macro looks like this to Vim with the line continuation characters removed:
:call SwitchToVisLine()dO----------<CR>++++++++++<ESC>k0pzako
/\
<CR>
which is incorrect due to the missing <CR> after :call SwitchToVisLine().
It should look like this:
xnoremap <F3> :call SwitchToVisLine()<CR>
\dO
\----------<CR>
\++++++++++<ESC>
\k0pzako
That said, there are quite a few improvement opportunities, here.
The macro could be replaced with two :help :silent :help :put commands:
xnoremap <F3> :sil'<put!='----------'\|sil'>put='++++++++++'<CR>
which:
makes your custom function unnecessary because there is no need to force visual-line mode anymore,
doesn't pollute any register,
is not destructive.
You could use the value of foldmarker instead of repeating yourself:
xnoremap <F3> :sil'<put!=&fmr->split(',')[0]\|sil'>put=&fmr->split(',')[1]<CR>
Ten lines to one, nice.
I have a few lines of codes that are too that I would like to break into 2 lines at certain location.
So instead of moving to the position then presse I to insert mode then Enter to break line then finally ESc back.
Is there way I can do it easier in normal mode only?
Many thanks.
You can define some simple mapping for it:
" <C-Enter> Insert single / [count] newline.
nnoremap <C-CR> i<CR><Esc>
Note that <C-CR> probably only works in GVIM, not in a terminal; choose a different key if necessary.
Here's an additional mapping that keeps the cursor on the original line:
" <C-S-Enter> Append single / [count] newline.
function! s:AppendCRSetPos()
keepjumps call setpos("''", getpos('.'))
return ''
endfunction
nnoremap <expr> <SID>(AppendCRSetPos) <SID>AppendCRSetPos()
nnoremap <script> <C-S-CR> <SID>(AppendCRSetPos)i<CR><Esc>g``
I have the following in my .vimrc
nnoremap S i<cr><esc>^mwgk:silent! s/\v +$//<cr>:noh<cr>`w
Which will split the line on pressing capital S.
Hope this helps
There is a vim key for that:
r<Enter>
This question already has answers here:
Vim command to insert blank line in normal mode
(4 answers)
Closed 8 years ago.
I recently surprised myself wanting to insert blank lines above or below the current line either in normal or insert mode.
I usually add blank lines to my code for better reading or to properly separate blocks of code.
To do this I am using the following in my .vimrc:
" Add empty line above and below the current line
nnoremap <silent> <C-K> mP:pu! _<cr>:']+1<cr>`P
inoremap <silent> <C-K> <esc>mP:pu! _<cr>:']+1<cr>`Pa
nnoremap <silent> <C-J> mP:pu _<cr>:'[-1<cr>`Pa
nnoremap <silent> <C-J> <esc>mP:pu _<cr>:'[-1<cr>`Pa
The only ugly thing with these mapping is I'm using the marker P just to go back to the previous cursor position. I tried to use `` instead but it doesn't do what I expected.
Perhaps there is a slightly better solution otherwise this snippet works quite well for me.
I am slowly adding new mapping in my .vimrc. I noticed the <C-[a-z]> combinaisons are mostly free and the existing ones are pretty useless (i.e. <C-Q>, <C-H>, <C-M>…). Then I decided to bind them to new useful mapping:
<C-N> New file
<C-S> Save (:update!)
<C-P> CtrlP mixed mode
<C-S-P> CtrlPCmdPalette
<C-B> CtrlP buffers mode
<C-D> <C-C>ciw
…
instead of using a named marker, you can use the backtick ' to explicitly add a jump. like:
nnoremap <c-k> m`O<esc>``
nnoremap <c-j> m`o<esc>``
inoremap <c-j> <esc>m`o<esc>``a
inoremap <c-k> <esc>m`O<esc>``a
If you want a robust mapping that also handles [count], my LineJuggler plugin contains ]<Space> mappings (among others). This particular mapping can also be found in the unimpaired plugin.
When the cursor is placed at the end of a line containing nothing but withspace characters, vim will, when i press enter, remove that whitespace. I find this irritating, as it breaks my script for selecting code that are indented to the same level. How can I prevent vim from doing this?
In my .vimrc (http://bjuhn.com/randomstuff/vimrc) I have the following:
filetype plugin on
set copyindent
that is, I am not using any syntax-aware auto-indention, as I have yet to find one that does everything to my liking.
The Vim wiki suggests this:
inoremap <CR> <CR>x<BS>
because the indenting is not removed if some text has been entered on the line, even if it has been deleted.
[EDIT - milimetric]
Just a couple of pieces missing from a full solution. You also need remaps for o and O and whatever else you use to add lines:
inoremap <CR> <CR>x<BS>
nnoremap o ox<BS>
nnoremap O Ox<BS>
Same idea but people newer to vim might not figure it out quickly.
For me this code works:
inoremap <silent> <Esc> <C-O>:stopinsert<CR>
I have these insert mode mappings in my .vimrc file:
imap <C-e> <C-o>A
imap <C-a> <C-o>I
They make Ctrl-A and Ctrl-E move the cursor to the start and end of the line without leaving insert mode, a la emacs keybindings.
However, I just realized that the Ctrl-E mapping introduces a conflict with the autocompletion submode. The documentation in :help complete_CTRL-E states:
When completion is active, you can use CTRL-E to stop it and go back to the originally typed text.
Thus, my Ctrl-E mapping interferes with this. Is there a way that I can make Ctrl-E jump to the end of the line only if auto-completion is not active?
There is no proper way to test whether the
Ctrl+X-completion mode is active or not.
However, the following two workarounds are possible.
1. If one uses the popup menu to choose from the list of available
completions (especially in the case of menuone set in the completeopt
option), an acceptable solution might be the mapping
inoremap <expr> <c-e> pumvisible() ? "\<c-e>" : "\<c-o>A"
2. A general solution can be based on a side effect: In the
completion submode, it is disallowed to enter Insert mode recursively
(see :helpgrep Note: While completion), so if an attempt to do so
fails, we can suppose that we are in the midst of a completion:
inoremap <c-e> <c-r>=InsCtrlE()<cr>
function! InsCtrlE()
try
norm! i
return "\<c-o>A"
catch
return "\<c-e>"
endtry
endfunction