
from __future__ import unicode_literals

import sys

import vim


ENCODING = vim.eval('&fileencoding')


def encode(text):
    if sys.version_info[0] >= 3:
        return text
    else:
        return text.encode(ENCODING)


def decode(text):
    if sys.version_info[0] >= 3:
        return text
    else:
        return text.decode(ENCODING)


if vim.eval('&syntax') == 'python':
    source = '\n'.join(decode(line)
                       for line in vim.current.buffer) + '\n'

    import autopep8
    options = autopep8.parse_args(['--range',
                                   str(1 + vim.current.range.start),
                                   str(1 + vim.current.range.end),
                                   ''])

    formatted = autopep8.fix_code(source, options=options)

    if source != formatted:
        if formatted.endswith('\n'):
            formatted = formatted[:-1]

        vim.current.buffer[:] = [encode(line)
                                 for line in formatted.splitlines()]


from __future__ import unicode_literals

import sys

import vim


ENCODING = vim.eval('&fileencoding')


def encode(text):
    if sys.version_info[0] >= 3:
        return text
    else:
        return text.encode(ENCODING)


def decode(text):
    if sys.version_info[0] >= 3:
        return text
    else:
        return text.decode(ENCODING)


def main():
    if vim.eval('&syntax') != 'python':
        return

    source = '\n'.join(decode(line)
                       for line in vim.current.buffer) + '\n'

    import autopep8
    options = autopep8.parse_args(['--range',
                                   str(1 + vim.current.range.start),
                                   str(1 + vim.current.range.end),
                                   ''])

    formatted = autopep8.fix_code(source, options=options)

    if source != formatted:
        if formatted.endswith('\n'):
            formatted = formatted[:-1]

        vim.current.buffer[:] = [encode(line)
                                 for line in formatted.splitlines()]


if __name__ == '__main__':
    main()

Put code in main function
Put code in main function

  
  from __future__ import unicode_literals
  
  import sys
  
  import vim
  
  
  ENCODING = vim.eval('&fileencoding')
  
  
  def encode(text):
      if sys.version_info[0] >= 3:
          return text
      else:
          return text.encode(ENCODING)
  
  
  def decode(text):
      if sys.version_info[0] >= 3:
          return text
      else:
          return text.decode(ENCODING)
  
  
+ def main():
- if vim.eval('&syntax') == 'python':
+     if vim.eval('&syntax') != 'python':
+         return
+ 
      source = '\n'.join(decode(line)
                         for line in vim.current.buffer) + '\n'
  
      import autopep8
      options = autopep8.parse_args(['--range',
                                     str(1 + vim.current.range.start),
                                     str(1 + vim.current.range.end),
                                     ''])
  
      formatted = autopep8.fix_code(source, options=options)
  
      if source != formatted:
          if formatted.endswith('\n'):
              formatted = formatted[:-1]
  
          vim.current.buffer[:] = [encode(line)
                                   for line in formatted.splitlines()]
  
+ 
+ if __name__ == '__main__':
+     main()
+ 
Put code in main function
## Code Before:

from __future__ import unicode_literals

import sys

import vim


ENCODING = vim.eval('&fileencoding')


def encode(text):
    if sys.version_info[0] >= 3:
        return text
    else:
        return text.encode(ENCODING)


def decode(text):
    if sys.version_info[0] >= 3:
        return text
    else:
        return text.decode(ENCODING)


if vim.eval('&syntax') == 'python':
    source = '\n'.join(decode(line)
                       for line in vim.current.buffer) + '\n'

    import autopep8
    options = autopep8.parse_args(['--range',
                                   str(1 + vim.current.range.start),
                                   str(1 + vim.current.range.end),
                                   ''])

    formatted = autopep8.fix_code(source, options=options)

    if source != formatted:
        if formatted.endswith('\n'):
            formatted = formatted[:-1]

        vim.current.buffer[:] = [encode(line)
                                 for line in formatted.splitlines()]

## Instruction:
Put code in main function
## Code After:

from __future__ import unicode_literals

import sys

import vim


ENCODING = vim.eval('&fileencoding')


def encode(text):
    if sys.version_info[0] >= 3:
        return text
    else:
        return text.encode(ENCODING)


def decode(text):
    if sys.version_info[0] >= 3:
        return text
    else:
        return text.decode(ENCODING)


def main():
    if vim.eval('&syntax') != 'python':
        return

    source = '\n'.join(decode(line)
                       for line in vim.current.buffer) + '\n'

    import autopep8
    options = autopep8.parse_args(['--range',
                                   str(1 + vim.current.range.start),
                                   str(1 + vim.current.range.end),
                                   ''])

    formatted = autopep8.fix_code(source, options=options)

    if source != formatted:
        if formatted.endswith('\n'):
            formatted = formatted[:-1]

        vim.current.buffer[:] = [encode(line)
                                 for line in formatted.splitlines()]


if __name__ == '__main__':
    main()
