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.
Resources
- I am going to use the YouTube Playing to try to Learn VIM
- A link to the VIM Online Website
- Vim online is a central place for the Vim community to store useful Vim tips and tools. Vim has a scripting language that allows for plugin like extensions to enable IDE behavior, syntax highlighting, colorization as well as other advanced features. These scripts can be uploaded and maintained using Vim online.
- neovim Website
- VIM Cheatsheet
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
- Recommends installing neovim for 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
- In a
- Create a
- 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
There are currently no comments to show for this article.