How do I Be More Productive In Vim?

This is a user contributed FAQ.

ADVERTISEMENTS

Q. I’m just starting in vim text editor but I’m annoying typing and retyping similar statements. Can you address me about it?

A. Yes. Vim offer several functionalities that improve your editing. Here are some of them.

1. Copying a whole line
yy or shift y or :ny

where n is the number of line if n is not specified then copy the current line

2. Copying from current position to end of line.
y$

3. Copying 3 lines after the current position
3yy

4. Copying a range of line
:1,5y or :1y 5
The above example copy 5 lines, from 1 to 5

5. Deleting a whole line (Deleted line is copy to the registers)
dd or :nd
Where n is the number of line if n is not specified then delete the current line

6. Deleting from current position to end of line (Deleted text is copied to the registers)
d$ or shift d
7. Deleting a range of line
:1,5d or :1d 5
The above example remove 5 lines, from 1 to 5 and copy them to a register
8. Put n times a text, previously copied, after the cursor:
np

9. Put n times a text, previously copied, before the cursor
n Shift p

10. Word completion, works in insert mode

  1. Ctrl n search forward for next matching
  2. Ctrl p search backward for previous matching

Very useful in programming (perl, bash, java, other)

11. Put vim in replace mode
Shift r

12. Undo
u

13. Redo
Ctrl r

14. Repeating last change
.

15. Getting help about a topic
:help topic

Note: previous commands work in command mode except 10.

This article / faq is contributed by Yoander Valdés Rodríguez (yoander). nixCraft welcomes readers’ tips / howtos.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
5 comments… add one
  • housetier May 25, 2008 @ 21:49

    I likle this series very much. While I already know quite a few things about vim, I usually discover new tricks here. Thanks!

  • Vaishnavi Sep 17, 2008 @ 10:44

    I love Vim for one more option.
    Ctrl-P for the match it provides when you type a word(Similar to autocomplete option) in Vim. very handy when writing large function and variable names.
    You might want to try and include that aswell.

  • guest Aug 13, 2009 @ 5:49

    int main(some garbage, I want to change )

    when I am inside (…), do: ci”int argc

  • guest Aug 13, 2009 @ 6:05

    int main(some garbage, I want to change )

    when I am inside (…), do: ci)int argc

  • Jose Tapia Apr 14, 2013 @ 9:13

    Vim ROCKS!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.