B1KMusic

Some Vim tips

Feb 4th, 2016
805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. 1. :%y+
  2.  
  3. Copies the entire file into the clipboard. `%` means select all lines, and `y+` means copy into the `+` register, which is the clipboard.
  4.  
  5.  
  6.  
  7.  
  8. 2. <C-x> <C-f>
  9.  
  10. File completion. There are tons more if you check out `:h ins-completion`. To revert back to internal matches, use <C-x> <C-p>.
  11.  
  12.  
  13.  
  14.  
  15. 3. gd
  16.  
  17. Go to definition. For example, if editing `foo.c` inside function `bar` and looking at variable `baz`, hitting `gd` while `baz` is under the cursor will cause it to jump the the line where it was defined, e.g. `int baz = 0;`. g does a lot of stuff.
  18.  
  19. Check out `:h g`
  20.  
  21.  
  22.  
  23.  
  24. 4. zf
  25.  
  26. Fold some lines. This is especially useful if `:set foldmethod=marker`. For example, go to line `int getRandomNumber(){`, then `f{zf%` to jump to the `{` and fold to its matching `}`. After folding, it can be toggled with `za`, deleted with `zd`, you can even move between folds with `zj` and `zk`.
  27.  
  28. See `:h z`
  29.  
  30.  
  31.  
  32.  
  33. 5. <C-v>
  34.  
  35. Visual-Block mode is more useful than you think. For example, if you have a long list of lines that need something added to the beginning, select the left-edge of all of the lines and press `I` to go into insert mode. After inserting the text on the current line and going back to normal mode, it will apply the same change to all the lines that were selected. Same applies for `A`.
  36.  
  37. You can also use `s` and `r` with visual-block mode. `s` will replace the selected block with what you type on all selected lines, and `r` will replace all selected chars with the next char you type.
  38.  
  39.  
  40.  
  41.  
  42. 6. <C-x> <C-e>
  43.  
  44. Okay, this one only works in bash to my knowledge. It will start $EDITOR to edit the current command.
  45.  
  46. It's part of bash, not vim. Still useful, though.
  47.  
  48.  
  49.  
  50.  
  51. 7. gj and gk
  52.  
  53. When word wrap is enabled (default), j and k will normally move up line-wise. Using gj and gk, you can move up and down *screen-wise*.
  54.  
  55. If you read `:h g`, then you probably already discovered this.
  56.  
  57.  
  58.  
  59.  
  60. 8. :help (or :h)
  61.  
  62. This is single-handedly one of the most useful features of Vim. This is how you discover new things, or dive deeper into an obscure tip to learn the full scope of what you can do with it.
  63.  
  64. Too many people take this for granted.
  65.  
  66.  
  67.  
  68.  
  69. 9. :read/:r and :write/:w
  70.  
  71. These commands allow you to read/write from/to a file or stdin/stdout.
  72.  
  73. Of course, you already know about `:w`.
  74.  
  75. For example, `:w !sudo tee %` will write the buffer contents into the stdin of sudo, which will execute tee, which will write to both stdout and the "%" file. % is expanded by Vim to the name of the current file.
  76.  
  77. You can also do this with read, just keep in mind that it will read into the buffer.
  78.  
  79. `:r ! cat some-file | awk '{print " " $line}'` will read some-file into the buffer, but indented by four spaces courtesy of awk.
  80.  
  81. `:r some-file` will read the entire file and insert it below the cursor.
  82.  
  83. the `r` in `r ! cat ...` can be replaced with a range (this also works with write) to only work on a specific set of lines. % for all lines, for example.
  84.  
  85. `!` is Vim's special pipe symbol. Use it to interface with the shell.
  86.  
  87. Keep in mind the difference between `:w!` and `:w !`. The former forces a write (defaults to current file) and will not register as a pipe; the latter writes to a pipe, and does not save to the current file.
  88.  
  89. :h read
  90. :h write
  91. :h !
  92.  
  93.  
  94.  
  95.  
  96. 10. !!
  97.  
  98. This expands to `:.!` automatically. `:.!` will read to the current line from the pipe. Try `!!ls`, for example.
  99.  
  100.  
  101.  
  102.  
  103. 11. "*p
  104.  
  105. You know when you middle-click-paste something in insert mode, and the indentation is all funky because of `:set autoindent`?
  106.  
  107. Use "*p and "+p to paste from the primary and clipboard buffers instead.
  108.  
  109. :h "
  110. :h registers
Add Comment
Please, Sign In to add comment