12

I pasted some code into a new, unwritten buffer in Vim. I'd like to do some quick edits on the code without saving it to a file, but it hurts my eyes to look at code without syntax highlighting. Can I use syntax highlighting without writing it to a file? I don't want to write it to a file so I don't have to delete the file when I'm done.

2
  • What are you editing that youre not saving when done?
    – D. Ben Knoble
    Commented Jun 28, 2019 at 19:21
  • 1
    A SQL query that I'm going to run once.
    – aswine
    Commented Jul 1, 2019 at 14:22

2 Answers 2

15

In addition to setting the syntax, setting the filetype brings indent options (if you have those on), mappings, and other filetype specific vimscript to bear on your code. So I recommend

:set filetype=java

Which we typically abbreviate in interactive use as

:set ft=java
4

You can set the syntax highlighting by doing:

:set syntax=java

This will work in the unwritten buffer. Change java for the appropriate value.

2
  • 2
    Or even set filetype if you have mappings/other goodies to use.
    – D. Ben Knoble
    Commented Jun 28, 2019 at 19:20
  • Thanks, alanboy. That works. @D.BenKnoble That's even better. I'll mark that as the correct answer if you make an answer.
    – aswine
    Commented Jun 28, 2019 at 20:09

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.