Posts Tagged ‘vim’

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.

Convert quad mesh OBJ to triangle mesh OBJ using regular expressions (with bash and sed)

Wednesday, December 15th, 2010

Here’s a simple bash one-liner that converts a quad mesh stored in an OBJ file into a triangle mesh stored in a OBJ file. It splits every quad (a,b,c,d) into two triangles (a,b,c) and (a,c,d).

I issue this in the bash shell of my mac (you may have to tweak the sed syntax):


cat quads.obj | sed -E -e "s/f ([0-9\/]+) ([0-9\/]+) ([0-9\/]+) ([0-9\/]+)/f \1 \2 \3`echo -e "\r"`f \1 \3 \4/g" > triangles.obj

Or I open the file with vim and use:


:%s/f \([0-9\/]\+\) \([0-9\/]\+\) \([0-9\/]\+\) \([0-9\/]\+\)/f \1 \2 \3\rf \1 \
3 \4/g

Digraphs stopped working with vim 7.3

Tuesday, November 30th, 2010

I excitedly upgraded vim to the new vim 7.3 (persistent undo is the big feature). To my dismay all the fancy digraphs I’ve been using to display math characters stopped working. And when I issued:


:digraphs

I only saw a handful of characters (maybe just ASCII), certainly no interesting unicode characters.

Luckily the fix was easy. I just needed to enable the “multi-byte” feature. To do this, in the vim project directory I issued:


./configure --enable-multibyte
make
sudo make install

Vi(m) tip #10: Current file name

Monday, September 20th, 2010

A friend at work wanted to be able to see the name of the current file he was editing in vim without closing it or saving it. The solution he found was to enter in command mode:


<ctrl> <shift> G

This displays a little info bar on the bottom.

This made me remember that you can use % as a standin for the current file's name when issuing a command. As in:


:!echo %

which displays the current file name (up to bash gotchas).

Or you could write the current file to filename.temp with:


:w %.temp

I used this in an earlier post to typeset a latex document from vim as a oneliner.

Vim syntax highlighting for cmake’s CMakeLists.txt files

Tuesday, August 31st, 2010

I use the following at the end of my .vimrc file to force vim to highlight cmake’s CMakeLists.txt files like any other cmake file:


au BufNewFile,BufRead CMakeLists.txt set filetype=cmake

Looks much better than whatever it was trying to do before, which for me was nothing.