Posts Tagged ‘vim’

Unwrap hard-wrapped text via command line

Monday, October 24th, 2016

I searched for a bash/sed/tr combination to unwrap hard 80-character per line text like:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed imperdiet felis
suscipit odio fringilla, pharetra ullamcorper felis interdum. Aenean ut mollis
est. Maecenas mattis convallis enim. Nullam eget maximus mi. Vivamus nec risus
suscipit, facilisis nunc at, eleifend massa. Aliquam erat volutpat. Aenean
malesuada velit vel libero cursus, et aliquam nibh imperdiet. Maecenas
ultrices, orci eu posuere commodo, leo diam ultricies velit, sed hendrerit odio
leo sed erat.

Pellentesque at enim id lacus tristique blandit. Duis at suscipit odio, eu
ullamcorper lorem. Interdum et malesuada fames ac ante ipsum primis in
faucibus. Sed non massa urna. Cum sociis natoque penatibus et magnis dis
parturient montes, nascetur ridiculus mus. Etiam blandit metus eget sem
consequat tincidunt. Vivamus auctor pharetra sapien non iaculis. Curabitur quis
fermentum est. Mauris laoreet augue finibus, rhoncus enim et, finibus nibh.
Praesent varius neque mi, id tempor massa facilisis eget. Nulla consectetur,
massa sed tempus laoreet, nisl purus posuere ipsum, eu gravida purus arcu nec
ante.

Pellentesque dapibus ultrices purus, et accumsan sapien ultrices a. Nulla
ultricies odio sit amet tellus tempus, et gravida dui feugiat. Aenean pretium
in lectus vitae molestie. Proin in rhoncus eros. Donec in ultricies nisi,
volutpat ultrices lacus. Suspendisse gravida hendrerit ipsum vitae feugiat.
Phasellus pharetra malesuada orci et euismod. Proin luctus nunc sit amet
gravida pulvinar. Nam quis dapibus mauris. Nulla accumsan nisl vel turpis
lobortis vulputate. Integer sem orci, lobortis ut blandit quis, consequat eget
purus. Fusce accumsan magna eu mi placerat rhoncus.

Into single lines per paragraph, like this:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed imperdiet felis suscipit odio fringilla, pharetra ullamcorper felis interdum. Aenean ut mollis est. Maecenas mattis convallis enim. Nullam eget maximus mi. Vivamus nec risus suscipit, facilisis nunc at, eleifend massa. Aliquam erat volutpat. Aenean malesuada velit vel libero cursus, et aliquam nibh imperdiet. Maecenas ultrices, orci eu posuere commodo, leo diam ultricies velit, sed hendrerit odio leo sed erat.

Pellentesque at enim id lacus tristique blandit. Duis at suscipit odio, eu ullamcorper lorem. Interdum et malesuada fames ac ante ipsum primis in faucibus. Sed non massa urna. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Etiam blandit metus eget sem consequat tincidunt. Vivamus auctor pharetra sapien non iaculis. Curabitur quis fermentum est. Mauris laoreet augue finibus, rhoncus enim et, finibus nibh. Praesent varius neque mi, id tempor massa facilisis eget. Nulla consectetur, massa sed tempus laoreet, nisl purus posuere ipsum, eu gravida purus arcu nec ante.

Pellentesque dapibus ultrices purus, et accumsan sapien ultrices a. Nulla ultricies odio sit amet tellus tempus, et gravida dui feugiat. Aenean pretium in lectus vitae molestie. Proin in rhoncus eros. Donec in ultricies nisi, volutpat ultrices lacus. Suspendisse gravida hendrerit ipsum vitae feugiat. Phasellus pharetra malesuada orci et euismod. Proin luctus nunc sit amet gravida pulvinar. Nam quis dapibus mauris. Nulla accumsan nisl vel turpis lobortis vulputate. Integer sem orci, lobortis ut blandit quis, consequat eget purus. Fusce accumsan magna eu mi placerat rhoncus.

This is useful, for example, when editing a plain text entry with vi that is ultimately pasted into a web form.

I couldn’t find a good unix tools solution so I settled on a python script I found. Here’s the slightly edited version I save in unwrap:

#!/usr/bin/env python

import sys;paragraph = []
for line in sys.stdin:
   line = line.strip()
   if line:
      paragraph.append(line)
   else:
      print ' '.join(paragraph).replace('  ', ' ')
      paragraph = []
