Wikia

Vim Tips Wiki

Emallove

5 Edits since joining this wiki
July 14, 2008

Some of my favorite Vim commands ... Edit

Thu Oct 20 10:50:27 2005 Edit

if wrap is on, go to the end of screen:

   g$

---

Good for command line stuff:

   h ;

---

   h sidescroll

---

   h changelist

(jump to changes)

---

How can mouse scrolling of the scrollbar be imitated in ex mode with only keystrokes?

e.g., i want to bring the last line of the file to the center of the screen, but can only do so with the scrollbar!

answer:

   :h scroll-down

ctrl-e

---


Fri Oct 21 17:57:33 2005 Edit

   h ctrl-w
   ctrl-w j   " go down windows
   ctrl-w k   " go up windows


Tue Dec 06 11:31:10 2005 Edit

jump to next/previous marks (set marks using m[a-z] or m[A-Z])

   [' 
   ]'

---

h helpgrep

page through the search results w/ :c[nN]ext or view them all with :cwindow

---

   @@
   @: repeat last ':' command

Thu Dec 15 11:33:45 2005 Edit

Q: how to enter multiple ':' commands from the cmd-line window (q:)? A:

   :command1 | command2 | ...


Fri Dec 16 12:04:09 2005 Edit

adjust readmeFold so that it doesn't execute on very large files (e.g., set the condition at 100,000 lines? or x number of KB?)

---

set offset to another search!

   let @J = "/|FAIL/;/?### RTE:?

use lower case j to overwrite the register ...

   let @j = "/|FAIL/;/?### RTE:?

do @J in normal mode to execute the register.

---

set scrolloff to 999 to keep the cursor in the middle of the window


Thu Jan 05 13:45:39 2006 Edit

EXECUTING COMMANDS ON FOLDS ~

   :[range]foldd[oopen] {cmd}			*:foldd* *:folddoopen*
           Execute {cmd} on all lines that are not in a closed fold.
           When [range] is given, only these lines are used.
           Each time {cmd} is executed the cursor is positioned on the
           line it is executed for.
           This works like the ":global" command: First all lines that
           are not in a closed fold are marked.  Then the {cmd} is
           executed for all marked lines.  Thus when {cmd} changes the
           folds, this has no influence on where it is executed (except
           when lines are deleted, of course).
           Example: >
               :folddoopen s/end/loop_end/ge
   <		Note the use of the "e" flag to avoid getting an error message
           where "end" doesn't match.
   :[range]folddoc[losed] {cmd}			*:folddoc* *:folddoclosed*
           Execute {cmd} on all lines that are in a closed fold.
           Otherwise like ":folddoopen".

---

instead of visually selecting entire file, use '%':

   :%<some command here>

% equal to 1,$ (the entire file) *:%*

---

                               *search-range*
   You cannot limit the search command "/" to a certain range of lines.  A trick
   to do this anyway is to use the ":substitute" command with the 'c' flag.
   Example: >
      :.,300s/Pattern//gc
   This command will search from the cursor position until line 300 for
   "Pattern".  At the match, you will be asked to type a character.  Type 'q' to
   stop at this match, type 'n' to find the next match.

---

                               *gd*
   gd			Goto local Declaration.  When the cursor is on a local
               variable, this command will jump to its declaration.
               First Vim searches for the start of the current
               function, just like "[[".  If it is not found the
               search stops in line 1.  If it is found, Vim goes back
               until a blank line is found.  From this position Vim
               searches for the keyword under the cursor, like with
               "*", but lines that look like a comment are ignored
               (see 'comments' option).
               Note that this is not guaranteed to work, Vim does not
               really check the syntax, it only searches for a match
               with the keyword.  If included files also need to be
               searched use the commands listed in |include-search|.
               After this command |n| searches forward for the next
               match (not backward).
               {not in Vi}
                               *gD*
   gD			Goto global Declaration.  When the cursor is on a
               global variable that is defined in the file, this
               command will jump to its declaration.  This works just
               like "gd", except that the search for the keyword
               always starts in line 1.  {not in Vi}

