2

For example, suppose I have this line of JavaScript:

console.log(
   "This is a really long line that I want to format, and it is inside of a quote"
)

I'd like to select the quote line in VISUAL-LINE mode, the pres gq to format it, but it doesn't work. The result is something like this:

console.log(
  "This is a really long line that I want to
  format, and it is inside of a quote"
)

but I was hoping that it would be something like this:

console.log(
  "This is a really long line that I want to "+
  "format, and it is inside of a quote"
)

so that the result is still a proper string.

Is it possible to do this with Vim?

2 Answers 2

1

For what it is worth: :h gq explains that you have 3 ways to format your code with gq

1. If 'formatexpr' is not empty the expression is
   evaluated. This can differ for each buffer.
2. If 'formatprg' is not empty an external program
   is used.
3. Otherwise formatting is done internally.

Let's begin with the third option: the internal formatting is controlled by 'formatoptions' and :h fo-table shows that we can't achieve what you want to do with built-in options.

Now you have two choices: either find an external program which will format your code as you want or write a function used by formatexpr.

About formatexpr the help (:h 'formatexpr') doesn't tell us a lot about how to write the function. You can have a look at this Vim dev mailing list post and this answer to get some very basic examples. I think writing such a function might be a lot of trouble since you would need to re-implement the current behavior of gq and add the quotes formatting.

So I would say that your best bet is to find an external tool to do the formatting and set your formatprg properly. (Remember to set it in an ftplugin so that it only affects javascript code.)

0

Thanks to @statox, I had a go at writing something that can be used with formatprg. At the time of writing it's still a big rough but it will do what you need.

To use it, you need to install my vim script:

  1. clone the repo, it has a helper vim script and the NodeJS script that does the formatting
    git clone https://github.com/tomsaleeba/js-string-literal-breaker.git /tmp/jsslb
    # note, we're cloning to /tmp for this demo but if you like the script, clone
    # somewhere more permanent
    
  2. start vim with your test case in a buffer
    cat <<EOF | vim
    console.log(
       "This is a really long line that I want to format, and it is inside of a quote"
    )
    EOF
    
  3. source my vim script
    :source /tmp/jsslb/script.vim
    
  4. enable the formatter
    <leader>j
    
  5. move the cursor to line 2
  6. run the formatter gqq
  7. the buffer should now look like
    console.log(
       "This is a really long line that I want to format, and it is inside of a " +
       "quote"
    )
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.