print ' '.join(paragraph).replace('  ', ' ')

Then I call it with

unwrap < my-text-file.txt

Copy text file without hard-wrap new lines

Friday, February 28th, 2014

I often format small text entries in vim and then copy them into other places like web forms. In vim I like to have a hard 80 character line wrap. But this means that after every 80 characters I have a newline character. If I just copy the file or from the terminal screen then I’ll impose this hard wrap in the submitted text. I’ve noticed that this is especially bad for academic review submissions because the submission system might then additionally impose its own hard wrap at a different width causing a very staggered, ragged appearance.

Here’s my solution to copy a text file without newlines but keeping double newlines which indicate paragraphs.

cat % | perl -pe '$/=""; s/\n([^\n])/ \1/g;' | pbcopy

There must be a way to do this inside of vim properly, but I couldn’t figure it out.

vim only spell checking in TeX comments

Monday, January 20th, 2014

Editing a LaTeX document with vim, I noticed it was only spell-checking comments. I fixed this by add this to my ./vimrc:

autocmd FileType plaintex,tex,latex syntax spell toplevel

Searching for digraphs by name in vim

Sunday, January 5th, 2014

I love using vim’s digraphs to insert unicode symbols into my text documents. I’ve memorized a lot of the more frequent ones that I type but I often end up looking up new ones or ones I’ve forgotten. I used to use:

:digraphs

to display a giant table of all of the digraphs. There sorted by unicode so they’re in a roughly coherent order, but there’s a lot to look at, and it’s just the raw symbols.

Recently I found a much better way. By typing

:help digraphs

you can pull up the digraphs help page which contains a more thorough list of all digraphs including English unicode symbol names. For example I can hit /ditt to find the line:

〃      +"      3003    12291   DITTO MARK

Find and replace deprecated eigen coeffRef(…,…) += … lines with Triplet

Friday, April 26th, 2013

This is the vim find and replace command I used to switch from the legacy DynamicSparseMatrix command coeffRef to the new format using the Triplet class.


%s/\([^\.]*\).coeffRef(\([^,]*\),\([^)]*\)) *+= *\([^;]*\);/\1_IJV.push_back(Triplet<Scalar>(\2,\3,\4));/g

Use vim to replace asserts with mexErrMsgTxt

Monday, December 31st, 2012

Snippet to find and replace appropriately:


%s/assert(\(.*\));/if(!(\1)) mexErrMsgTxt("\1");/g

Move vim’s undo files

Thursday, November 15th, 2012

vim creates little files to keep a persistent undo. The names are like:


.filename.txt.un~

The dot prefix and the tilde suffix make these files a pain to deal with in bash. Here’s how to move these files to another directory:


mv .*un\~ other_directory

Force vim to use specific file type

Tuesday, September 27th, 2011

I recently download the glsl syntax highlighting plugin for vim. It perfectly recognizes my .vert and .frag files. But as soon as I added the line:


#version 120

vim starting thinking the filetype was not glsl but conf.

The cause seems to be that the glsl plugin suggests a rather weak form of recognizing the filetype. Adding the following to your .vimrc file


au BufNewFile,BufRead *.frag,*.vert,*.fp,*.vp,*.glsl setf glsl 

When instead to override whatever vim is doing to think that the file is conf you must use the only slightly different


au BufNewFile,BufRead *.frag,*.vert,*.fp,*.vp,*.glsl set filetype=glsl

Vi(m) tip #11: remove swap file of current file (e.g. after a recover operation)

Wednesday, June 22nd, 2011

You can remove the swap file (.*.swp) of the current file in vim with a simple command in command mode:


:!rm .%.swp

Vim sometimes only partially syntax-highlighting for .tex files

Thursday, May 12th, 2011

Editting .tex files with vim I noticed that sometimes I got full syntax highlighting, but other times only certain keywords were markes and the colors were a bit different. Turns out this was because vim was occasionally recognizing files (based on keywords) as plaintex rather than tex. You can see what filetype vim thinks you have open by issuing:


:set ft

For a bit I was fixing this on a case-by-case basis, if vim’s highlighting was wrong I would issue:


:set ft=tex

and that would fix it for at least the current file and session.

The real permanent fix was to add the following to my ~/.vimrc file:


let g:tex_flavor = "latex"

Now all my .tex files are recognized as tex and not plaintex.