Archive

Archive for the ‘vim’ Category

How to write javascript codes more jslint-compliant

February 21, 2012 Leave a comment

1. The basics
If you love writing Javascript codes, you should have some kind of syntax validator installed. The one and only tool I am using is jslint, which can be installed in a number of ways, depending on the os and development toolkits available. My favorite way to is to install through npm (provided that node.js is installed, if not, try nvm):
npm install -g jslint

Once installed, you can fire it like:

$ jslint my_script.js

Most likely it’ll produce a lot of errors/warnings unless you already write code in a jslint-compliant fashion. There are some options you might want to add:

$ jslint --eqeq --white my_script.js

Where

  • –eqeq: tolerant == and !=
  • –white: doesn’t enforce strict whitespace rules, for example, doesn’t require spaces around =’s

See the jslint doc site for a complete list of options.

2. Run jslint directly from vim
If you are like me – a vim nut, you might want to integrate jslint into vim, add the following into your ~/.vimrc

command Jslint !jslint --white --eqeq %
nnoremap <leader>j :Jslint<CR>

To activate jslint while your are inside vim, simply hit \j in command mode. Special thanks to http://blog.sanctum.geek.nz/series/unix-as-ide/ for the vim integration tip.

Categories: javascript, jslint, vim

A couple of plugins that make web developing life easier.

September 18, 2011 Leave a comment

matchit
The plugin basically extends the functionality of % in vim, it cycles between opening and closing tags, very handy if you love coding htmls by hand.
Steps to make it work: { windows users pls unzip the files to the vim home directory }
1) download the zip file from the above link
2) unzip matchit.zip -d ~/.vim
3) add the following line to your .vimrc
filetype plugin on
if filetype plugin is set to off [ default setting ]
4) open an existing .html file or create a new one, under normal mode, pressing % will cycle between html tags, v% will select text between opening and closing tags (with the opening tag name selected)

jsbeatify
This is yet another handy and powerful vim plugin. As its name suggests it does one thing and it does really well – beatifies javascript files. Steps to make it work:
1) Download it and save it to ~/.vim/plugin
2) Open an existing .js file or create a new one, under normal mode, press \ff and the code is formatted

There’s one thing that this plug in is not able to do – format the selected js code – in other words you can’t beautify the embedded js code in html or php files.

[ Edit 9/18/2011: Actually I just found another better solution to format javascript code – what I like the best is that this indenter formats all javascript code either in stand-alone file or embedded code. Just put the file html.vim from http://www.vim.org/scripts/download_script.php?src_id=6885 to ~/.vim/indent and use gg=G to format the whole file or = to format selected code. ]

Categories: html, Tip, vim

Beautify codes with a simple vim trick

May 20, 2011 Leave a comment

For codes that are not properly indented, use the followings to correct them with vim:

1) select the parts that are not correctly indented, you can
    a) select codes line-by-line by pressing v, then the up or down arrow key
    b) select a whole block of codes by entering viB
    c) select the whole document by entering ggvG

2) Now press the equal sign = and smile

Categories: Tip, vim

My favorite VIM tips

January 2, 2010 Leave a comment

I love using vim as my text editor. It’s almost always the first program I would try to check if it’s installed on a newly installed Linux system. Below are some of the tips that I find quite useful and use quite often:

1. Moving cursor

gg - move cursor to the beginning of the file
G – move cursor to the end of the file
nG – move cursor to the nth line
Ctrl + o – move cursor to the previous cursor position

2. Multiple lines commenting

1) Shift + v to select the first line
2) move up or down arrow key to select more lines
3) :
4) status line will become :’<,’>
5) complete the line as below, followed by the Enter key
:'<,'>s/^/#/g

3. Multiple Lines deletion

Select and delete

1) shift + v to select the first line
2) move up or down arrow key to select more lines
3) hit letter d

Use direct command

:.,$d – Delete all lines starting from current line (inclusive) to the end of the file
:0,.d – Delete all lines starting from beginning of the file to current line (inclusive)
:1,3,5d – Delete lines 1, 3 and 5
:.,+Nd – Delete current line plus N lines below, N>=1
:.,-Nd – Delete current line plus N lines above, N>=1, you will be prompted ‘Backward range given, ok to swap (y/n)?‘ hit y to accept.

4. Typo correction

This is better explained by using the “the” example: say you want to enter the but instead you type teh. To correct the typo:
1) exit edit mode
2) move cursor to the letter e so it becomes teh
3) enter commands xp

5. Line swapping

Suppose you have the following two lines at the beginning of your bash script

echo "hello"
#!/bin/bash

Obviously you want to swap those two lines so it becomes a valid bash script. Here’s how you do it:
1) make sure you are not in edit mode
2) move cursor to (anywhere) the first line
3) now enter commands ddp

6. File opening tips

vim file1 file2 … -o opens file1, file2, … in stacked windows
vim file1 file2 … -O opens file1, file2, … side by side
vim file + opens file and move the cursor to the last line.

That’s all for now. I will add more if I find more interesting enough to the above list. Of course there are a lot more useful tips using vim. As always I welcome comments and feedback.

Categories: Tip, vim
Follow

Get every new post delivered to your Inbox.