Learning Vim

I've been editing files in the command-line more frequently recently as I have gotten into configuring servers. Editing files with with nano is difficult to do when you want to make large changes to files, so I am going to try to learn more about vim to make it easier to edit files more efficiently in the command line. It is also easier to use Windows Subsystem for Linux than Windows sometimes for editing files - another reason to use VIM to sometimes edit files using the command line.

Date Created:
Last Edited:

Resources


Notes


Vim is a highly configurable text editor built to make creating and changing any kind of text very efficient. It is included as "vi" with most UNIX systems and with Apple OS X.
Vim is rock stable and is continuously being developed to become even better. Among its features are:
- persistent, multi-level undo tree
- extensive plugin system
- support for hundreds of programming languages and file formats
- powerful search and replace
- integrates with many tools

Determine if Vim is Installed on Machine


Linux

$ command -v vim # If you don't see an output, then Vim is not installed
/usr/bin
  • Installation of vim depends on the Linux distribution you are running
$ sudo apt-get install vim # example of installing vim on Debian

Mac

  • Vim comes pre-installed on Mac

Windows


Basic Usage


  • Open vim with the vim <filename> command
  • Exit vim by pressing :q then ENTER.
  • When we open Vim, we start out in normal mode. You can't edit anything in this mode.
  • You can enter insert mode by pressing i.
  • Press ESCAPE to enter normal mode again.
  • Type :q! to exit a file without saving changes.
  • Pressing : enters you into command mode.
  • Save changes by entering :w or by entering :wq, which saves the file and exits the file.
  • Entering SHIFT+A in normal mode enters you into command mode and moves you to the end of the current line
  • How to UNDO: Go to normal mode and then press u. This UNDO-s the most recent change.
  • Pressing a 0 in normal mode moves you to the start of the line and pressing a $ moves you to the end of the line.


Modes, Navigation, and More


  • You'll always have at least one buffer in vim. A buffer in vim is like an empty or existing file.
  • In Normal Mode:
    • You can delete a character with x
    • You can delete an entire line with dd
    • You can move the cursor around with the arrow keys and the h, j, k, and l keys
  • In Command Mode:
    • :r <filename> can be used to insert the contents of the <filename> into the current file at the current cursor position
    • :w <filename> saves the file as the given <filename> instead of overwriting the current file
    • You can enter Linux commands from within Vim:
      • :! <command> you can execute commands from within vim


Getting Started with Buffers


  • A buffer is essentially a holding area for text
  • A buffer is not a file until you save it as a file
  • :e <filename> - opens up another buffer
  • :bp = buffer previous
  • :bn = buffer next
  • :enew = opens up an empty buffer


Visual Mode


  • Visual mode is activated by pressing v while in normal mode
  • As you move the arrow around in visual mode, you select an area of text
  • Type y on the keyboard while in visual mode to copy (or yank) the highlighted text
  • Type p while in normal mode to paste (or put) the copied line
  • You can cancel visual mode by pressing ESC
  • Press : then sort ui to sort highlighted text while in visual mode
  • :%s/<find>/<replace>/g to find and replace text
  • Type gg in normal mode to move to the beginning of the file, type G to move to the end of the file


Splitting the Vim Window


  • :split <filename> splits the window so that you can have multiple files open at once
    • :sp <filename> does the same thing
    • These splits are horizontal splits. You can split the screen horizontally by using :vsplit <filename>. You can also abbreviate this with :vs <filename>.
  • CTRL+w+w lets you move between files
  • Typing :q while you have split files open only closes the current split


Tips, Tricks and How to Configure Vim


  • :set number adds line numbers to a file
  • :set nonumber takes the line numbers away
  • Creating / Editing vim Configuration file:
    • Create a .vimrc in your home directory
      • In a .vimrc file, " starts a comment
  • You can specify the line number that you want to start vim at by writing the vim command like this: vim +LINE_NUMBER <filename>
  • The dd delete whole line command copies the deleted line to the clipboard (paste buffer)
  • Use the :bd command to delete the buffer (not the not the file)
  • :badd <filename> adds a buffer without switching to it
  • You can open vim in horizontal split state when writing the command vim -o <filename1> <filename2>
  • You can open vim in horizontal split state when writing the command vim -O <filename1> <filename2>



Comments

You must be logged in to post a comment!

Insert Math Markup

ESC
About Inserting Math Content
Display Style:

Embed News Content

ESC
About Embedding News Content

Embed Youtube Video

ESC
Embedding Youtube Videos

Embed TikTok Video

ESC
Embedding TikTok Videos

Embed X Post

ESC
Embedding X Posts

Embed Instagram Post

ESC
Embedding Instagram Posts

Insert Details Element

ESC

Example Output:

Summary Title
You will be able to insert content here after confirming the title of the <details> element.

Insert Table

ESC
Customization
Align:
Preview:

Insert Horizontal Rule

#000000

Preview:


Insert Chart

ESC

View Content At Different Sizes

ESC

Edit Style of Block Nodes

ESC

Edit the background color, default text color, margin, padding, and border of block nodes. Editable block nodes include paragraphs, headers, and lists.

#ffffff
#000000

Edit Selected Cells

Change the background color, vertical align, and borders of the cells in the current selection.

#ffffff
Vertical Align:
Border
#000000
Border Style:

Edit Table

ESC
Customization:
Align:

Upload Lexical State

ESC

Upload a .lexical file. If the file type matches the type of the current editor, then a preview will be shown below the file input.

Upload 3D Object

ESC

Upload Jupyter Notebook

ESC

Upload a Jupyter notebook and embed the resulting HTML in the text editor.

Insert Custom HTML

ESC

Edit Image Background Color

ESC
#ffffff

Insert Columns Layout

ESC
Column Type:

Select Code Language

ESC
Select Coding Language