DEV Community

Cover image for Vim
Omar Ahmed
Omar Ahmed

Posted on

Vim

  • 1. Match all occurrences of the word under the cursor
*
Enter fullscreen mode Exit fullscreen mode
  • 2. Replace a word with another word
:%s/word/newword/gc
Enter fullscreen mode Exit fullscreen mode

g: global , c: confirm

  • 3. Visually select a word i / a (inside / outside)
viw ( Selects only the word itself )
vaw ( Selects the word plus surrounding whitespace )
vi( ( Visual texts inside () )
ci" ( change around double "" )
va( ( Visual () )
va" ( Visual "" )
Enter fullscreen mode Exit fullscreen mode
  • 4. Show all registers
:reg
Enter fullscreen mode Exit fullscreen mode
  • 5. Paste from a specific register
"<reg number>p
Enter fullscreen mode Exit fullscreen mode
  • 6. Yank (copy) the current line to the system clipboard ( + : clipboard system register)
"+yy
Enter fullscreen mode Exit fullscreen mode
  • 7. Yank the current file name to the system clipboard
:let @+=@% , then go to any file and ctrl+v
Enter fullscreen mode Exit fullscreen mode

assigns the contents of the % register (current file name) to the + register (system clipboard).

  • 8. Copy the absolute path of the current file
:let @+ = expand('%:p')
Enter fullscreen mode Exit fullscreen mode
  • 9. Record and play a macro
q<any character><action>q
@h
Enter fullscreen mode Exit fullscreen mode

  • 10. :normal Mode
When you select lines in Visual mode and press :
:'<,'>

:'<,'>normal <keys><text>
Enter fullscreen mode Exit fullscreen mode

Keys:
I : go to start of lines and enter Insert mode
A : go to end of lines and append a new text


hello
world
goodbye

:'<,'>normal Ivar + space

var hello
var world
var goodbye
Enter fullscreen mode Exit fullscreen mode
hello
world
goodbye

:'<,'>normal A ;

hello ;
world ;
goodbye ;
Enter fullscreen mode Exit fullscreen mode
  • 11. Number
Ctrl-a : increment number
Ctrl-x : decrement number
value = 2
Enter fullscreen mode Exit fullscreen mode
list.get(0);
list.get(0);
list.get(0);
list.get(0);
list.get(0);
list.get(0);

select all lines + g + Ctrl-a

list.get(1);
list.get(2);
list.get(3);
list.get(4);
list.get(5);
list.get(6);
Enter fullscreen mode Exit fullscreen mode
  • 12. Switching Selection
select lines + o
select lines + O
Enter fullscreen mode Exit fullscreen mode
  • 13. w / W letters, digits, or underscores / whitespace characters
Lis:st.get(0) hello world
Enter fullscreen mode Exit fullscreen mode

Top comments (0)