Vim Tips and Tricks — for faster editing

Manish Kumar
4 min readApr 17, 2020

Hello Everybody,

Vim is one of the most powerful text editor. It is an improved version of VI editor.

Vim turns out to be frustrating for some early users but that could be avoided if they know the basics of vim.

Here I am going to share some quick tips for fast editing, navigating and searching in text files opened via Vim.

This post is mainly for beginners struggling to find keymaps and shortcuts for Vim editors. I would also like to explain the shortcuts along with their usage. I am sharing the keymaps which are very important and useful for beginners

Vim editor after configuring various changes.

#1. The problem of navigating through a large file — NAVIGATION

In Vim basic default navigation keys which are useful and need to be remembered are :

  • Arrow keys work as usual
  • 0 : go to start of the current line
  • $ : go to end of the current line
  • e : move forward word by word
  • b : move backward word by word

Ever stuck scrolling by arrow keys in 500 line file. These keys can save a lot of your time.

  • Ctrl + f : Move down one page
  • Ctrl + b : Move up one page
  • G : Move the cursor to the bottom of the file
  • gg : Move cursor to top of the file

#2. The Problem of Cut Copy Paste in Vim File — EDITING

Firstly I would like to share that Vim editor maintains its own buffer (yank) to store the copied text. Thus when you copy text from somewhere outside the Vim editor, you won't be able to paste it in the currently opened vim file.

Curious, what Yank means?

Yanks mean to pull. The text is pulled into a register for further use. i.e to paste.

Before copying, you need to mark the text which you need to copy.

Press Esc to exit Insert Mode and then press v to enter the visual mode.

Now mark via arrow keys or the above-mentioned navigation keys.

  • y : to copy(yank) the marked text.
  • p : to paste the above-copied text.

For copying the text from vim file to the system clipboard,

  • “*yy : to copy to the system clipboard
  • “*p : to paste from the system clipboard into vim file.

There is a shortcut to map so that cmd+c and cmd+v works the same in vim files.

create/add the following lines in .vimrc file :

vmap <C-c> “+yi
vmap <C-x> “+c
vmap <C-v> c<ESC>”+p
imap <C-v> <C-r><C-o>+

The above lines execute the copy/paste to be done on system clipboard rather than on vim buffer using the ctrl/cmd + c and cmd+v.

#3. Basic useful commands to remember

  • :q! : force close if file has changed and not save changes
  • :wq! : write/save and close ( override)
  • dd : delete the current line
  • u : undo
  • ctrl+r : redo
  • Search in Vim: /<word> and press n to find next occurrences.
  • o : enter a new line BELOW the cursor and insert text.

#4. Saving a read-only file edited in vim

There are times when we open a file that is in read-only mode, we make the changes and while saving we come to know that the file needs to be opened via sudo command. I face this problem many times in a day, so here’s a workaround

The wayout of this without exiting the file & writing it there is :

  • Press Esc and type :
:w !sudo tee > /dev/null %

What the above command does

:w = write a file

!sudo = excute the shell sudo command

tee = the output of vim file redirected is redirected to tee

/dev/null = is a black hole where any data sent, will be discarded

% = using the current file name

Also I have set a shortcut on .vimrc , Just type after updating .vimrc incase you forgot to open file as sudo.

:w!!

After typing this, exit the file using :q!

#4. Quick shortcuts and settings for vim

I would also like to share some .vimrc basic file configuration, which I recommend you to use as per requirement. I recommend you to copy only those to .vimrc which you think can be useful for you

Go to your home folder :

cd ~
vi .vimrc

add the below lines to the .vimrc file.

" highlight search terms
set hlsearch
" search incrementally
set incsearch
" ignore case in searches...
set ignorecase
" show line numbers
set number
" show matching bracket(briefly jump)
set showmatch
" automatically indent lines and intelligently
set autoindent
" backspace 'normally'
set smartindent backspace=indent,eol,start
syntax on
colorscheme desert
" For quick copy/paste using cmd+c and cmd+v
vmap <C-c> "+yi
vmap <C-x> "+c
vmap <C-v> c<ESC>"+p
imap <C-v> <C-r><C-o>+
" Allow saving of files as sudo when forgot to start vim using sudo.
cmap w!! w !sudo tee > /dev/null %

Just Incase, If I have missed out on something please let me know, how I can improve and modify the above.

😜 FYI: This is my first article on Medium. Please share your comments, if you find this useful.

--

--

Manish Kumar

Working as a Full Stack developer at Cardekho. Exploring new things in my part-time. A travel Enthusiast.