---

It would be nice if one could visually select text in diff mode, and have that text not included in the diff operation.

---

write a macro | mapping | script | thingy to open all files visually selected

foo bar foo1 bar1

so vselect the above and do a <leader><something> and this is performed:

   ! gvim `find . | grep -w \<foo\>|\<bar\>|\<foo1\>|\<bar1\>`


Thu Jan 19 13:59:20 2006 Edit

multiline register assignments are really ugly! another way?

   let @c = "/*" . "\n * Copyright 2006 Sun Microsystems, Inc.  All rights reserved." . "\n * Use is subject to license terms." . "\n *" . "\n * The contents of this file contain proprietary material of Sun" . "\n * Microsystems, Inc., and are subject to the current version of the Sun" . "\n * Community Source License for Sun HPC ClusterTools (TM) (\"the License\")." . "\n * You may not use this file except in compliance with the License.  You" . "\n * may obtain a copy of the License on the World Wide Web from" . "\n * http://www.sun.com/software/communitysource/.  See the License for the" . "\n * rights, obligations, and limitations governing use of the contents of" . "\n * this file." . "\n *" . "\n * Sun, Sun Microsystems, the Sun logo, Sun HPC ClusterTools, Sun PFS," . "\n * Sun C++, Sun MPI, Prism, Sun Prism, and all Sun-based trademarks and logos," . "\n * are trademarks or registered trademarks of Sun Microsystems, Inc. in" . "\n * the United States and other countries." . "\n */"

---

just cd to a different dir!

   :cd

---

   :h system() - capture shell output 

---

highlight search items without using / (persistent highlighting across searches!)

   :mat[ch] {group} /{pattern}/

---

a good "undo" for s/;/;\r/g -> do "vip" in visual-mode. it will select the whole paragraph.


Wed Mar 01 16:39:52 2006 Edit

Show all lines that match keyword under cursor - also searches include files! (how about one for all buffers?)

   [I
   ]I

---

To add line numbers in vim:

   :g/^/exec "s/^/".strpart(line(".")."    ", 0, 4)


Thu Mar 02 16:53:46 2006 Edit

Never download another colorscheme again - create your own in a single line with existing colorschemes!

   :h reloaded
   :Reload blah blah blah


Thu Mar 02 17:44:44 2006 Edit

   :[range]foldd[oopen] {cmd}			*:foldd* *:folddoopen*
   :[range]folddoc[losed] {cmd}			*:folddoc* *:folddoclosed*

Fri Mar 03 19:00:40 2006 Edit

