summaryrefslogtreecommitdiff
path: root/src/Vim:Frequently_Used_Bits_and_Doodads.ascii
blob: f4b4b4e72412dbe1cf6abfdd560c71f4e8a79485 (plain)
    1 Vim:Frequently Used Bits and Doodads
    2 ====================================
    3 :author: Aaron Ball
    4 :email: nullspoon@iohq.net
    5 
    6 
    7 == {doctitle}
    8 
    9 I have a friend (yes, you Zeke) who is considering changing from a graphical
   10 editor to a CLI editor, namely vim, for his web developement. His primary
   11 hesitation though is the learning curve for vim, and rightfully so. Coming from
   12 a GUI with menus to an environment you can't even exit unless you know what
   13 you're doing already is pretty intimidating (or fun
   14 https://www.youtube.com/watch?v=pY8jaGs7xJ0[if that's what you're into]).
   15 
   16 I understand this well. When I first started in vim, I was quite taken
   17 back by the tremendous amount of documentation. Lots of documentation
   18 [hopefully] means lots of functionality. Lots of documentation is great
   19 but if you don't know where to start, it doesn't do you
   20 http://hyperboleandahalf.blogspot.com/2010/04/alot-is-better-than-you-at-everything.html[alot]
   21 of good. Here I'm hoping to narrow things down a bit for you folks
   22 thinking of learning vim.
   23 
   24 
   25 [[two-modes]]
   26 == Two Modes
   27 
   28 Before we start anywhere else, you need to know that there are two modes for
   29 vim: *command mode* and **insert mode**. Since vim is a command line editor, it
   30 doesn't [technically] support a mouse (it kind of does, but we won't get into
   31 that) which means all of your controls are keyboard-based. However, if you also
   32 use the keyboard keys to write text, you're going to have a lot of conflicts -
   33 hence two modes.
   34 
   35 When you open a text file, you are first put in command mode. That means that
   36 your whole keyboard won't insert text. Instead, it interprets all key presses
   37 as commands. The first command you'll want is the letter __i__. This will put
   38 you into insert mode. Once in insert mode, all key presses will insert text. To
   39 get/escape out of insert mode, hit the escape key a la top left of yonder
   40 http://search.dilbert.com/comic/Kybard[kybard].
   41 
   42 
   43 [[frequently-used-features]]
   44 == Frequently Used Features
   45 
   46 
   47 [[find]]
   48 === Find
   49 
   50 Finding in vim is actually fairly simple. You just have to remember that it
   51 searches in two directions: forwards and backwards. To search fowards in
   52 thedocument from the present position of the cursor, type the following in
   53 command mode
   54 
   55 ----
   56 /searchterm
   57 -----
   58 
   59 To search backwards in the document from the present position of the cursor,
   60 type the following in command mode <pre>?searchterm</pre>
   61 
   62 
   63 [[replace]]
   64 === Replace
   65 
   66 This one is pretty complex unfortunately. However, for those of you who like to
   67 weild the power of the gods, find and replace in vim uses
   68 http://www.regular-expressions.info/[regex].
   69 
   70 I won't get heavily into this because this topic is so broad and can take so
   71 much time to learn because of how robust it can be. You can actually put this
   72 piece on your resume and you'll likely get quite a few bites.
   73 
   74 A basic find and replace would perform a replace on the entire document.  For
   75 example, we want to replace the word _foo_ with __bar__. To do this, do the
   76 following (actually type the : at the beginning of the expression):
   77 
   78 ----
   79 :%s/foo/bar/
   80 ----
   81 
   82 That will replace all instances of foo with bar from the beginning to the end
   83 of the document (the % here means the whole file), unless there is more than
   84 one instance of foo on a particular line. That will only replace the first
   85 instance found on each line. To replace all for real this time, just append the
   86 letter "g" for "global" (at least I think that's what it stands for).
   87 
   88 ----
   89 :%s/foo/bar/g
   90 ----
   91 
   92 
   93 [[code-folding]]
   94 === Code Folding
   95 
   96 Depending on the distribution of Linux you are using, code folding may or may
   97 not be enabled already. To enable it however, you need to type exactly the
   98 following command in command mode: +:set foldmethod=indent+. You could also put
   99 that in your +\~/.vimrc+ file and it will enable it for all future vim
  100 sessions. Next up, here's how you open and close folded code...
  101 
  102 **za/A**: Toggles code folding. Capital A toggles all folds inside of the
  103           currently selected fold.
  104 
  105 **zc/C**: Closes the folded code. Capital C closes all folds inside of the
  106           currently selected fold.
  107 
  108 **zo/O**: Opens the folded code. Capital O opens all folds inside of the
  109           currently selected fold.
  110 
  111 **zr/R**: Opens the first level of folded code throughout the file. Capital R
  112           will open all folds, including nested.
  113 
  114 **zm/M**: Closes the first level of folded code throughout the file. Capital M
  115           will close all folds, including nested.
  116 
  117 I pretty much only every use zR (open all), zM (close all), and zA (toggle all
  118 nested under currently selected).
  119 
  120 
  121 [[syntax-highlighting]]
  122 === Syntax Highlighting
  123 
  124 Depending on what distro of Linux you use, syntax highlighting may or may not
  125 be enabled for you by default. If it isn't already, there are two ways to turn
  126 'er on.
  127 
  128 When in command mode, type exactly (with the preceeding colon) <code>:syntax
  129 on</code>. That should enable syntax highlighting for you.  If it doesn't, it's
  130 possible either the file you are editing doesn't have a known extension (eg:
  131 .pl for Perl, .php for PHP, .cpp for C+\+, etc) or it doesn't start with a
  132 http://en.wikipedia.org/wiki/Shebang_%28Unix%29[shebang] that indicates what
  133 the language is (eg: #!/usr/bin/perl).
  134 
  135 
  136 [[line-numbers]]
  137 === Line Numbers
  138 
  139 I don't typically put this piece into my .vimrc file because I don't always
  140 like to see line numbers, especially when I need to copy and paste code.
  141 However, they do come in handy occasionally. For instance, when you're working
  142 on a really big perl script and you want to know what line you are presently on
  143 so when you close the file you can come back and hit ":742" and you're back
  144 where you left off. To show or hide line numbers respectively, try the
  145 following commands
  146 
  147 .Turn on line numbers
  148 ----
  149 :set nu
  150 ----
  151 
  152 .Turn off line numbers
  153 ----
  154 :set nonu
  155 ----
  156 
  157 
  158 [[reading-in-the-output-of-a-command]]
  159 === Reading in the Output of a Command
  160 
  161 This is super handy for me very often. When I'm editing a file and I need to
  162 put the the output of a command (typically data that I need) into my document
  163 for further parsing, this really saves the day. Without this, I'd have to close
  164 vim (or open a new terminal), then run the command and redirect the output to
  165 append to the end of my file. If I need it somewhere else in the file though, I
  166 then have to reopen the file in vim and and move the data around. Here's how we
  167 can read in output from a command. For this example, we'll use the output of
  168 __ifconfig -a__. Put the cursor where you want the data to go and then in
  169 command mode, type
  170 
  171 ----
  172 :read !ifconfig -a
  173 ----
  174 
  175 
  176 [[visual-mode]]
  177 === Visual Mode
  178 
  179 This one is a real live saver. If any of you have used vi, you likely know the
  180 wonders of this bit of functionality. For those of you who don't know, in old
  181 vi, to make a change to more than one line, you had to perform the action and
  182 tell vim to apply it to the current line and however many lines forwards. That
  183 means that you have to count lines. If you're going to delete say, 87 lines,
  184 that's a really big pain. With visual mode, we can highlight the lines we want
  185 to modify (delete, shave some characters off the front, indent, unindent, etc)
  186 and simply perform the action and it will be applied to all highlighted lines.
  187 
  188 To do this, in command mode, hit <code>Shift+v</code> (or capital V for short).
  189 Move the cursor up or down and you will see lines being highlighted. To delete
  190 those lines, hit the _d_ key. To indent them, hit the _>_ key. To unindent, hit
  191 the _<_ key. To copy/yank them, hit the _y_ key.
  192 
  193 To escape visual mode without making changes, just hit the escape key.
  194 
  195 
  196 [[an-awesome-cheatsheet]]
  197 === An Awesome Cheatsheet
  198 
  199 http://tnerual.eriogerg.free.fr/vimqrc.html
  200 
  201 Category:Linux Category:Unix Category:Vim Category:Editors
  202 
  203 
  204 // vim: set syntax=asciidoc:

Generated by cgit