Hot answers tagged

32

You can do that via scp like this: vim scp://user@myserver[:port]//path/to/file.txt Notice the two slashes // between server and path, which is needed to correctly resolve the absolute path. [:port]is optional. This is handled by vim's netrw.vim standard plugin. Several other protocols are supported.


31

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


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 ...


26

You could do this by mounting the remote folder as a file-system using sshfs. To do this, first some pre-requisites: sudo apt-get install sshfs #for Debian based OS, use yum or zypper depending on your OS sudo adduser <username> fuse Now, do the mounting process: mkdir ~/remoteserv sshfs -o idmap=user ...


19

It's possible to do this without a plugin using the w command, so the buffer contents can be used in a shell command: :w !diff -au "%" - > changes.patch (% is substituted with the path of the file being edited, - reads the buffer from stdin)


19

You could also set your default text editor by using the following command. sudo update-alternatives --config editor


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 ...


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) ...


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

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

-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

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).


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

The indexed color palette has the actual rendering open to interpretation - on actual hardware, there were different standards (especially brown vs. dark yellow, brown is more useful and nicer to look at). Just check out this: https://en.wikipedia.org/wiki/Color_Graphics_Adapter On terminal emulators, it depends on the configuration. Most emulators have a ...


5

Depending on what you mean when you say you do not have the rights to edit the Vim settings, there may be a way of using Vim on the server in the way you want anyway. If you can't change your user .vimrc (because you're logging in as a shared user, for example) but you can still create files, create it as a file called, say, Loom.vimrc and then call Vim ...


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

:%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

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

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


4

The best approach is to first modify Vim configuration files to automatically load your configurations. It can be done by writing the following lines to either ~/.vimrc if you want just to your user or /etc/vim/vimrc if you wish it applied to every user. syntax on set autoindent set smartindent set tabstop=4 set shiftwidth=4 set expandtab set ...


4

Using :w! in vim is similar to the following: echo 'test' > sess.vim.temp mv sess.vim.temp sess.vim The mv commands only cares about the directory permissions, the permissions of the file are not relevant. This is because you are modifying the directory, not writing to the file. To accomplish your goal, you will also need to adjust the permissions of ...


4

You could issue the command: :intro



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