Jump to match in [I or ]I operation! input("blah blah") is neat too

   :map <F4> [I:let nr = input("Which one: ")<Bar>exe "normal " . nr ."[\t"<CR>
   :ilist or :il to search for a pattern instead of what's under the cursor


Wed Mar 22 11:44:26 2006 Edit

TODO: learn how to set 'foldtext' to my liking. E.g., eliminate dashes.


Thu Mar 23 10:45:58 2006 Edit

To debug a vim function/script:

Do, e.g.:

     pepper code w/ echo statements
     redir @a    ('a' is the register of your choosing)

Then, to see trace of echo statements, do:

     normal "ap  


Thu Mar 23 19:00:01 2006 Edit

Why does vim break up into two windows when i want all 64 buffers in the same one?

   em162155@burl-ct-v440-2 /workspace/em162155/hpc/mpi-neworder/openmpi/sparc 380> g -lw opt.SUNWhpc `f . | gs2` | x gvim
   46 files to edit
   18 files to edit

Mon Mar 27 18:28:28 2006 Edit

Open all folds that contain pattern "xyz".

In normal mode:

   qq/xyznq�
   @q

Mapping for this?

Q: How do i prevent it from wrapping around? A: set nowrapscan


Fri Mar 31 11:44:34 2006 Edit

grab words, lines, paragraphs, ...

   :h visual-operators


Fri Mar 31 11:55:28 2006 Edit

Notice the time-to-exec difference between this:

   default-universe-21646\|default-universe-10056\|default-universe-28908\|default-universe-17386\|default-universe-19999\|default-universe-17111\|default-universe-16938\|default-universe-16936\|default-universe-16937\|default-universe-16935\|default-universe-16926\|default-universe-16927\|default-universe-16925\|default-universe-16924\|default-universe-16916\|default-universe-16915\|default-universe-16914\|default-universe-16906\|default-universe-16905\|default-universe-16904\|default-universe-16896\|default-universe-16895\|default-universe-16887\|default-universe-16886\|default-universe-16866\|default-universe-4226

and this:

   default-universe-\(21646\|10056\|28908\|17386\|19999\|17111\|16938\|16936\|16937\|16935\|16926\|16927\|16925\|16924\|16916\|16915\|16914\|16906\|16905\|16904\|16896\|16895\|16887\|16886\|16866\|4226\)

Fri Apr 07 10:24:38 2006 Edit

numeric sorting:

   :%!sort -k8 -n

Wed Apr 12 11:06:42 2006 Edit

TODO: shortcut (mapping) to set focus to previously visited buffer

Fri Apr 14 11:40:33 EDT 2006 Edit

Network based vim!

   :Nread "ftp://[user@]machine[[:#]port]/file"

---

Use these options with indent_dtrace.pl

   :set foldmethod=indent | set shiftwidth=1

---

   /home/em162155/share/vim/vim63/colors $ find . | grep -vw core | tail -138 | xargs -i -t sudo cp {} /ws/ompi-tools/share/vim/vim70/colors/\{\}
   /home/em162155/share/vim/vim63/plugin $ sudo cp sketch* increment.vim drawing.vim explorer.vim Align* vis.vim themes.vim cecutil.vim Decho.vim /ws/ompi-tools/share/vim/vim70/plugin/

1-liner:

   find . | grep -vw core | tail -138 | xargs -i -t sudo cp {} /import/lab-tools/local/sparc/share/vim/vim70/colors/\{\} && cd ../plugin && sudo cp sketch* increment.vim drawing.vim explorer.vim Align* vis.vim themes.vim cecutil.vim Decho.vim /import/lab-tools/local/sparc/share/vim/vim70/plugin/ &&
   find . | grep -vw core | tail -138 | xargs -i -t sudo cp {} /import/lab-tools/local/i386/share/vim/vim70/colors/\{\} && cd ../plugin && sudo cp sketch* increment.vim drawing.vim explorer.vim Align* vis.vim themes.vim cecutil.vim Decho.vim /import/lab-tools/local/i386/share/vim/vim70/plugin/

Fri May 26 15:38:25 2006 Edit

vim70 vs vim63

ls works differently now ... it now knows (and lists) every buffer vim has open in all windows, versus just the buffers in the current window

Sat Jun 24 11:48:31 2006 Edit

Attempted install of vimDebug. Needs some tweaking for solaris?

Mon Jun 26 12:40:45 2006 Edit

Figure out how 'comments' option works. So that I can get "normal gqq" to indent for lines like this:

Q: this is a long long long question ... (insert many lines worth of chars here)? A: You could set sw=3 ?

   :h comments

Wed Jun 28 11:27:53 2006 Edit

Undo branches (in vim 7)

   g+ and g-

Wed Jun 28 11:37:51 2006 Edit

dosini.vim needed to account for '#' comments:

   syn match  dosiniComment        "^\(;\|#\).*$"

Mon Jul 10 12:18:39 2006 Edit

Why does :colorscheme take so long to set on sparc?

Mon Jul 10 17:37:48 2006 Edit

What vim feature is best for code browsing? cscope, tags, grep, or find?

cscope

   + find symbols (definitions and usage)
   - not loaded on i386
   - doesn't use quickfix/location lists
   - need to attach to cscope database

tags

   + find symbols (definitions)

[vim]grep

   + find any string

find

   + find files


Tue Aug 01 17:45:36 2006 Edit

USING A PATTERN IN A RANGE

Suppose you are editing a chapter in a book, and want to replace all occurrences of "grey" with "gray". But only in this chapter, not in the next one. You know that only chapter boundaries have the word "Chapter" in the first column. This command will work then: >

:?^Chapter?,/^Chapter/s=grey=gray=g

Wed Sep 20 10:35:09 2006 Edit

scroll-cursor

redraw screen with cursor in middle, top, ...

   z.
   z<CR>

Fri Oct 13 18:03:23 2006 Edit

!@#$%! I need to get off this machine but i have a zillion vim windows/tabs/buffers opened just how i want them!

   You can quickly start editing with a previously saved View or Session with the
   |-S| argument: >
       vim -S Session.vim
   <
   All this is {not in Vi} and {not available when compiled without the
   |+mksession| feature}.
   :mks[ession][!] [file]	Write a Vim script that restores the current editing

session.

Wed Oct 18 22:42:33 2006 Edit

   :h client-server
   $ gvim --servername GVIM1 --remote files ...


Fri Nov 17 18:13:01 2006 Edit

   filename-modifiers

e.g.,

   ! echo %:t

Tue Dec 05 12:58:27 2006 Edit

How come the RSS of the last entry is so damn big?!

      PID USERNAME  SIZE   RSS STATE  PRI NICE      TIME  CPU PROCESS/NLWP
     6645 em162155   15M 1280K sleep   59    0   0:03:20 0.0% vim/1
     5887 em162155   15M 6888K sleep   59    0   0:00:14 0.0% vim/1
     1055 em162155   17M  480K sleep   59    0   0:00:03 0.0% vim/1
     5507 em162155   15M 1304K sleep   59    0   0:00:35 0.0% vim/1
    29766 em162155   24M   14M sleep   59    0   0:24:48 0.0% vim/1


Wed May 02 18:42:24 2007 Edit

   :h modelines!

E.g.,

  vim:comments=fb\:[x],fb\:[s],fb\:[\ ],fb\:*,fb\:[/],fb\:[j],fb\:[m]:


Thu Jun 21 11:44:49 2007 Edit

word count, wordcount:

   g<C-g>

Mon Sep 10 18:34:31 2007 Edit

Vim expressions in `` :-)

