Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript syntax & indentation in Vim

Tags:

javascript

vim

I know this has been asked before, but I'm having trouble getting JavaScript indentation to work properly in Vim.

I tried installing this plugin:

http://www.vim.org/scripts/script.php?script_id=3081

And I get this behaviour:

if (x == 1) {
alert("nice");
}

This is my vimrc:

syntax on
set background=light
colorscheme solarized
set tabstop=4
filetype plugin indent on
let g:solarized_termcolors=16

I also tried it with this plugin:

http://www.vim.org/scripts/script.php?script_id=1840

But that gives me this:

if (x == 1) {
        alert("nice");
}

i.e., two tabs, where I only want it to indent by a single tab.

Anyone have any ideas what to do here?

like image 216
Cera Avatar asked Oct 05 '11 00:10

Cera


People also ask

What is JavaScript basic syntax?

The syntax of JavaScript is the set of rules that define a correctly structured JavaScript program. The examples below make use of the log function of the console object present in most browsers for standard text output.

What does () => mean in JavaScript?

It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.

How JavaScript code is written?

To write a JavaScript, you need a web browser and either a text editor or an HTML editor. Once you have the software in place, you can begin writing JavaScript code. To add JavaScript code to an HTML file, create or open an HTML file with your text/HTML editor.

What is the syntax of JavaScript value?

The JavaScript syntax defines two types of values: Fixed values and variable values. Fixed values are called literals. Variable values are called variables.


2 Answers

Vim wiki explains how to setup filetype-specific indentation, and it's pretty straight-forward: http://vim.wikia.com/wiki/Indenting_source_code#Different_settings_for_different_file_types

The simplest way is to put autocmd FileType instructions in your .vimrc file. You can specify indentation for each file type separately:

autocmd FileType javascript setlocal shiftwidth=2 tabstop=2
autocmd FileType html       setlocal shiftwidth=2 tabstop=2
autocmd FileType python     setlocal shiftwidth=4 softtabstop=4 expandtab

or set default indentation for all file types, and override it for the specific ones:

set tabstop=4
set shiftwidth=4

autocmd FileType javascript setlocal shiftwidth=2 tabstop=2                                                   
autocmd FileType html setlocal shiftwidth=2 tabstop=2
like image 104
David Avsajanishvili Avatar answered Sep 30 '22 21:09

David Avsajanishvili


I came here from google and was unsatisfied with Yi Zhao's indent file as suggested above. Still wasn't catching some nested functions of mine.

I asked around on twitter and was suggested https://github.com/pangloss/vim-javascript - with which I am far happier.

HTH,

like image 33
phillmv Avatar answered Sep 30 '22 21:09

phillmv