The windows command prompt is a very powerful tool for those who can utilize it to its fullest potential. However, for those who are new to it, it can be very intimidating at first. Among the many tasks you can perform in the command prompt is editing text files. You can use the command prompt to do with Vim (One of the best text editors available for this purpose). You can checkout How to Edit Text Files in the Windows Command Prompt using Notepad.

Vim is a highly customizable text editor that is known for its efficiency and speed. It is widely used by developers and administrators to make quick text edits from the command line. Vim was initially created for Unix based systems with the name VI. Once it found its way to windows, the name was changed to Vim. Apart from copying, cutting and pasting, Vim can perform more advanced functions like search, replace, syntax highlighting, and macros.

This article shows you how to use Vim to edit text files in the windows command prompt.

Let us get started.

Editing files with Vim.

Check How to install Vim on you PC so that you can follow along.

  1. The first step is to open the command prompt. You can do so by pressing the windows key + X and select Command Prompt from the menu. Alternatively, press Windows key + R and type "cmd" in the Run box and press Enter.

  2. Navigate to the folder that contains the text file you want to edit using the cd command. For example, if the text file you want to edit is in the desktop, use the following command and press Enter.

    cd desktop
    
  3. How to Launch Vim

    Once you are in the right directory, the next step is to launch Vim. If Vim is installed the right way, you should be able to launch it by typing the following command to the cmd

    vim
    

    Output:

    Img-12

    Since you are in the folder with the text file to be edited, you can use the vim command followed by the name of the text file and press Enter. ie.,

    vim textfile.txt
    

    Img-14

    This command takes you to insert mode where you can start typing, deleting or pasting text into the file.

    Note: If you find that you cant edit the file, press the i key to get into insert mode.

    Saving Files in Vim

  4. Once you are done editing, the next step is to save your changes. If you are not familiar with Vim, you might find it difficult to save files at first.

    To save files, one must exit the insert mode first. To do so, press the Esc key then type:w for writing and press Enter. This should save the changes to the file.

    Img-15

  5. How to quit Vim

    After saving the changes, quit Vim using the command :q and press Enter. Since you are still in command mode, there is no need to press escape again.

With these steps, you should be able to edit text files in with command prompt using Vim. Vim may take sometime to get used to, but once you become familiar with the commands, you'll find it is a fast and efficient way to edit text files from the command line.

You can stop here, if that is all you wanted to do, but if you want to become an expert in Vim, keep reading.

Vim Cheat sheet

Vim Modes

Vim has two modes that you are likely to use every single time.That is the Command mode that you get into by pressing Esc key and the Insert mode which you get into by pressing i key.

Basic Vim Commands

Command Action
:help Gets you the help documentation
:e [ file ] Opens the file specified in the box brackets
:w Writes/saves the file
:w [ filename ] Allows you to save a file after specifying the name you want to call it
:wq This command saves the file and quits Vim
:q! Quits Vim without saving the open file.

Vim Movement Commands

Command Action
h Moves cursor to the left
l Moves cursor to the right
j Moves cursor down one line
k Move cursor up one line
H Put cursor at the top of the screen
M Put cursor in the middle of the screen
L Put cursor at the bottom of the screen
w Put cursor at the start of the next word
b Put cursor at the start of the previous word
e Put cursor at the end of the a word
0 Put cursor at the start of a line
$ Put cursor at the end of a line
) Go tothe start of the next sentence
( Go to start f the previous sentence
} Go to start of the next paragraph or text block
{ Go to start of the previous paragraph or text block
ctrl + f Move one page forward
ctrl + b Move One page back
gg Put cursor at the start of the file
G Put cursor at the end of the file

Vim Editing Commands

Command Action
yy Copy line
yw Copy word
Y$ Copy from cursor position to end of line.
v Highlight one character at a time using arrow keys
V Highlights one line. Highlight more lines using the arrow keys
p Paste whatever is copied to the unnamed register
d Delete highlighted text
dd Delete line of text
dw Delete a word
D Delete everything from cursor position to end of line.
d0 Delete everything from cursor position to start of line.
dgg Delete everything from cursor position to the start of the file
dG Delete everything from cursor position to the end of the file
x Delete a single character
u Undo the last operation
ctrl + r Redo last undo
. repeat last action

Vim Search text commands

Command Action
/ [keyword] Search for text using the keyword specified in the box brackets
? [keyword] Search previous text for the keyword specified
n Search text again in the direction of your last search was
N Search text again in the opposite direction
:%s/[pattern]/[replacement]/g Replace all occurrences of a pattern without confirming each one
:%s/[pattern]/[replacement]/gc Replace all occurrences of a pattern with confirming each one

Vim Commands for Multiples files

Command Action
:bn Switch to next buffer
:bp Switch to previous buffer
:bd Close a buffer
:sp [filename] Open new file and split screen horizontally for multiple buffers
:vsp [filename] Open new file and split screen vertically for multiple buffers
:ls List all open buffers
ctrl + ws Split windows horizontally
ctrl + wv Split window vertically
ctrl + ww Switch between windows
ctrl + wq Quit a window
ctrl + wh Move cursor to the window on the left
ctrl + wl Move cursor to the window on the right
ctrl + wj Move cursor to the window below the one you are in
ctrl + wk Move cursor to the window above the one you are in

Vim Visual mode - Marking text

Command Action
v Start visual mode
V Start line-wise visual mode
ctrl + v Start visual block mode
ab a block with ()
aB a block with {}
ib inner block with ()
iB inner block with {}
aw mark a word
Esc exit visual mode

Vim Tab Pages

Command Action
:tabedit file open a new tab and it takes you to edit "file"
gt move to the next tab
gT move to the previous tab
#gt Move to a specific tab number ie 2gt goes to tab 2
:tabs list all open tabs
:tabclose close a single tab

Conclusion

Vim is a very powerful editor that is used mostly by programmers and administrators for its efficiency and speed.

This guide shows you how you can edit text files using vim on the command prompt. The guide also provides you with an in-depth Vim cheat sheet to get you started with Vim.

If you found this article helpful, share it and subscribe to geekbits

Thank you.

If you enjoy our content, please consider buying us a coffee to support our work:

Edit text file in CMD with notepad
cd command in Linux
Javascript Average Function
How to shutdown computer from the terminal

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.