:e `=tempname()`


Thu Oct 25 11:28:05 2007 Edit

   :help rename-files ... 
       $ vim
       :r !ls *.c
       :%s/\(.*\).c/mv & \1.bla
       :w !sh
       :q!

Thu Oct 25 11:33:02 2007 Edit

Q: Why does vim become slow after a while? A: Do this:

   :set fdm=manual

My folds can slow the works down!

Mon Nov 19 18:42:28 2007 Edit

   windo:

Does everything in a single tab. I'd been thinking

bufdo does this!


Sun Dec 16 23:06:51 2007 Edit

   d) - delete a sentence.
   ) - move to next sentence
   ( - move to prev sentence
   :h )


Tue 08 Apr 2008 04:11:04 PM EDT Edit

   ./configure --disable-nextaw-check --prefix=/ws/ompi-tools/ && make all && sudo make install ; irc.pl `pwd`
   ./configure --prefix=/foo/bar CC=cc CXX=CC && make all X_LIBS_DIR="-L/usr/X11R6/lib64 -R/usr/X11R6/lib64" && sudo make install ; irc.pl `pwd`

Wed Apr 16 11:23:43 2008 Edit

Autocommands.

(:h au!). Jump to the end of a file immediately after opening it:

   au! BufRead,BufWinEnter * normal G


Wed Apr 16 11:23:43 2008 Edit

Replace expression

   :%s@>\d\+<\/a>@\=">" . Urndm(0,100) . "%</a>"@
   :h sub-replace-expression


Tue Jun 10 10:02:12 2008 Edit

If you want to format the current paragraph and continue where you were, use: > gwap If you always want to keep paragraphs formatted you may want to add the 'a' flag to 'formatoptions'. See |auto-format|.


Fri Jun 20 15:43:29 2008 Edit

Makes things legible (e.g., not darkblue against black background!)

   :set bg=dark

Around Wikia's network

Random Wiki