Is it possible to format text with vim - vim

is it possible to format text with vim like here. If it is possible how to do that.
Thanks!

Vim has :set textwidth, :set formatoptions and the gq command for wrapping paragraphs. Use :set smartindent to enable left alignment. of wrapped paragraphs.
:left left-aligns text.
:center centers it.
:right right-aligns it.

You can use word wrapping, if that is what you want.
:set wm=2
:set textwidth=72
To apply these settings to existing text, use the gq command. To apply it to all the text, just hit the following sequence:
<ESC>
gg
gq
G
Or first select a portion of the text followed by gq.

Related

Find and highlight a unit separator character 0x1f in VIM

The title says it all, how you can highlight all the hidden 0x1f(31) chars using vim? I tried :set list on its own as well as with
:set listchars=eol:$,tab:>-,trail:~,extends:>,precedes:<
:set list
but no luck
Use :match:
:match Search /\%x1f/
See the following for more information:
:h :match
:h /\%x

Vim Dumb Indenting?

Is there a way to just have Vim copy the indent from the line above, whether it be spaces or tabs, oblivious of the file types?
:set ai
See :help autoindent
I assume you are going to paste something and adjust the indent.
Try ]p
If you are at the beginning of the line and want to copy all the indenting characters above the line that you are currenly on now you can use Ctrl+y. It copies the character from the line above one at a time. Ctrl+e does the same thing but it copies from the line below.
It seems what I've wanted isn't actually possible as Vim automatically removes whitespaces, and uses configuration settings for its indention.
I've avoided this put slapping these in to my vimrc:
:inoremap <CR> x<BS><CR>x<BS>
:inoremap <up> x<BS><up>
:inoremap <down> x<BS><down>
:nnoremap o ox<BS>
:nnoremap O Ox<BS>
It simply puts a character in place and then removes it before I exit the editing mode, so Vim doesn't remove the empty line. If this is the case then it may be simply Vim checking if any editing was done to the line, auto indenting not counted. Maybe someday I'll check out the source and poke around.
I also wanted to use the previous line's indent (so I'd get different indents for different files and not have to tamper with settings each time), but I've managed to compromise and use the lovely Vim plugin.

Indenting a bunch of lines in Vim

Is there a way to indent a selection of lines in Vim, like we have in text editors where we select a bunch of lines and press tab (or shift tab) to indent/unindent the selected lines?
I am talking about general indentation and not related to code indentation.
You can select a set of lines with visual line mode (via Shift + V), and then type
>
and, to dedent,
<
You can also add numeric arguments. Find out you didn't indent enough? Hit gv to re-select your previous selection.
While typing in normal mode, try out Ctrl + T or Ctrl + D to indent or dedent.
Use visual mode as Peter suggests. You can also use X>> where X is the number of lines you want to indent. E.g. 5>> indents five lines from current line and down.
I use the following mappings to indent/unindent:
vmap <TAB> >gv
vmap <S-TAB> <gv
Use TAB to indent and shift-TAB to unindent the visually selected lines.
If a block is selected Vim indents/unindents what is right of the start of
the block.
As suggested by the other answers you can use >. Alternatively, you can automatically correctly indent your code by selecting the set of line in visual mode (using shift+V), and then using =, or using == to indent the current line.
There's a Vim Cast on this topic: Indentation commands
I like Vim Casts. They are informative and pleasant to watch.

Word wrap in Gvim

How do I make Gvim word wrap in such a way that doesn't break words in the middle?
Looks like there is a solution online after all.
:set formatoptions=l
:set lbr
Link: http://vim.wikia.com/wiki/Word_wrap_without_line_breaks
You can
:set nowrap
to just let huge lines scroll of the edge of your screen. But tw is probably the better way to go.
you can
:set wrap linebreak nolist
:set tw=78
sets the text width to 78 characters. You can use "[movement]gq" to re-wrap some text.
I use the following settings to wrap long lines for things like markdown files.
:set wrap
:set linebreak
:set nolist " list disables linebreak
:set textwidth=0
:set wrapmargin=0
Source: http://vim.wikia.com/wiki/Word_wrap_without_line_breaks
You can also use wrapmargin, which the manual defines as:
Number of characters from the right window border where wrapping
starts. When typing text beyond this limit, an <EOL> will be inserted
and inserting continues on the next line.

Is there any command to toggle enable auto text wrapping?

By default, I think my vimrc setttings enable the auto-wrapping. However, sometimes, I would like to see text lines are not wrapped. Is there any command I toggle the text line wrapping enabled and disabled? This would avoid me to exit and to change settings.
I think what you want is:
:set wrap!
This will toggle line wrapping.
More about using ! (bang) to alter commands can be found at:
:help :_!
In your vimrc, create a function such as this:
:function ToggleWrap()
: if (&wrap == 1)
: set nowrap
: else
: set wrap
: endif
:endfunction
Then map a key (such as F9) to call this function, like so:
map <F9> :call ToggleWrap()<CR>
map! <F9> ^[:call ToggleWrap()<CR>
Whenever you press F9, it should toggle your wrapping on and off.
:set nowrap
There is also the linebreak option that controls whether wrapped text is broken at word boundaries or not.
Add the following to have CTRL+W toggle wrapping. You can change it to some other key if you don't want w to be it.
map <C-w> :set wrap!<CR>
The quickref suggests (no)wrap
I happen to like tpopeā€™s unimpaired plugin, where yow will toggle wrap settings.
For those who want to change the text instead of just visual effect, for example in git commit, just press qt in a roll and press enter. This will properly wrap the current paragraph your cursor is in. The paragraph is only delimited by blank lines. or you can select some area to press qt.
I found this by total accident.

Resources