home.

tagged: vim

Vim: Change tabs to spaces and set how many spaces on tab press

When you press tab in vim, you normally get a tab character inserted in your text.
 
You can turn this off via set expandtab. When you press tab now, you get spaces.
 
To alter how many spaces you get on tab press, issue set tabstop=4.
 
Finally, to alter how to spaces you get when you indent text with >, issue set shiftwidth=4.

vim


Vim: Syntax highlighting fix

For some reason, in large files, syntax highlighting in vim breaks sometimes, with Javascript at least.
 
You can ensure the syntax highlighting starts from the beginning of the file, thereby not breaking, via:

:syntax sync fromstart

Something you should probably put in .vimrc.

unix vim


Replacing commas in fields in CSV files with regex

Sometimes -- in Comma Separated Value files -- you have commas inside the fields themselves.
 
These means, should you run them through sed, awk or whatever, based on commas you'll have extra fields:

afield,"another field","oh look, a false field",bugger

However, luckily, the field with the comma within is in double quotation marks.
 
This means we can run a regex to replace all such occurrances with the commas's unicode entity, \\u0027
 
The regex works like this:
  1. Look for text that starts with ,"
  2. Keep grabbing text, which is not the end of the qutotation mark, until we get a comma
  3. Keep on grabbing again until we reach a double quotation mark
Then we can output the grabbed text between such and replace , with \\u0027
 
The regex, in vim syntax, looks like this:

%s/\(,"[^\"]*\),\(.*"\)/\1\\\u0027\2/

\( and \( are the grouping, and the /\1\\u0027\2/ defines the replacement with the HTML entity, so they can be ignored for this explanation.

,"[^\"]*,.*"

Leaving us with ," saying start the match with such, then [^\"].* is saying only grab text that's not a double quotation mark.
 
Then, , is saying look for the comma in the quotation marks, and then .*" grabs everything until we get an ending quotation mark.
 
Then, since we're grouping everything except the comma, we can do the replacement: /\1\\u0027\2/

sed unix csv awk regex vim


Sanitizing CSV files with regex

Often, you want to use a CSV file, but commas within fields, double and single quotation marks can work trickily with some other programs.
  1. The first regex will replace all commas in double quotation fields with unicode entity (only if such is not the first field, however)
  2. The second will then remove all the double quotation marks
  3. The third will replace the single quotation marks with their unicode entity
These are all in vim syntax.

%s/\(,"[^\"]*\),\(.*"\)/\1\\u002C\2/
%s/"//g
%s/'/\\u0027/g

unix csv regex vim


Vim: Binding to comment out code

If you want a shortcut to quickly comment out some text, put this in your .vimrc

" Quick comment binding
map ^_ :s#^#//#<CR>
map ;^_ :s#^//##<CR>

The ^_ is inserted by pressing the control then / on your keyboard.
 
When you press the ctl-/ key, that line, or selection, will be prefixed with //
When you press ctrl ; and /, the comment will be removed.
 

vim

Page 2 of 3
prev next