A Vim Primer: Setup and Keyboard Shortcuts
Last year, I made the switch from Sublime to Vim as my text editor. Between matching the tools I had in Sublime, and learning the keyboard, navigation, the learning curve was steep; However, I’m moving faster now than I ever have. Below are some general setup tools I used to get going, as well as keyboard shortcuts I have used the most. Disclaimer: I’m still a Vim noob; I’d love any suggestions!
A Good Starting Point
As a longtime Sublime user, I had grown accustomed to some of the amazing tools it provided, such as the file tree, linting, advanced searches, and snippets with tools like Emmet. I needed to at least match these if Vim was to be a viable option for me.
After some research, I found YADR, a vim-centric dotfile repo. It proved to be an amazing starting point for me, giving me much of the above, and further configuration for the command line. I highly recommend trying it out, even if you only use vi on a limited basis. The updates to the Vim keymaps, theme, searching, git integration are just a few. You can find a rundown here.
I forked the repo and made my own tweaks, based on tools I liked. The added bonus is that this gives me a good starting point on any new machine.
A Note on Setup
Vim’s config lives in the
~/.vimrc file. YADR symlinks their own config file from the root of the repo. If you make a change, it’s a good idea to commit it to your YADR repo so you’ll have it for later.
Two keys you use often in Vim are Escape and Control. To make things a bit easier on my hands, I remapped them.
First I remapped pressing
jj quickly to escape. This way when I need to switch between modes I can do so without removing my hands from the home row on the keyboard. Just add this to your .vimrc:
:inoremap jj <Esc>
Secondly, I remapped my Caps Lock key to Control. To do this on a Mac, go to System Preferences>Keyboard>Modifier Keys
By no means is this an exhaustive list, but I find these to be the commands I’ve used the most in my first year as a Vim user.
There are more than four modes in Vim, but these are the ones I used mostly.
i insert mode (for typing) v visual mode (for selecting areas of text) V visual line mode (for selecting multiple lines of text) : command line mode (for entering commands, such as saving a file)
. repeat last event h left j down k up l right w go to end of next word b go to start of previous word 0 go to start of line $ go to end of line gg to start of file G go to end of file
,+t File search ,+b Recently opened files :windo do command to all windows :e reload current file :windo e reload all windows :q quit file :w save file :wa save all files :explore file navigation in current split // These are specific to the NERDtree file navigator, with comes with YADR and I use frequently ctrl+\ Show File tree in left split ctrl+$ Show current file in left split m show menu r refresh current directory R refresh root directory
These let you split the vim pane into smaller panes, like in the screenshot above.
vv Vertical split ss horizontal split ctrl + w _ Max out the height of the current split ctrl + w | Max out the width of the current split ctrl + w = Normalize all split sizes, which is very handy when resizing terminal ctrl + w + arrow keys resize one line at a time Ctrl+W R Swap top/bottom or left/right split Ctrl+W T Break out current window into a new tabview Ctrl+W o Close every window in the current tabview but the current one
:%y+ copy all dd cut current line yy copy current line yw copy current word dw cut current word x cut current letter viwp replace current word with yanked content
gcc toggle comment line gcp toggle comment block
== auto indent line =G auto indent to end of file =5 auto indent next 5 lines
yss<div> surround current line with div ysiwt<div> surround next word with div cs"<q> change surround from " to q tag ,# ," ,' ,] ,) ,} surround a word in these wrappers ds delete a surrounding cs change a surrounding ys add a surrounding yS add a surrounding and place the surrounded text on a new line + indent it yss add a surrounding to the whole line ySs add a surrounding to the whole line, place it on a new line + indent it cit delete inside html tag and enter insert mode ci" same, but inside quotes
:%s/a/b/g find all occurances of a in file and replace with b :%s/a/b/gc same, but ask for confirmation on each / search on page n next result N previous result // clear search :Ag search in all files :Ag -C 5 print 5 lines of context before and after a match k search in all files for current word Ag --python search only Python files e open search file and close search split h open in horizontal split v open in vertical split
Marks (can set from a-z)
Mark lines in the code for quick navigation later.
ma set mark for letter a 'a go to mark for letter a
Edit multiple lines at once.
,mc multi cursor mode (can be used in visual-line mode to edit each line) ctrl-n next occurance ctrl-p previous ctrl-x skip
zR open all folds zC close all folds za toggle current fold
Toggling paste mode
This sets inline to paste mode, which lets you pasted in code with cleaner indents.
:set paste :set nopaste
:mks my-session.vim vim -S my-session.vim
I hope this helps anyone out there who wants to start with Vim. It looks daunting, but the payoff is a big one. I am more productive now than ever. Let me know in the comments any thoughts!
Leave a Reply