Skip to content

Instantly share code, notes, and snippets.

@deckarts
Last active March 4, 2020 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deckarts/8e4df75b15cbf2e4a6be0294270211a7 to your computer and use it in GitHub Desktop.
Save deckarts/8e4df75b15cbf2e4a6be0294270211a7 to your computer and use it in GitHub Desktop.
A Productive Start with Vim

Next Level Vim

It is definitely worth the time it takes to learn text manipulation with the ubiquitous Vi(m) command line text editor. There is a visual version, but we're going to focus on the command line interpreter version. If you only know how to open a file and enter text, edit some text, and especially how to save edited files and exit the program you will be much better off for it.

Vim is readily available in nearly all modern Linux (or BSD) distributions at the terminal emulator shell command prompt. Once you've defined Vim as your default editor for your user shell, then you can navigate builtin utilities like $ man etc. using familiar Vim key bindings. We'll highlight how to do that with bash and zsh, now distributed by Apple on the Mac since Catalina.

Bash

# ~/.bashrc (or alternatively) ~/.bash_profile
# set default editor to Vim
export EDITOR=vim

Zsh

Apple's Darwin (FreeBSD) recently switched default terminal emulator shell from bash to zsh "Z" shell):

#  ~/.zshrc (or alternatively) ~/.zprofile
# set default editor to Vim
export EDITOR=vim

Circumstances where you will find it extremely convenient to know Vim nearly always involve tasks running remote shell operations. If you regularly use Secure Shell $ ssh user@hostname.provider.com and work with Virtual Private Servers (VPS), or with local virtualization containers for that matter, then you could benefit greatly from having strong Vim skills.

Run Time Commands

It is through run time command files that you set personal preferences. The first setting that you're likely going to want is to switch its legacy Vi compatibility mode to off. As a super set of Vi, everything in Vi is pretty much available and vastly improved for you in Vim. You get many advanced features, as well. The latest version (8.2) allows you to open a terminal as a sub process shell running in a split window.

" ~/.vimrc 
" ensure that legacy compatibility mode is off
" documentation: http://vimdoc.sourceforge.net/htmldoc/options.html#'compatible'
set nocp

Setting legacy compatibility off might not seem like it's doing anything (and in fact it might not be doing anything). Vim automatically switches the mode off by implication when it encounters a .vimrc file. It can still be important at times to have it turned off explicitly. The shorthand "nocp" is synonymous with "nocompatible," which also works. There are many "timtowtdi" conveniences you can learn for switching preferences as you work.

The notion of Vim's "modes" is very important to learn about, especially the difference between the very distinct "Normal" and "Insert" modes. Confusion about modes is what trips up most new users. Modes aren't unique to Vim nor were they introduced by Vi. Command mode is so old that it predates the invention of copy and paste functionality in the 1970s.

Important modes

  • Normal mode
  • Insert mode (includes Replace)
  • Visual mode
  • Command (Line) also Ex Command or Last Line mode

Use Vimtutor $ vimtutor to interactively learn about movement, modes, and running Ex Commands in "Last Line" mode. Indispensable productivity operators then become:

:E (opens explorer for locating files and directories)

. (repeat the last edit action)

; (repeat the last motion or movement forward, use "," for backwards)

/ (search document forwards, use ? for backward)

* (find next occurrence of the word under the cursor, use # for previous occurrence)

~ (toggle case)

% (toggle between opening and closing (), [], and {} which is highly useful for coding)

z= (spelling suggestions)

While it's important to commit the operator 'language' of Vim to memory, the challenge to mastery is to learn to think like a musician and combine operators and movements into key chords in harmony so that you can play Vim like a piano. That's where the power of text manipulation using Vim rivals that of the other notable command line editor, Emacs.

Where using one of these editors will wear down your ESC key, using the other will wear down your CTRL key.

Key Chords

It's sometimes convention when describing key chords to designate the CTRL key using the capital letter C followed by a hyphen. It's certainly not always the case, but we will follow that convention and clarify when there is any potential to mix up the two. Therefore, C- is a stand-in for a CTRL key chord combination from here onward.

If you start typing long lines in Vim, you're going to want to set it to wrap your text. To personalize Vim for the way you work, it's smart to start thinking about that setting. How would you like the default switch for wrapping when Vim starts? On or off? If you're like me, you like it off and leave it out of the run time commands file. When I want text to wrap, I simply set it in Command Line mode with :set wrap and voila!

There's nothing wrong with having Vim set to wrap text by default. It's simply a matter of your personal preference which can change over time. The same goes for handling paste, code language indent syntax, and the tab key (tabs or spaces and how many spaces then?). All these options for default behavior are entirely configurable and changeable in real time as you work with Command Line mode operations.

You will find many suggestions for how to set Vim defaults in community forums, at Vim wikis, and in articles like this one. Setting preferences for your personal computing environment should be a fairly familiar task for you and Vim is going to be no different. It is highly recommended that you start with very small changes to your settings and to go slowly adding so you can easily revert settings. You might avoid the use of plugins for years, or entirely.

Splits, Tabs, and Terminals in 8.2

The last topic we'll cover in this first installment of Vim is the notion of splitting your screen. There are two ways to split your working files into different views so they appear side-by-side, or switch between them with full (window) screens via tabs. Both of these changes to your application window are initiated from Command Line mode, which requires the : for calling up the prompt.

Each window split can host a file open for editing and you can arrange tabs to switch between additional files as much as you like. There is limited screen space for splits so your tabs come in handy immediately when you want more screens to split. It's purely a matter of preference for how you decide to work. In order to split a window horizontally use :sp and :vs for vertical splits.

As of Vim 8.2 you can now open a terminal shell sub process in a vertical split with :vert term to run operations on the command line right alongside your code. You would need to type exit in order to close your terminal process, just like you would to end the shell session anyway, but for splits and tabs you close them the same way you would :q with any ordinary Vim window.

To initialize a tab, use a special edit command: :tabedit which automatically switches you to the new open tab. If you gave the command a file name as an argument that file would be open for editing. Or if you neglected to, the Command Line mode edit :e filename.txt works just like it would in any ordinary Vim window. Navigate tabs with next :tabn and previous :tabp commands.

When you start to use splits, you need to know how to navigate between them using the key chord combinations of C-w plus a movement key in the direction you want to move such as left with h, down with j, up with k, or to the right with l. When you want to learn more key chords specific to splits and tabs, read the :help split and :help tabpage for the Vim manual entries.

Remembering Help

While the Vim manual is referenced in Vimtutor, it's worth repeating the tip to open :help here and now so that you can spend time with the editor on your own to get productive without wholly relying on articles such as this one. Experience is key to Vim mastery. The experience itself contributes to your computing intuition with other things since so much of what has gone into Vim is very much drawn from the builtin Unix universe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment