Vim auto line-break - vim

When I'm writing a long line of text in vim (such as a paragraph in latex), it wraps my text into multiple lines which is good. However, if I then try to navigate these lines with 'j' and 'k' (or the up/down arrows) it will skip the entire paragraph. I fixed this problem by highlighting the paragraph and pressing gq. This inserts line breaks at the end of each line.
My question is, is there a way to automate this, so I don't have to keep highlighting text and pressing gq?

You can limit the width of a line with the textwidth option
(see :help tw).
For example, if you want to limit the width to 80 columns, you can use:
:set tw=80
With this option, when you will type something longer than 80 columns, Vim
will automatically insert a newline character.

You need to step back a little and use gj and gk which go down and up inside wrapped lines.
Since gjand gk work exactly the same as j and k in non-wrapped lines you can safely map j or <down> to gj and k or <up> to gk making it all seamless.
-- EDIT --
Yes it doesn't adress Eddy's immediate problem but it solves his original problem (vertical movement in wrapped lines) which led him to a poor workaround that, in turn, put him in this situation.

Related

Remapping display line navigation in vim

I'm currently setting up a .vimrc and remapping my home-row (ie navigation keys) to wormkan. So for example I have
nnoremap n j
nnoremap j n
which works all well and good, but I'm having issues with figuring out how to use display line navigation with this setup.
Basically, if I want to move down one display line on a qwerty layout this would be achieved with gj - so now I want this to happen with gn.
However, gn currently still maps to visual search and gj is still associated with the display line navigation. I thought that operator-pending mode would help here (ie adding onoremap n j and vice versa) but this doesn't have the desired result. Any ideas on what I have to do to get this functioning?
By reading: :help gn
*gn* *v_gn*
gn Search forward for the last used search pattern, like
with `n`, and
start Visual mode to select the match.
If the cursor is on the match, visually selects it.
If an operator is pending, operates on the match.
E.g., "dgn" deletes the text of the next match.
If Visual mode is active, extends the selection
until the end of the next match.
You realize that gn is the motion command. That's why mapping :onoremap n j did not work as expected. What you need instead is :noremap gj gn.

Is it possible to jump to the next wrapped line in vim?

In vim, if there're many lines in a file, I can use hjkl to navigate, I can just press j to jump to the next line.
However, if I am processing a very long line of text, I use set wrap to wrap it into many lines. It is still one single line, but visually looks like many line to a vim user.
In such a case, if one line of text is wrapped into many lines, it is possible to jump between wrapped fake-lines instead of pressing h or l to jump by characters or pressing w to jump by words?
The motion for that is gj. (Or g<any other direction>.)
gj or *gj* *g<Down>*
g<Down> [count] display lines downward. |exclusive| motion.
Differs from 'j' when lines wrap, and when used with
an operator, because it's not linewise.

vim line operations (e.g., dd) on wrapped text

I'm using wrap in vim, but I want vim to otherwise behave as though the lines have actual breaks in them (rather than "soft breaks" for screen rendering).
I've mapped j to gj and k to gk for navigation. However, line operations (such as dd) still act upon the whole line, rather than the "screen line". Is there any way to alter this behavior such that 'dd' is limited to the screen line?
You can create a key mapping:
:nnoremap dd g0dg$
I would rather create a new operator, for example x in operator-pending mode:
onoremap x :norm! g0vg$<cr>
xnoremap x g$og0o
With the first mapping, dx will delete a screen line, yx will yank a screen line (beware, it will not be pasted linewise, but characterwise), cx will delete a screen line and start insert mode, and so on.
With the 2nd mapping, x in visual mode will extend to screen lines the visual selection.
I wouldn't advise remapping dd, because this might break plugins (if they use :normal instead of :normal! or if they use :×××map instead of :×××noremap.

How to wrap the text 5 characters before the end of window?

I would like to wrap the text 5 characters before the end of window (without breaking the line).
I don't know how to do this without putting an EOL character in the text (wrapmargin/textwidth).
You need to set 3 options as follows:
:set linebreak
:set wrap
:set nolist
If any of these are set otherwise (e.g. :set list), then it won't work.
Additionally, there is an option to set a character which will display at the beginning of wrapped lines. e.g.:
:set showbreak=>
If you want to turn that feature off again, set it to an empty string:
:set showbreak=
Note that even though wrapped lines look like lines in their own right, Vim still considers them to be a single line. So moving the cursor down with the j key will move it on to the next numbered line, rather than the next displayed line. This makes better sense if you have line numbers displayed (:set number). If you want to move up and down through display lines, you can use gk and gj instead. Here are a few more g commands that you will find useful:
Numbered line Display line
------------- ------------
j gj
k gk
$ g$
0 g0
^ g^
Maybe the linebreak is the setting you're looking for?
:se linebreak

How to paste in a new line with vim?

