Vim: plugins need not apply

Rohit Sthapit
3 min readDec 31, 2020

If you are coming from VScode or any other IDE like environment you must be confused about what plugins should I use to make VIM my goto IDE. Well, as a matter of fact, you don’t need many plugins on VIM to make it like IDE.

I will assume you are familar with vim and editing the .vimrc (config file)

Here are the 5 configurations/tips that can be useful while working with any programming language

  1. VIM Align

Ever wondered how to flawlessly align your code? Aligning it based on colon(:), assignment(=,+=,-=,*=,/=) and arrow(=>)

It is usually the case when you are working with models, config files, or JSON data.

0f=20i<Space><Esc>020lvf=hx

Place the cursor on the line you want to align then

  • 0 goes to the first column
  • f= finds the next occurrence of =on current line
  • 20i<space><esc> inserts 20spaces before =
  • 0 goes back to the first column
  • 20lforward 20 column
  • vf=hx deletes everything up to the = sign

Now, the question is do you need to do this on every single line. Well, we can use the handy feature of macros on vim where you record all the specific commands.

q<letter><commands>q
  • q start recording
  • <letter> any letter from a-z
  • q stop recording

and to execute the command Nnumbers of time

<N>@<letter>

2. Move a line

Press Shift +up/down to move the line. Well to achieve this you need to work with vim functions, then map the shift+up/downkeys to it.

Well for this you need to add the following to your .vimrc

3. Auto Indentation

Are you a messy coder? Well, most IDE has a shortcut to auto-indent the lines based on the programming language you’ve been using.

For vim, I like to map my <leader>= to command gg<S-v><S-g>= for this

noremap <leader>= gg<S-v><S-g>=
  • gg goto top line
  • <S-v> start selection of line (visual line mode)
  • <S-g> goto bottom line
  • = indent all the selected line i.e the whole file

In my case my leader is space bar

let mapleader = ' '

You can just copy line 3 into your .vimrc to achieve this

4. Ctrl + Click

Want to go to the definition of a variable or a function. Move your cursor to any variable then follow with the command gd i.e go to definition.Usually this works, however, in few cases where definition is in a separate file in javascript/Typescript we need to add extra commands.

I have mapped my <leader>gg to command gdf/gfn to handle such cases.

noremap <leader>gg gdf/gfn
  • gd go to local definition
  • f/ find / ie the file path
  • gf go to file path under the cursor
  • n go to the next occurrence of the method name

5. Adding Prefix

In cases where you want to group variables with the unique prefix you use Visual block mode <ctrl>v .

Take your cursor to the first variable you want to add prefix

  • <ctrl>v switch to visual block mode
  • Press j 4x times
  • <shift>i switch to insert mode
  • <esc>

More resources

--

--

Rohit Sthapit

Hi there, I’m Rohit — aka nhuzaa 👋 🔭 Python, JS, Vim, Arch Linux, Tmux etc all make me waaaaay too excited.