viref.txt 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. File: viref.txt
  2. # resources
  3. Repository for vim plugins - https://vimawesome.com
  4. Vim screencasts - http://vimcasts.org
  5. Repository for vim color schemes - http://vimcolors.com
  6. Vim tips repository - https://vim.fandom.com/wiki/Vim_Tips_Wiki
  7. Effective text editing (with vim) - https://moolenaar.net/habits.html
  8. Using template files in vim - https://shapeshed.com/vim-templates/
  9. vim --version # info on .vimrc file
  10. Read: vimtutor, vim man page,
  11. /usr/share/vim/vim74/doc/^.txt # The Vim documentation files
  12. Use ":help doc-file-list" to get the complete list.
  13. # info on autocmd
  14. :help autocmd
  15. :h autocmd
  16. The default shift is eight spaces (right or left). This default can be changed
  17. with a command such as:
  18. :set shiftwidth=4
  19. vi provides automatic indentation control. To use it, issue the command:
  20. :set autoindent
  21. /usr/share/vim/vimrc # system wide Vim initializations
  22. ~/.vimrc # your personal Vim initializations
  23. # Vi options [~/.vimrc]
  24. echo set number >> ~/.vimrc # shows line numbers when vi opens a file
  25. echo set nonumber >> ~/.vimrc # don't show line numbers
  26. echo syntax on >> ~/.vimrc # turns on syntax highlighting
  27. echo set tabstop=4 >> ~/.vimrc # sets the tab size to 4 spaces (default is 8)
  28. echo set autoindent >> ~/.vimrc # carries over previous indent to the next line
  29. echo set noautoindent >> ~/.vimrc # turn off autoindent
  30. echo set showmatch >> ~/.vimrc # show matching set of parentheses as they are typed
  31. echo set noshowmatch >> ~/.vimrc # turn off showmatch
  32. echo set showmode >> ~/.vimrc # display mode on last line of screen
  33. echo set noshowmode >> ~/.vimrc # turn off showmode
  34. [ctrl] G # display the name of the current file and the cursor position in it
  35. :s/^/#
  36. :1,4s/^/#
  37. :1.4s/^#
  38. :1,4s/^/#/g # s - substitute
  39. :1,4s/^#/g # uncomment
  40. :%s/^/#/g # comment all the lines in the file
  41. # search and replace
  42. :%s/apple/pear/g # replaces every occurances of apple with pear
  43. :%s/apple/pear/gc # c can be added to the end to ask for confirmation
  44. ctrl+w and v for spliting screen vertically
  45. # editing multiple files at a time
  46. vi file1 file2 file3
  47. (First, vi will open file1. To switch to the next file (file2), we need to use
  48. the :n command. When we want to return to the previous file, :N will do the
  49. job.) :buffers # the buffers command will show a list of the file currently
  50. being edited
  51. Basic & Essential Vim Commands:
  52. i - Enter insert mode
  53. Esc - Enter command mode
  54. x - Delete a character
  55. X - Delete character in backspace mode
  56. u - Undo changes
  57. Ctrl+r - Redo changes
  58. yy - Copy a line
  59. dd - Delete a line
  60. p - Paste the content of the buffer
  61. /<search_term> - Search and then cycle through matches with n and N
  62. [[ or gg - Move to the beginning of a file
  63. ]] or G - Move to the end of a file
  64. :%s/foo/bar/gci - Search and replace all occurrences with confirmation
  65. Esc + :w - Save changes
  66. Esc + :wq - Save and quit Vim
  67. Esc + :q! - Force quit Vim discarding all changes
  68. vi -d file1 file2 # Start in diff mode. There should be two, three or four file
  69. name arguments. Vim will open all the files and show
  70. differences between them. Works like vimdiff(1).
  71. vi -h # Give a bit of help about the command line arguments and options.
  72. After this Vim exits.
  73. vi -l # Lisp mode. Sets the 'lisp' and 'showmatch' options on.
  74. vi -m # Modifying files is disabled. Resets the 'write' option.
  75. You can still modify the buffer, but writing a file is not possible.
  76. vi -M # Modifications not allowed. The 'modifiable' and 'write' options will
  77. be unset, so that changes are not allowed and files can not be written.
  78. Note that these options can be set to enable making modifications.
  79. vim -v # Start Vim in Vi mode, just like the executable was called "vi". This
  80. only has effect when the executable is called "ex".
  81. ## Deleting characters, words and lines
  82. x delete the character at the cursor location
  83. dw delete the current word
  84. D delete the remainder of the line after the cursor
  85. dd delete the current line
  86. ## Looking for strings
  87. /string # find the first occurrence of string after the cursor
  88. ?string # find the first occurrence of string before the cursor
  89. n # find the next occurrence in the last search
  90. ## Replacing strings
  91. n,ps/str1/str2/g between line numbers n and p, substitute all (g:global) occurrences
  92. of str1 by str2
  93. 1,$s/str1/str2/g in the whole file ($: last line), substitute all occurrences of
  94. str1 by str2
  95. Note: http://vimsheet.com/advanced.html
  96. # all tabs into spaces
  97. :ret
  98. :se et (:set expandtab) # to make sure that any new lines will not use literal tabs
  99. # To copy lines 1 through 20 of the current file to your current cursor position, use
  100. this command:
  101. :1,20 co .
  102. # cit
  103. delete the tag
  104. # read contents from a file
  105. :r! sed -n 147,227p /path/to/file
  106. # convert tabs to spaces
  107. :set list # list tabs
  108. :set expandtab # use expand tab to convert new tabs to spaces
  109. :set tabstop=4 shiftwidth=8 expandtab # use expand tab to convert new tabs to spaces
  110. :retab # convert existing tabs to spaces
  111. :set nolist # normal viewing
  112. # search and replace
  113. :%s/text1/text2
  114. . for the current position
  115. .+10 for 10 lines down
  116. % for the whole file
  117. '<,'> for the lines of visually selected block
  118. 'a,'b from mark a to mark b
  119. .,$ from the current line to the end of the file
  120. # tabs
  121. :o <filename> # open files in vi text editor
  122. :e <directoryname>/<filename> # open files in vi text editor using tab
  123. :tabedit <filename> # open files in tab mode
  124. gt # forward tab
  125. gT # backward tab
  126. 2gt # second tab
  127. vim -p file1.txt file2.txt # opening multiple files in vim in tab mode
  128. # saved sessions
  129. : vsplit # vertical split
  130. : split # horizontal split
  131. : mksession file.vim # save a session
  132. $ vim -S file.vim # open the saved session
  133. # spell check (~/.vim/spell/)
  134. :set spell
  135. :set spelllang=en_us,de_de,fr_ch # spell check with english, german, french
  136. # file type
  137. :set filetype=c
  138. # let vim do typing
  139. to repeat previous typing ----> press . (normal mode)
  140. to complete any previous text ----> press ctrl + p (insert mode)
  141. ctrl + p and ctrl + n loops the previous text in backward and forward mode
  142. # file explore
  143. :Explore
  144. :Hex
  145. :Sex
  146. :Lex
  147. :Lex!
  148. # shift indent text (visual block mode)
  149. ctrl + V ----------> visual mode
  150. then press < for towards left and > for towards right
  151. # recall commands from history
  152. Vim records the commands that we enter in command-line mode and provides two
  153. ways of recalling them: scrolling through past command-lines with the cursor
  154. keys or dialing up the command-line window. Let's switch to command-line mode
  155. by pressing : key. Leave the prompt empty; then press the <UP> arrow key. The
  156. command-line should be populated with the most recent command that we executed.
  157. We can use the <UP> key again to go further back through our command history or
  158. use the <DOWN> key.
  159. # vim to shell
  160. :sh
  161. # shell within vim (Ctrl+W Ctrl+W toggle between vim and shell)
  162. :term
  163. :terminal
  164. # options available inside vim
  165. :options
  166. /tabstop # to get info regarding tabstop
  167. :q # to quit
  168. # tabpage
  169. :h tabpage
  170. # vim environment variables
  171. :echo $VIM
  172. :echo $VIMRUNTIME
  173. # compile python (% - current file)
  174. :!python %
  175. # highlight white spaces
  176. / $
  177. # move to the last edit
  178. `.
  179. # source the edited .vimrc file
  180. :so%
  181. # set/change the color scheme
  182. :colorscheme <press tab to toggle>
  183. # edit commands
  184. Text object Change Delete Copy(yank)
  185. One word cw dw yw
  186. Two words, whitespace separted 2cw or c2w 2dw or d2w 2yw or y2w
  187. Three words back 3cb or c3b 3db or d3b 3yb or y3b
  188. One line cc dd yy
  189. To end of line c$ or C d$ or D y$
  190. To beginning of line c0 d0 y0
  191. Single character r x or X yl or yh
  192. Five characters 5s 5x 5yl
  193. # movement
  194. To first character of next line +
  195. To first character of previous line -
  196. To end of word e or E
  197. Forward by word w or W
  198. Backward by word b or B
  199. To end of line $
  200. To beginning of line 0
  201. To a particular line G
  202. There are vi commands to scroll forward and backward through the file by full
  203. and half screens:
  204. ^F - scroll forward one screen
  205. ^B - scroll backward one screen
  206. ^D - scroll forward a half screen (down)
  207. ^U - scroll backward a half screen (up)
  208. # read manual page
  209. pressing shift+k in keyword opens the manual page of that keyword
  210. # <leader>
  211. By default <leader> is \, backslash. You can check it with:
  212. :echo mapleader
  213. If this gives you an E121: Undefined variable: mapleader, it means it's set to
  214. the default of \.
  215. You can easily remap it. To mapped it to the space-bar:
  216. :let mapleader = "\<Space>"
  217. # open terminal inside vim
  218. :vert term
  219. ctrl-w ctrl-w # move focus to the next window
  220. # check file <tab> / <space>
  221. $ cat -etv makefile
  222. [it show line starting by ^I if <tab> is given to that line and it end the line by $]