How to use Vim Editor to Save Python Script in Linux In Python

Step 1: Create and open the file using Vim

We are going to create a file and start writing our Python script using Vim. For that, open your terminal and write the following command.

vim script.py

The above command will open vim editor with the file name “script.py” and then we can start writing script in the file.

Starting vim editor

Step 2: Vim and python script

Vim editor has two modes:

Command mode: This mode is used for running commands to save file, undo changes and many more. In this article we are going to see the general commands that are used to save files. You can search for vim cheatsheet as vim has a lot of commands.

Insert mode: In this mode, we write the stuff we want to. All writing related stuff is done in Insert mode.

How to switch between these two modes?. Well it is quite simple, for that you have to check, if you are in insert mode or command mode.

  • esc: press escape key to switch to command mode

Command mode

  • i : Press “i” to switch into insert mode

insert Mode

The above picture explains the mode in which you are. If it says “Insert” You are in insert mode and write your stuff.

Step 3: Writing script and saving the script

before writing make sure to switch to insert mode by pressing “i” key on your keyboard. Now lets write a small script and save it.

Python Script

After writing our script, it’s time to save the script. For that make sure to switch to “Command mode” by pressing “esc” key.

commands to save file:

  • :wq – This command will save the file and quit the vim editor
  • :w –This command will save or write the file but didn’t quit the vim editor
  • :q -this command will simply quite the vim and your work will go in vain.

Saving the file

After entering the command press enter and then your script will be saved in your present working directory.

To run the python script simply run the following command:

python3 script.py

Output:

Output

How To Save Python Scripts In Linux Via The Terminal?

Linux users like to do tasks using the terminal as it gives the user more power to do anything with the system whether it is to store, create, or remove files. Other than file management and system control tasks, the terminal is used to write scripts for any programming language. Python and Bash scripts are mostly typed using a terminal and it is best practice to do so, as it makes the programmer more versatile.

In this article, we are going to look into how can we write and save Python scripts using a terminal with terminal editors like Vim and Nano.

Table of Content

  • Using Vim Editor to Save Python Script in Linux
  • Using Nano Editor to Save Python Script in Linux
  • How To Save Python Scripts In Linux Via The Terminal – FAQs

Similar Reads

Using Vim Editor to Save Python Script in Linux

Step 1: Create and open the file using Vim...

Using Nano Editor to Save Python Script in Linux

Step 1: Starting nano editor...

How To Save Python Scripts In Linux Via The Terminal – FAQs

How do I create a new Python script file in Linux using the terminal?...

Conclusion

In this article we discussed how to use the terminal in Linux to write and save Python scripts using Vim and Nano editors. It explains basic commands and steps for creating, editing, and saving scripts. Additionally, it provides answers to common questions, suggesting best practices for organizing scripts and using version control. By mastering these techniques, users can efficiently manage Python projects in Linux....

Contact Us