Smart Wrap in Vim - vim

I have been wondering if Vim has the capability to smart wrap lines of code, so that it keeps the same indentation as the line that it is indenting. I have noticed it on some other text editor, such as e-text editor, and found that it helped me to comprehend what I'm looking at easier.
For example rather than
<p>
<a href="http://www.example.com">
This is a bogus link, used to demonstrate
an example
</a>
</p>
it would appear as
<p>
<a href="somelink">
This is a bogus link, used to demonstrate
an example
</a>
</p>

This feature has been implemented on June 25, 2014 as patch 7.4.338. There followed a few patches refining the feature, last one being 7.4.354, so that's the version you'll want.
:help breakindent
:help breakindentopt
Excerpts from vim help below:
'breakindent' 'bri' boolean (default off)
local to window
{not in Vi}
{not available when compiled without the |+linebreak|
feature}
Every wrapped line will continue visually indented (same amount of
space as the beginning of that line), thus preserving horizontal blocks
of text.
'breakindentopt' 'briopt' string (default empty)
local to window
{not in Vi}
{not available when compiled without the |+linebreak|
feature}
Settings for 'breakindent'. It can consist of the following optional
items and must be seperated by a comma:
min:{n} Minimum text width that will be kept after
applying 'breakindent', even if the resulting
text should normally be narrower. This prevents
text indented almost to the right window border
occupying lot of vertical space when broken.
shift:{n} After applying 'breakindent', wrapped line
beginning will be shift by given number of
characters. It permits dynamic French paragraph
indentation (negative) or emphasizing the line
continuation (positive).
sbr Display the 'showbreak' value before applying the
additional indent.
The default value for min is 20 and shift is 0.
Also relevant to this is the showbreak setting, this will suffix your shift amount with character(s) you specify.
Example configuration
" enable indentation
set breakindent
" ident by an additional 2 characters on wrapped lines, when line >= 40 characters, put 'showbreak' at start of line
set breakindentopt=shift:2,min:40,sbr
" append '>>' to indent
set showbreak=>>
Note on behaviour
If you don't specify the sbr option, any showbreak any characters put appended to the indentation. Removing sbr from the above example causes an effective indent of 4 characters; with that setting, if you just want to use showbreak without additional indentation, specify shift:0.
You can also give a negative shift, which would have the effect of dragging showbreak characters, and wrapped text, back into any available indent space.
When specifying a min value, the shifted amount will be squashed if you terminal width is narrower, but showbreak characters are always preserved.

There is a patch for this, but it's been lingering for years and last time I checked did not apply cleanly. See the "Correctly indent wrapped lines" entry in http://groups.google.com/group/vim_dev/web/vim-patches -- I really wish this would get in the mainline.
Update: that link seems to have bitrotted. Here is a more up to date version of the patch.
Update 2: it has been merged upstream (as of 7.4.345), so now you only have to :set breakindent.

I don't think it's possible to have exactly the same indentation, but you can still get a better view by setting the 'showbreak' option.
:set showbreak=>>>
Example:
<p>
<a href="http://www.example.com">
This is a bogus link, used to demonstrate
>>>an example
</a>
</p>
The real thing looks better than the example code above, because Vim uses a different colour for '>>>'.

UPDATE: In June 2014, a patch to support a breakindent option was merged into Vim (version 7.4.346 or later for best support).
You might also try :set nowrap which will allow vim to display long lines by scrolling to the right. This may be useful for examining the overall structure of a document, but can be less convenient for actually editing.
Other options close to what you're looking for are linebreak and showbreak. With showbreak, you can modify what is displayed at the left margin of lines that are wrapped, but unfortunately it doesn't allow a variable indent depending on the current context.

