Hot answers tagged

33

You can append to a register instead of erasing it by using the upper-case letter instead of the lower-case one. For example: :1y a # copy line 1 into register a (erases it beforehand) :3y A # copy line 3 into register a (after its current content) 8G # go to line 8 "ap # print register a


30

vipe is a program for editing pipelines: command1 | vipe | command2 You get an editor with the complete output of command1, and when you exit, the contents are passed on to command2 via the pipe. In this case, there's no command1. So, you could do: : | vipe | pandoc -o foo.pdf Or: vipe <&- | pandoc -o foo.pdf vipe picks up on the EDITOR and ...


29

Oddly enough, \n in vim for replacement does not mean newline, but null. ASCII nul is ^@ (control@). Historically, vi replaces ^M (controlM) as the line-ending, which is the newline. vim added an extension \r (like the C language) to mean the same as ^M, but the developers chose to make \n mean null when replacing text. This is inconsistent with its use ...


19

From :h :w: :w_a :write_a E494 :[range]w[rite][!] [++opt] >> Append the specified lines to the current file. :[range]w[rite][!] [++opt] >> {file} Append the specified lines to {file}. '!' forces the write even if file does ...


19

You can do this from within Vim: :w !pandoc -o file.pdf Or even write the buffer into a complex pipeline: :w !grep pattern | somecommand > file.txt And then you can exit Vim without saving: :q! However, considering your specific use case, there is probably a better solution by using vi as your command line editor. Assuming you use bash: set -...


11

Just because you ran vi demo.c does not mean a file demo.c was created. It isn't created until you write the buffer to disk for the first time. Simply write the buffer to disk before compiling: :w This is confirmed by the message [No write since last change] you see. This message means the buffer changed (in that you created the buffer called demo.c) ...


10

Running in a pipeline Try: quickedit() ( trap 'rm ~/temp$$' exit; vim ~/temp$$ >/dev/tty; cat ~/temp$$ ) The key is that, to be able to use vim normally, vim needs stdout to be the terminal. We accomplish that here with the redirect >/dev/tty. For purposes of security, I put the temporary file in the user's home directory. For more on this, see ...


7

Unfortunately, I can't add a comment yet to Alex Leach's answer so I'm going to include an addendum here for Mac OS X users: Enter Copies the selected text


7

GNU screen supports the xterm alternate-screen feature using the altscreen setting in your .screenrc file. According to the manual: — Command: altscreen state (none) If set to on, "alternate screen" support is enabled in virtual terminals, just like in xterm. Initial setting is ‘off’. A quick check shows that screen is actually simulating the ...


7

The problem with Ctrl-Z When you suspend a process with Ctrl-Z, the process gets a SIGTSTP signal, and all execution will stop (i.e., no more CPU cycles), until a SIGCONT signal comes along. You will not be able to send vim any commands or input while it is suspended. In other words, don't use Ctrl-Z. Use vim --remote When starting your vim session for ...


7

How about :%s/\n\s\+/\t/gc That will find any newline character that is followed by white space and replace all of it with a tab thereby combining your lines.


7

Use :q!. :!q tells vim to execute a command called q in your example. See also :help ! and :help quit for details


7

-o, -O, and -p are mutually exclusive. You can't combine them. From main.c, command_line_scan(): case 'p': /* "-p[N]" open N tab pages */ parmp->window_layout = WIN_TABS; break; case 'o': /* "-o[N]" open N horizontal split windows */ parmp->window_layout = WIN_HOR; break; case 'O': /* "-O[N]" open N vertical ...


6

Going to the help on navigation (:h navigation): Cursor motions cursor-motions navigation These commands move the cursor position. If the new position is off of the screen, the screen is scrolled to show the cursor (see also 'scrolljump' and 'scrolloff' options). 1. Motions and operators operator 2. Left-right ...


6

Vim has a built-in help. You can read the section about the argument list with the command :help argument-list and get a list of all the commands. :argadd filename to add a file to the argument list. :argedit filename to add a file and start editing it. For buffers and windows, you can read all about them in :h buffers : Summary: - A ...


6

You are confusing $(…) with <(…). You used the former, which passes the output as arguments to vimdiff. For example, if the last line of /path/to/foo contains bar bar bar, then the following command echo $(tail -1 /path/to/foo) is equivalent to echo bar bar bar Instead, you need to use <(…). This is called process substitution, and passes the ...


6

Try this: %s/$/^V^M/ (where ^V is Ctrl-V and ^M is Ctrl-M - when you type ^V it will print a ^ char then backspace over it and then when you type the ^M it will appear as ^M ... the Ctrl-V is the standard tty literal next character - run the command stty -a to show your tty's special chars).


5

I got this error on every exit. I did not use sudo. It explicitly mentioned my user home directory: E138: Can't write viminfo file /Users/henrik/.viminfo! Removing ~/.viminfo did not fix the error. Turns out I had a bunch of viminfo temp files, and removing those fixed the issue: ls ~/.viminf* # If you want to see the files. rm -rf ~/.viminf* # ...


5

Means folk can leverage this to get a root shell, thereby bypassing your security, eg :!/bin/sh from within vim. Or :r /etc/shadow and :w /etc/shadow. And so on...


5

! generally means what you'd expect from "force", but what it means for specific command depends on the command. In the case of w!, if Vim cannot write to the file for some reason, it will try to delete and create a new one with the current buffer's contents. Consider the following example (observe the inode numbers): $ touch foo $ chmod -w foo $ stat foo ...


5

muru and others are looking at this in terms of the programmability and configurability of vim itself; other comment-answerers are looking at SSH. But there is a third, quite different, answer. You have a keyboard layout that supports dead keys. In your layout, ~ is such a dead key, possibly mapped to the conventional U+0303 "combining tilde" (  ...


5

The vim command line switch -c will execute vim commands. You can pass multiple commands and passing the two following commands will start help, in only one window vim -c help -c only


5

:%s:.*:<a href="&">&</a>: Same as in ed/sed/perl... Another less ex and more vim-like way would be: if you know how to do it once for a line, record it as a macro and then run :%normal @m where m is that macro. Like (in normal mode): qmS<a href="<Ctrl-R>""><Ctrl-R>"</a><Esc>q to record the macro.


5

You can use: vim -c 'set syntax=sql' -


5

This depends on the whims of the vendor; some (e.g. Redhat) include a "minimal vim" that behaves in a compatible fashion to vi, to be specific "mostly like Vi" per the -C option docs in the vim manual. Others could install nvi (this is what OpenBSD ships with) or ex-vi (likely the most orthodox version, and thus least likely to be installed), though I ...


5

sed does not understand \d. You can use [0-9] or, more generally, [[:digit:]] in its place: $ sed -r 's/.*(X[[:digit:]])(.*)45.*/\1\2/' test.txt X1yad X2fad X3had X4wad X5mad Note that [[:digit:]] is unicode-safe but [0-9] is not.


4

You almost have it with your second command. You did it correctly with the first, just use the same shell sequence expansion: vim directory_{0..10}/results/output.txt You should see something in the shell about opening 11 files. Then you can use vim to iterate through each one.


4

You must search for <td>n. The escaped version searches for a single isolated word td followed by n. \<word\> pattern is useful for searching words even when they are separated by other stuff than whitespace or appear at the beginning or end of the line. So try: /<td>name=


4

Add altscreen on to your ~/.screenrc and restart your screen session. From man screen altscreen on|off If set to on, "alternate screen" support is enabled in virtual terminals, just like in xterm. Initial setting is `off'.


4

From within vim you can use: :tabe `find $PWD -name build.xml`



Only top voted, non community-wiki answers of a minimum length are eligible