I often have to paste some stuff on a new line in vim. What I usually do is:
o<Esc>p
Which inserts a new line and puts me in insertion mode, than quits insertion mode, and finally pastes.
Three keystrokes. Not very efficient. Any better ideas?
Shortly after :help p it says:
:[line]pu[t] [x] Put the text [from register x] after [line] (default
current line). This always works |linewise|, thus
this command can be used to put a yanked block as
new lines.
:[line]pu[t]! [x] Put the text [from register x] before [line]
(default current line).
Unfortunately it’s not shorter than your current solution unless you combined it with some keyboard map as suggested in a different answer. For instance, you can map it to any key (even p):
:nmap p :pu<CR>
Options:
1) Use yy to yank the whole line (including the end of line character). p will then paste the line on a new line after the current one and P (Shift-P) will paste above the current line.
2) Make a mapping: then it's only one or two keys:
:nmap ,p o<ESC>p
:nmap <F4> o<ESC>p
3) The function version of the mapping (unnecessary really, but just for completeness):
:nmap <F4> :call append(line('.'), #")<CR>
" This one may be a little better (strip the ending new-line before pasting)
:nmap <F4> :call append(line('.'), substitute(#", '\n$', '', ''))<CR>
:help let-register
:help :call
:help append()
:help line()
:help nmap
You can paste a buffer in insert mode using <C-R> followed by the name of the buffer to paste. The default buffer is ", so you would do
o<C-R>"
I found that I use <C-R>" very often and bound that to <C-F> in my vimrc:
inoremap <C-F> <C-R>"
This still uses three keystrokes, but I find it easier than Esc:
o<Alt-p>
Since you're in insert mode after hitting o, the Alt modifier will allow you to use a command as if you weren't.
Using this plugin: https://github.com/tpope/vim-unimpaired
]p pastes on the line below
[p pastes on the line above
advantages:
works on all yanked text (word, line, character, etc)
indents the pasted text to match the indentation of the text
around it
2 keystrokes instead of 3 and much "easier" strokes
fast
Personally I've nmapped Enter (CR) like this:
nmap <CR> o<Esc>k
...based on this Vim Wikia article.
This way I can make newlines directly from normal mode, and combining this with wanting to paste to a newline below I'd do:
<CR>jp
You could also skip k in the nmap above, depending on what functionality you prefer from Enter, so it would just be <CR>p.
I've also imapped jj to Esc, which would also assist in this case. Esc is way too far away from the home row for how significant it is in vim.
Not shorter than the other solutions, but I do think it feels less clunky than some of them, and it has other uses too.
If you wanted to stay in the insert mode, you can do o ctrl+o p
o – insert mode and go to the new line
ctrl+o – run a single command
like in normal mode
p – paste
It's three keystrokes but you stay in insert mode and also o ctrl+o is quite fast so I personally treat it as 2.5 keystrokes.
If you're copying a whole line then pasting a whole line, use Y to yank the line or lines, including line break, in the first place, and p to paste. You can also use V, which is visual line mode, in contrast with plain v for visual mode.
I have mapping inoremap jj <ESC>. So it is easy to insert new line with ojj and Ojj and then p.
so ojjp paste new a newline. it have one more stroke then o<esc>p but ojjp is easy for me.
I found an elegant solution to this. If you are putting the yank register in your OS's clipboard (which is great anyway), with
set clipboard+=unnamed
than you can do o<Ctl-v>.
Besides being fewer strokes, this improves on both o<Esc>p and :pu because it preserves indenting: both of the other options start you at character zero on the new line.
Caveat is that this may or may not be OS dependent. All I know is that it works on recent version of OS X, but clipboard is just one of many ways to get yank in the OS clipboard.
If you want to paste in a new line and still keep indentation, create this mapping:
nnoremap <leader>p oq<BS><Esc>p
Prerequisite: you have leader mapped and you have set autoindent in your .vimrc.
Explanation: a new line is created with 'o', 'q' is typed and then back-spaced on (to keep indentation), and 'esc' brings you back to normal mode where you finally paste.
If you also want to end in insert mode, it is possible to paste while in insert mode using CTRL-R ". https://stackoverflow.com/a/2861909/461834
Still three keystrokes, but no escape, and you save a keystroke if you want to end in insert anyway.
I use the following mapping in my Neovim config:
nnoremap <leader>p m`o<ESC>p``
nnoremap <leader>P m`O<ESC>p``
A little explanation:
m`: set a mark in the current cursor position.
o<Esc>p: create a new line below and paste the text in this line
O<Esc>P: create a new line above and paste the text in this line
``: put the cursor in the original position
See :h mark for more information about marks in Vim.
This solution only seems to apply when the block of copied text starts on a new line (as opposed to grabbing a snippet of text somewhere within a line), but you can always start your copy on the last character you want to grab, then navigate to the last character at the end of line prior to the start of your desired copy block. Then when you want to paste it, place the cursor at the end of the line under which you want your text to be pasted and hit p. If I haven't screwed up the explanation, this should provide the effect you're looking for.

Resources