The only way I know of that you could do this would be to use a return character (as mentioned by Cfreak) and combine the textwidth option with the various indentation options. If your indent is configured correctly (as it is by default with the html syntax I believe, but otherwise see the autoindent and smartindent options), you can:
:set formatoptions = tcqw
:set textwidth = 50
gggqG
If you have any customisation of the formatoptions setting, it may be better to simply do:
:set fo += w
:set tw = 50
gggqG
What this does:
:set fo+=w " Add the 'w' flag to the formatoptions so
" that reformatting is only done when lines
" end in spaces or are too long (so your <p>
" isn't moved onto the same line as your <a...).
:set tw=50 " Set the textwidth up to wrap at column 50
gg " Go to the start of the file
gq{motion} " Reformat the lines that {motion} moves over.
G " Motion that goes to the end of the file.
Note that this is not the same as a soft wrap: it will wrap the lines in the source file as well as on the screen (unless you don't save it of course!). There are other settings that can be added to formatoptions that will auto-format as you type: details in :help fo-table.
For more information, see:
:help 'formatoptions'
:help fo-table
:help 'textwidth'
:help gq
:help gg
:help G
:help 'autoindent'
:help 'smartindent'

:set smartindent
:set autoindent
I think you still have to use a return though

If your HTML is sufficiently well formed, running it through xmllint might help:
:%!xmllint --html --format

A Macro Solution:
Edit:
The operate gq{motion} auto-formats to whatever the variable "textwidth" is set to. This is easier/better than using the 80lBi^M I have for my macro.
If you have autoindent enabled
:set autoindent
Then entering a return at the end of a line will indent the next line the same amount. You can use this to hard enter in linewraps if you'd like. The following macro takes advantage of this to automatically indent your text:
set register z to:
gg/\v^.{80,}$^M#x (change 80 to whatever length you want your text to be)
and set register x to:
80lBi^M^[n#x (change 80 to whatever length you want your text to be)
Then do
#x
to activate the macros. After a few seconds you're text will all be in properly indented lines of 80 characters or less.
Explanation:
Here's a dissection of the macros:
Part 1 (macro z):
gg/\v^.{80,}$^M#x
gg - start at the top of the file (this avoids some formatting issues)
/ - begin search
\v - switch search mode to use a more generic regex input style - no weird vim 'magic'
^.{80,}$ - regex for lines that contain 80 or more characters
^M - enter - do the search (don't type this, you can enter it with ctrl+v then enter)
#x - do macro x
Part 2 (macro x):
80lBi^M^[n#x
80l - move right 80 characters
B - move back one WORD (WORDS include characters like "[];:" etc.)
i^M - enter insert mode and then add a return (again don't type this, use ctrl+v)
^[ - escape out of insert mode (enter this with ctrl+v then escape)
#x - repeat the macro (macro will run until there are no more lines of 80 characters or more)
Caveats:
This macro will break if the there's a WORD that is 80 characters or longer.
This macro will not do smart things like indent lines past tags.
Use the lazyredraw setting (:set lazyredraw) to speed this up

Related

In vim, how can I modify all existing indents to be 2?

In vim, I set shiftwidth=2, but all my previous indents are still at the default 8. How can I change the previous indents from 8 to 2?
You can reindent the whole file with gg=G. gg goes to the first line, = indents (taking a movement), G goes to the last line.
If you're using set expandtab (like you should), you can modify the indentation in a file with
:%s/^ */ /
The settings affect how changes are made, but do not themselves make changes to the file.
If your original indents were achieved using hard tabs stops, then one trick you can do is this. Set the hard tab stop to 2:
:set ts=2
Now you have the two-space indentation (but achieved with hard tabs).
Now, do
:retab 8
This means, roughly, change the hard tab size to 8 (as if by :set ts=8) but at the same time edit all the tabbing in the buffer so that the indentation's appearance does not change.
So now the buffer is still indented to two spaces, but now :ts is back to 8.
If you have :expandtab set, then the indentation is now all spaces, otherwise it is a combination of 8-space tabs and spaces.
Even if this doesn't apply to your situation, retab is good to know because it's handy for dealing with sources that use hard tabs and that you'd like to convert to use spaces and a different indentation level at the same time.

Stop Vim wrapping lines in the middle of a word

After doing :set wrap, Vim wraps lines longer than the window.
But is it possible to have Vim wrap to a new line on blank spaces only, not half-way through a word?
:help wrap
This option changes how text is displayed. It doesn't change the text
in the buffer, see 'textwidth' for that.
When on, lines longer than the width of the window will wrap and
displaying continues on the next line. When off lines will not wrap
and only part of long lines will be displayed. When the cursor is
moved to a part that is not shown, the screen will scroll
horizontally.
The line will be broken in the middle of a word if necessary. See
'linebreak' to get the break at a word boundary.
:help linebreak
If on Vim will wrap long lines at a character in 'breakat' rather
than at the last character that fits on the screen.
:help breakat
'breakat' 'brk' string (default " ^I!#*-+;:,./?")
So, :set linebreak and it should work out of box. Or you can restrict breakat to just break on spaces, instead of spaces+punctuation.
Use
:set linebreak
Or 'lbr' for short. It will break lines on characters included in your 'breakat' option, which includes a space by default.
With vim open, press esc and enter
:set lbr
The following will do a line wrap without breaking any words and preserve the shorter lines.
:set formatoptions+=w
:set tw=80
gggqG
To try and format the current paragraph try the follwoing:
:nnoremap Q gqip
The following commands work for me, and I am using Red Hat 7.x at work, and Cygwin 3.1.4 at home. The exclamation point acts like a not operator.
:set wrap
:set wrap!

Vim: Indent with one space (not shiftwidth spaces)

The default VIM indentation commands indent by shiftwidth spaces
e.g.
>> Indent line by shiftwidth spaces
<< De-indent line by shiftwidth spaces
Is there any way to indent with one or n (where n != shiftwidth) space(s)?
One way to do that is to vertically select a column in the block with Ctrl+V then, I to insert vertically and then type a space and <Esc>. But is there a better way?
I'm not sure that there is a better way. But, there are a few ways that you could do it (that I can think of anyway)...
Your Visual Block Solution
Like you said: press Ctl-V select the lines you want, press I to insert, and enter the number of spaces.
Search
Similar to the above but a little more flexible - you can use with with the 'select paragraph' vip command, or any range really: press v or vip or what have you to select the range, and the type :s/^/{n spaces} where {n spaces} is the number of spaces you want to insert.
Its a little more verbose, but works pretty well for pretty much any range. Heck, if you wanted to do the whole file you could do Ctl-A (OS dependent) and indent the whole file (or just skip the whole visual mode thing and just do it command mode...as in :1,$s/^/{n spaces}
Note that you don't have to include the third slash in s/// since you aren't putting any switches at the end of the search.
Global
Maybe you want to only indent lines that match some pattern. Say...all lines that contain foo. No problem: type :g/foo/s/^/{n spaces}
Global is especially handy if its multi-line sections with a similar pattern. You can just escape into normal mode land and select the lines you want and indent accordingly: :g/foo/norm Vjj:s/^/{n spaces}Ctl-V{Enter}. Little more complicated with that extra Ctl-V{Enter} at the end but useful under certain circumstances.
Use tabstop and shiftwidth
Yes, if your doing it a lot - I'd do :set ts=2 and :set et and :set sw=2 and use >> and << every which way...
Make a Function
Okay, so still not brief enough and for whatever reason you need to do this a lot and you can't abide messing with sw, et and ts settings. No problem, just write up a quick function and give it a localleader mapping:
function! AddSpace(num) range
let s:counter = 0
let s:spaces = ''
while s:counter < a:num
let s:spaces .= ' '
let s:counter = s:counter + 1
endwhile
execute a:firstline .','. a:lastline .'s/^/'. s:spaces
endfunction
:map <LocalLeader>i :call AddSpace(3)Ctl-V{enter}
Maybe just knowing more than one way to do this is better than only knowing one? After all, sometimes the best solution depends on the problem :)
Indent a block of code in vi by three spaces with Visual Block mode:
Select the block of code you want to indent. Do this using Ctrl+V in normal mode and arrowing down to select text. While it is selected, enter ":" to give a command to the block of selected text.
The following will appear in the command line: :'<,'>
To set indent to 3 spaces, type le 3 and press enter. This is what appears: :'<,'>le 3
The selected text is immediately indented to 3 spaces.
Indent a block of code in vi by three spaces with Visual Line mode:
Open your file in VI.
Put your cursor over some code
Be in normal mode press the following keys:
Vjjjj:le 3
Interpretation of what you did:
V means start selecting text.
jjjj arrows down 4 lines, highlighting 4 lines.
: tells vi you will enter an instruction for the highlighted text.
le 3 means indent highlighted text 3 lines.
To change the number of space characters inserted for indentation, use the shiftwidth option:
:set shiftwidth = <number>
Have a look here for more details.
You can also add that to your .vimrc file.
If I'm understanding correctly, you could use:
ctrl+V, jj then ':le n', where n is the number of spaces to indent.
http://vim.wikia.com/wiki/Shifting_blocks_visually
Place marks ('a' and 'b') between the code you want to indent:
<position cursor at top of block>
m a
<position cursor at bottom of block>
m b
Do a replace command such that each newline character between your marks is replaced with the number of spaces desired (in this example, 2 spaces):
:'a,'bs/^/ /g
If white space indentation already exists and you want to increase it further by one or more columns, then select a block of one or more white space columns using Ctrl-V, yank and paste it in the same place.
I had to dedent by a given number of spaces, amount, inside a vim script. This worked:
let sw_setting = &shiftwidth
set shiftwidth=1
exe "normal v" . amount . "<"
let &shiftwidth = sw_setting
A side-effect is that it resets the last visual mode selection. Instead, you may wish to edit the exe... line such that it executes "<<" repeated amount times in normal mode. That is: instead of normal v3<, make it normal <<<<<<, if amount is 3.
I like to use Space to indent a visual selection with a single space:
vnoremap <silent> <space> :s/^/ /<CR>:noh<CR>gv
And I couldn’t get Shift+Space to dedent, so I use z:
vnoremap <silent> z :s/^\s\=//<CR>:noh<CR>gv

What is Vim's feature name for this: # vim:sw=4:ts=4:et:

Even after 20 years with Vim, I keep forgetting name for the Vim feature where the editor picks up config statements from a comment at the beginning (or I think end) of a file:
# vim:sw=4:ts=4:et:
Thanks for a reminder!
It's called modeline
:he modeline
If you start editing a new file, and the 'modeline' option is on, a
number of lines at the beginning and end of the file are checked for
modelines. There are two forms of modelines.
The first form: [text]{white}{vi:|vim:|ex:}[white]{options}
[text] any text or empty
{white} at least one blank character (<Space> or <Tab>)
{vi:|vim:|ex:} the string "vi:", "vim:" or "ex:"
[white] optional white space
{options} a list of option settings, separated with white space or ':',
where each part between ':' is the argument for a ":set"
command (can be empty)
Add this to $MYVIMRC:
setglobal modeline
It's called modeline. In help it can be found by grepping
helpgrep # vim
If you wish to check whether modeline are active, do set modeline? (if the are it will say modeline, otherwise nomodeline)
To turn them off for certain, add this in your vimrc
:set modelines=0 "number of modelines vim parses
:set nomodeline "turn off parsing
:h 'ts (i.e. :help 'tabstop') will bring up a detailed explanation of how to use et, ts, and sw as well as giving a pointer to modeline (modeline is the option that is in question).

How to stop line breaking in vim

I like that the long lines are displayed over more than one terminal line; I don’t like that vim inserts newlines into my actual text. Which part of .vimrc I should change?
Use
:set wrap
To wrap lines visually, i.e. the line is still one line of text, but Vim displays it on multiple lines.
Use
:set nowrap
To display long lines as just one line (i.e. you have to scroll horizontally to see the entire line).
I like that the long lines are displayed over more than one terminal line
This sort of visual/virtual line wrapping is enabled with the wrap window option:
:set wrap
By default this will wrap at the first character that won't fit in the window. This means it will wrap in the middle of a word if that's where the window boundary lies. To change it to wrap on word boundaries, you can also:
:set linebreak
This will cause wrap to only wrap at the characters in the breakat setting, which defaults to space, tab, and small set of punctuation characters.
:set breatat
breakat= ^I!#*-+;:,./?
I don’t like that vim inserts newlines into my actual text.
To turn off physical line wrapping, clear both the textwidth and wrapmargin buffer options:
:set textwidth=0 wrapmargin=0
I'm not sure I understand completely, but you might be looking for the 'formatoptions' configuration setting. Try something like :set formatoptions-=t. The t option will insert line breaks to make text wrap at the width set by textwidth. You can also put this command in your .vimrc, just remove the colon (:).
:set tw=0
VIM won't auto-insert line breaks, but will keep line wrapping.
Use :set nowrap .. works like a charm!
You may find set linebreak useful; with set wrap on this will wrap but only cutting the line on whitespace and not in the middle of a word.
e.g.
without linebreak the li
ne can be split on
a word
and
with linebreak on the
line will be
split on
whitespace only
set formatoptions-=t Keeps the visual textwidth but doesn't add new line in insert mode.
Its strange that such a simple setting would require this amount of 'hocus-pocus' to work.
To answer your question now, for me it seemed to work with the combination of the following:
:set wrap linebreak nolist
(this seems to prevent existing lines from breaking, just wrap.)
AND
set formatoptions=l
(this prevents new/edited lines from breaking, while += does not do it for me as other settings/plugins seem to find space and add their own options which override mine.)
If, like me, you're running gVim on Windows then your .vimrc file may be sourcing another 'example' Vimscript file that automatically sets textwidth (in my case to 78) for text files.
My answer to a similar question as this one – How to stop gVim wrapping text at column 80 – on the Vi and Vim Stack Exchange site:
In my case, Vitor's comment suggested I run the following:
:verbose set tw?
Doing so gave me the following output:
textwidth=78
Last set from C:\Program Files (x86)\Vim\vim74\vimrc_example.vim
In vimrc_example.vim, I found the relevant lines:
" Only do this part when compiled with support for autocommands.
if has("autocmd")
...
" For all text files set 'textwidth' to 78 characters.
autocmd FileType text setlocal textwidth=78
...
And I found that my .vimrc is sourcing that file:
source $VIMRUNTIME/vimrc_example.vim
In my case, I don't want textwidth to be set for any files, so I just commented out the relevant line in vimrc_example.vim.
It is correct that set nowrap will allow you to paste in a long line without vi/vim adding newlines, but then the line is not visually wrapped for easy reading. It is instead just one long line that you have to scroll through.
To have the line visually wrap but not have newline characters inserted into it, have set wrap (which is probably default so not needed to set) and set textwidth=0.
On some systems the setting of textwidth=0 is default. If you don't find that to be the case, add set textwidth=0 to your .exrc file so that it becomes your user's default for all vi/vim sessions.
I personnally went for:
set wrap,
set linebreak
set breakindent
set showbreak=ͱ.
Some explanation:
wrap option visually wraps line instead of having to scroll horizontally
linebreak is for wrapping long lines at a specific character instead of just anywhere when the line happens to be too long, like in the middle of a word. By default, it breaks on whitespace (word separator), but you can configure it with breakat. It also does NOT insert EOL in the file as the OP wanted.
breakat is the character where it will visually break the line. No need to modify it if you want to break at whitespace between two words.
breakindent enables to visually indent the line when it breaks.
showbreak enables to set the character which indicates this break.
See :h <keyword> within vim for more info.
Note that you don't need to modify textwidth nor wrapmargin if you go this route.

Resources