Introduction to Text Editors: Vi(m)

Before we dive deep into the topic, you must have wondered what "vi(m)" means. I wrote it that way because any command I would be teaching you would apply to both the vi and vim text editors. VIM stands for Vi Improved, so you get the gist.

The default editor bundled with the UNIX operating system is referred to as vi, short for "visual editor". Other alternative editors for UNIX environments include pico and emacs, which is a product of GNU. The vi editor in UNIX operates in two modes:

  1. Command mode, where commands are entered to perform actions on the file.

  2. Insert mode, where typed text is inserted into the file.

In command mode, each character entered is a command affecting the text file, and typing a character might switch vi to insert mode. In insert mode, every typed character is added to the file, and pressing the <Esc> (Escape) key turns off insert mode.

Although there are numerous vi commands, beginners often find just a few essential commands sufficient. This article offers a selection of basic vi commands, with asterisks (*) marking the most fundamental and useful ones. With practice, these commands should become second nature.

It is important to note that commands in vi(m) are case-sensitive. Using an uppercase letter instead of lowercase can yield unexpected results.

Basic Vi(m) Commands

Outlined in the table below are the basic vi/vim commands. Remember that "*" is not part of the commands, but to tell you that the commands marked with asterisks are the fundamental and most useful commands.

COMMANDRESULT OF COMMAND
* vi <filename>edit filename, starting from the first line.
vi -r <filename>recover filename that was being edited when an error occurred.
* :x, then <Return/Enter>quits vi(m) and saves the file with the name specified before the edit is made.
:wq, then <Return/Enter>quits vi(m) and saves the file with the name specified before the edit is made.
:q, then <Return/Enter>quits vi(m) (gives an error if the latest changes of the file have not been saved).
* :q!, then <Return/Enter>quits vi(m) even though the latest changes of the file have not been saved.
*j or <Return/Enter> or [down-arrow]moves the cursor down one line.
*k or [up-arrow]moves the cursor up one line.
*h or <Backspace> or [left-arrow]moves the cursor left one character.
*l(small letter L) or <Space> or [right-arrow]moves the cursor right one character.
*0 (zero)moves the cursor to the start of the current line.
*$moves the cursor to the end of the current line.
wmoves the cursor to the beginning of the next word.
bmoves the cursor to the beginning of the preceding word.
:0(zero), then <Return/Enter> or 1Gmoves the cursor to the first line in the file.
:n, then <Return/Enter> or nGmoves the cursor to line n in the file.
:$, then <Return/Enter> or Gmoves the cursor to the last line in the file.
^fmoves forward one screen(page).
^bmoves backward one screen(page).
^dmoves down (forward) one-half screen.
^umove up (backward) one-half screen.
^l (small letter L)redraws the screen.
^rredraws the screen, removing deleted lines.
*uundo whatever you just did.
*iinserts text before the cursor, until <Esc> is hit.
I (capital letter i)inserts text at the beginning of the current line, until <Esc> is hit.
*aappends text after the cursor, until <Esc> is hit.
Aappends text to the end of the current line, until <Esc> is hit.
*o(small letter)opens and puts text in a new line below the current line, until <Esc> is hit.
O (capital letter)opens and puts text in a new line above the current line, until <Esc> is hit.
*rreplaces the single character under the cursor (no <Esc> needed).
Rreplaces characters, starting with the current cursor position, until <Esc> is hit.
cwchanges the current word with new text, starting with the character under the cursor, until <Esc> is hit.
cNwchanges N words beginning with character under the cursor, until <Esc> is hit. For example, c3w changes 3 words.
Cchanges (replaces) the characters in the current line, until <Esc> is hit.
ccchanges (replaces) the entire current line, stopping when <Esc> is hit.
Ncc or cNcchanges (replaces) the next N lines, starting with the current line, stopping when <Esc> is hit.
*xdeletes the single character under the cursor.
Nxdeletes N characters, starting with the character under the cursor.
dwdeletes the single word beginning with the character under the cursor.
dNwdeletes N words, beginning with the character under the cursor. For example, d3w deletes 3 words.
Ddeletes the remainder of the line, starting with the current cursor position.
*dddeletes the entire current line.
Ndd or dNddeletes N lines, beginning with the current line. For example, 3dd deletes 3 lines.
yycopies (yanks, cuts) the current line into the buffer.
Nyy or yNycopies (yanks, cuts) the next N lines, including the current line, into the buffer.
ppastes the line(s) in the buffer into the text space after the current line.
/stringsearches forward for the occurrence of the string in the text.
?stringsearches backwards for the occurrence of the string in the text.
nmoves to the next occurrence of the search string.
Nmoves to the next occurrence of the search string in the opposite direction.
:.=returns the line number of the current line at the bottom of the screen.
:=returns the total number of lines at the bottom of the screen.
^gprovides the current line number, along with the total number of lines, in the file at the bottom of the screen.
:r <filename> <return>reads the file named filename and inserts after the current line.
:w <return>writes the current contents to the file named in the original vi(m) call.
:w <newfile> <return>writes current contents to a new file named newfile.
:12, 35w <smallfile> <return>writes the contents of the lines numbered 12 through 35 to a new file named smallfile.
:w! <prevfile> <return>writes current contents over a pre-existing file named prevfile.

Note Below:

  1. Unlike GUI editors, you cannot highlight and delete text here, you can only use commands.

  2. The undo command acts like a toggle, undoing and redoing your last action. You cannot go back more than one step.

  3. Unlike GUI editors, the mouse cannot move the cursor here. You must use the key commands outlined above. Arrow keys may also work for some UNIX shells but are better avoided because they may produce unwanted behaviour.

  4. The cursor moves to the bottom of the screen whenever the ":" (semi-colon) is typed (when not in insert mode).

  5. When you type "vi(m) filename", the editor will open for you to start editing from the first line only when "filename" didn't previously exist. If it does, the editor will open to the first page of the already existing file.

Are you lost? The link to the first topic of this series is embedded below.

Conclusion

I hope you found this content as enjoyable to read as I did to write. If you are interested in more content like this, please navigate to my profile and follow me for more content of the same nature.

Connect with Me

Feel free to reach out via X or LinkedIn if you're interested in collaborating on projects or just want to chat about tech, design, or anything in-between. Let's create something amazing together!

10
Subscribe to my newsletter

Read articles from Ikechukwu Ashimonye directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ikechukwu Ashimonye
Ikechukwu Ashimonye

About Me Welcome to my developer's corner on Hashnode! I'm an Electrical and Electronics Engineering graduate with a passion for technology, creative design, and software development. My journey into the tech world began with a deep curiosity for how things work, which eventually led me to explore various facets of engineering and software. Work Experience I have a solid foundation in electrical and electronics engineering, and over the past 1-2 years, I've honed my skills in Photoshop, focusing on photo editing, minimalistic logo creation, and other creative services. My work under the brand TechGecko (TGPhotoshop) has allowed me to combine technical expertise with creativity, providing unique solutions to my clients. On the development side, I am currently building a web application called DayMinder, designed to help users manage their schedules, complete tasks, and receive timely reminders. This project reflects my interest in creating practical tools that make everyday life easier. Accomplishments Freelance Photoshop Editor: Successfully launched a freelance business on Freelancer.com and Upwork, where I offer a range of photo editing and design services. University Graduate: Completed a degree in Electrical and Electronics Engineering, gaining a strong technical background. Ongoing Web App Development: Actively working on DayMinder, a personal productivity tool, which showcases my skills in both design and software development. Interests and Dreams Creative Design: I enjoy exploring the intersection of art and technology, particularly in photo editing and design. My deep curiosity might again lead me to explore other facets of art. Who knows? Software Development: My current focus is on improving my coding skills, particularly in web and application development, and expanding my knowledge in areas like SEO and user experience design. Dreams: My goal is to continue growing as a developer and designer, eventually creating tools and products that have a meaningful impact. I also aim to establish a strong presence in the tech community, sharing my experiences and learning from others. Connect with Me Feel free to reach out on X (formerly known as Twitter) or LinkedIn via my blog home or profile page if you're interested in collaborating on projects or just want to chat about tech, design, or anything in-between. Let's create something amazing together!