fbpx

Comandos Linux – comando rview

Comando Linux vim

Atualizado: 05/04/2019 por Computer Hope

comando vim

Nos sistemas operacionais do tipo Unix, o vim , que significa ” Vi Improved”, é um editor de texto . Ele pode ser usado para editar qualquer tipo de texto e é especialmente adequado para editar programas de computador .

Descrição

O vim é um editor de texto compatível com o Vi . Existem muitas melhorias acima do Vi : desfazer vários níveis , várias janelas e buffers, destaque de sintaxe , edição de linha de comando , conclusão de nome de arquivo , um sistema completo de ajuda, seleção visual e outros.

Iniciando o vim

Na maioria das vezes, o vim é iniciado para editar um único arquivo usando o seguinte comando.

arquivo vim

De maneira mais geral, a sintaxe para iniciar o vim é a seguinte:

vim [ opções ] [ lista de arquivos ]

Se a lista de arquivos estiver ausente, o editor começará com um buffer vazio . Caso contrário, uma das quatro opções a seguir poderá ser usada para escolher um ou mais arquivos a serem editados.

Opções principais

arquivo …Uma lista de um ou mais nomes de arquivos. O primeiro será o arquivo atual e será lido no buffer. O cursor será posicionado na primeira linha do buffer. Você pode acessar os outros arquivos com o comando ” : next “. Para editar um arquivo que começa com um traço, preceda a lista de arquivos com um traço duplo (”  “).
Um único traço especifica que o arquivo a ser editado deve ser lido a partir da entrada padrão .
-t { tag }O arquivo a ser editado e a posição inicial do cursor dependem de uma “tag”, uma espécie de rótulo de goto . A { tag } é pesquisada no arquivo de tags, o arquivo associado se torna o arquivo atual e o comando associado é executado. Principalmente, isso é usado para programas em C ; nesse caso, { tag } pode ser um nome de função . O efeito é que o arquivo que contém essa função se torna o arquivo atual e o cursor é posicionado no início da função. Para obter mais informações no vim , use o comando ” : help tag- command “.
-q [ arquivo de erro ]Inicie no modo quickFix. O arquivo errorfile é lido e o primeiro erro é exibido. Se o arquivo de erro for omitido, o nome do arquivo será obtido na opção ‘ errorfile ‘ (o padrão é ” errors.err ” na maioria dos sistemas). Erros adicionais podem ser saltados para com o comando ” : cn “. Para obter mais informações no vim , use o comando ” : help quickfix “.

Invocando o vim

O vim se comporta de maneira diferente, dependendo do nome do comando usado para invocá-lo. Por exemplo, se você criar um link simbólico para o executável do vim com um dos seguintes nomes, ele se comportará da seguinte maneira:

nome do comandocomportamento
vimExecução “normal”. Tudo é o padrão.
exComece no modo Ex . Vá para o modo Normal com o comando ” : vi “. Também pode ser feito com o argumento ” -e ” .
VisãoInicie no modo somente leitura. Você estará protegido contra a gravação dos arquivos. Também pode ser feito com o argumento ” -R “.
gvim , gviewA versão da GUI (se instalada). Inicia uma nova janela. Também pode ser feito com o argumento ” -g “.
evim , eviewExecuta a GUI vim no “modo fácil”. Este comando é um modo simplificado de operação do vim , que é muito parecido com um editor de texto normal. É o mesmo que executar o vim com o argumento ” -y “.
rvim , rview , rgvim , rgviewComo o acima, mas executando no modo “restrito”. Não será possível iniciar comandos do shell a partir do vim ou suspender o vim . É o mesmo que especificar o argumento ” -Z “.

Sintaxe

vim [ opções ] [ arquivo ..]
vim [ opções ] -
tag vim [ opções ] -t
vim [ opções ] -q [ arquivo de erro ]

Opções adicionais

As opções podem ser dadas em qualquer ordem, antes ou depois dos nomes dos arquivos. Opções sem argumento podem ser combinadas após um único traço.

+ [ num ]Para o primeiro arquivo, o cursor será posicionado na linha num . Se num estiver faltando, o cursor será posicionado na última linha.
+ / { pat }Para o primeiro arquivo, o cursor será posicionado na primeira ocorrência de { pat }. No <v> vim, use o comando ” : help search-pattern ” para os padrões de pesquisa disponíveis.
+ { comando }, -c { comando }command } será executado após a leitura do primeiro arquivo. { command } é interpretado como um comando ex . Se o { comando } contiver espaços, ele deverá ser colocado entre aspas duplas (isso depende do shell usado). Por exemplo:

vim "+ set si" main.c

Você pode usar até 10 comandos ” + ” ou ” -c “.

-S { arquivo }file } será originado após a leitura do primeiro arquivo. Isso é equivalente a -c “origem {arquivo}” . { arquivo } não pode começar com ‘  ‘. Se { arquivo } for omitido, “Session.vim” será usado (funciona apenas quando -S é o último argumento).
–cmd { command }Como usar ” -c “, mas o comando é executado imediatamente antes de processar qualquer arquivo vimrc . Você pode usar até 10 desses comandos, independentemente dos comandos ” -c “.
-UMASe o vim foi compilado com suporte árabe para editar arquivos orientados da direita para a esquerda e mapeamento de teclado árabe, esta opção inicia o vim no modo árabe, ou seja, ‘ árabe ‘ está definido. Caso contrário, uma mensagem de erro é fornecida e o vim é interrompido.
-bModo binário . Algumas opções serão definidas para possibilitar a edição de um arquivo binário ou executável.
-CModo compatível. Define a opção ‘ compatível ‘. Isso fará com que o vim se comporte principalmente como o Vi , mesmo que exista um arquivo .vimrc .
-dComece no modo diff . Deve haver dois, três ou quatro argumentos de nome de arquivo. O vim abrirá todos os arquivos e mostrará diferenças entre eles. Funciona como vimdiff .
-d { dispositivo }Abra { dispositivo } para uso como um terminal (apenas no Amiga ).
-DModo de depuração . Vá para o modo de depuração ao executar o primeiro comando a partir de um script .
-eInicie o vim no modo ex , como se o executável fosse chamado ” ex “.
-EInicie o vim no modo ex aprimorado , como se o executável fosse chamado ” exim “.
-fModo de primeiro plano. Para a versão da GUI, o vim não bifurca e desconecta do shell em que foi iniciado. No Amiga, o vim não é reiniciado para abrir uma nova janela. Esta opção deve ser usada quando o vim for executado por um programa que aguardará a conclusão da sessão de edição (por exemplo, correio ). No Amiga, os comandos ” : sh ” e ” :! ” Não funcionarão.
–noforkModo de primeiro plano. Para a versão da GUI, o vim não se bifurca e desconecta do shell em que foi iniciado.
-FSe o vim foi compilado com o suporte do FKMAP para editar arquivos orientados da direita para a esquerda e mapeamento de teclado farsi, esta opção inicia o vim no modo farsi, ou seja, ‘ fkmap ‘ e ‘ rightleft ‘ estão definidos. Caso contrário, uma mensagem de erro é fornecida e o vim é interrompido.
-gSe o vim foi compilado com suporte à GUI, essa opção ativará a GUI. Se nenhum suporte GUI foi compilado, uma mensagem de erro é fornecida e o vim é interrompido.
-hAjude um pouco sobre os argumentos e as opções da linha de comando. Depois que esse vim sai.
-HSe o vim foi compilado com o suporte RIGHTLEFT para editar arquivos orientados da direita para a esquerda e mapeamento do teclado hebraico, esta opção inicia o vim no modo hebraico, ou seja, ‘ hkmap ‘ e ‘ rightleft ‘ estão definidos. Caso contrário, uma mensagem de erro é fornecida e o vim é interrompido.
-i { viminfo }Quando o uso do arquivo viminfo está ativado, essa opção define o nome do arquivo a ser usado, em vez do padrão ” ~ / .viminfo “. Isso também pode ser usado para ignorar o uso do arquivo .viminfo , dando o nome ” NONE “.
-EUO mesmo que -r .
-euModo Lisp . Ativa as opções ‘ lisp ‘ e ‘ showmatch ‘.
-mA modificação de arquivos está desativada. Redefine a opção ‘ gravar ‘. Você ainda pode modificar o buffer, mas não é possível gravar um arquivo.
-MModificações não permitidas. As opções ‘ modificável ‘ e ‘ gravação ‘ serão desabilitadas, de modo que as alterações não são permitidas e os arquivos não podem ser gravados. Observe que essas opções podem ser definidas para permitir fazer modificações.
-NModo não compatível. Redefina a opção ‘ compatível ‘. Isso fará com que o vim se comporte um pouco melhor, mas menos compatível com o Vi , mesmo que não exista um arquivo .vimrc .
-nNenhum arquivo de troca será usado. A recuperação após uma falha será impossível. Prático, se você deseja editar um arquivo em uma mídia muito lenta (por exemplo, disquete). Também pode ser feito com ” : set uc = 0 “. Pode ser desfeito com ” : set uc = 200 “.
-nbTorne-se um servidor editor para o NetBeans.
-o [ N ]Abra N janelas empilhadas. Quando N for omitido, abra uma janela para cada arquivo.
-O [ N ]Abra N janelas lado a lado. Quando N for omitido, abra uma janela para cada arquivo.
-p [ N ]Abra as guias N. Quando N for omitido, abra uma guia para cada arquivo.
-RModo somente leitura. A opção ‘ somente leitura ‘ será definida. Você ainda pode editar o buffer, mas será impedido de substituir acidentalmente um arquivo. Se você deseja substituir um arquivo, adicione um ponto de exclamação ao comando Ex , como em ” : w! “. A opção -R também implica a opção -n (veja abaixo). A opção ‘ somente leitura ‘ pode ser redefinida com ” : set noro “. Veja ” : help ‘readonly’ “.
-rListar arquivos de troca, com informações sobre como usá-los para recuperação.
-r { arquivo }Modo de recuperação. O arquivo de troca é usado para recuperar uma sessão de edição com falha. O arquivo de troca é um arquivo com o mesmo nome que o arquivo de texto com ” .swp ” anexado. Veja ” : ajude a recuperação “.
-sModo silencioso. Somente quando iniciado como ” Ex ” ou quando a opção ” -e ” foi fornecida antes da opção ” -s “.
-s { scriptin }O arquivo de script { scriptin } é lido. Os caracteres no arquivo são interpretados como se você os tivesse digitado. O mesmo pode ser feito com o comando ” : source! {Scriptin} “. Se o final do arquivo for alcançado antes da saída do editor, outros caracteres serão lidos no teclado.
-T { terminal }Diz vim o nome do terminal que você está usando. Necessário apenas quando o modo automático não funciona. Deve ser um terminal conhecido pelo vim (embutido) ou definido no arquivo termcap ou terminfo .
-u { vimrc }Use os comandos no arquivo { vimrc } para inicializações. Todas as outras inicializações são ignoradas. Use isso para editar um tipo especial de arquivo. Também pode ser usado para pular todas as inicializações, dando o nome ” NONE “. Veja ” : ajuda na inicialização ” no vim para mais detalhes.
-U { gvimrc }Use os comandos no arquivo { gvimrc } para inicializações da GUI. Todas as outras inicializações da GUI são ignoradas. Também pode ser usado para pular todas as inicializações da GUI, dando o nome ” NONE “. Veja ” : help gui-init ” no vim para mais detalhes.
-V [ N ]Verbose . Envie mensagens sobre quais arquivos são originados e para ler e gravar um arquivo viminfo . O número opcional N é o valor para ‘ detalhado ‘. O padrão é 10 .
-vInicie o vim no modo Vi , assim como o executável foi chamado ” vi “. Isso só tem efeito quando o executável é chamado ” ex “.
-w { scriptout }Todos os caracteres que você digita são gravados no arquivo { scriptout }, até você sair do vim . Isso é útil se você deseja criar um arquivo de script para ser usado com ” vim -s ” ou ” : source! “. Se o arquivo { scriptout } existir, os caracteres serão anexados.
-W { scriptout }Como -w , mas um arquivo existente é substituído.
-xUse criptografia ao gravar arquivos. Solicitará uma chave de criptografia.
-XNão conecte ao servidor X. Diminui o tempo de inicialização em um terminal, mas o título da janela e a área de transferência não serão utilizados.
-yInicie o vim no modo fácil, assim como o executável foi chamado ” evim ” ou ” eview “. Faz com que o vim se comporte como um editor de clique e digitação.
-ZModo restrito. Funciona como o executável começa com ” r “.
Indica o final das opções. Os argumentos após isso serão tratados como um nome de arquivo. Isso pode ser usado para editar um nome de arquivo que comece com um ‘  ‘.
–echo-widApenas GUI GTK: reproduza o ID da janela na saída padrão.
–SocorroDê uma mensagem de ajuda e saia, como ” -h “.
–literalTome os argumentos do nome do arquivo literalmente, não expanda curingas . Isso não tem efeito no Unix, onde o shell expande curingas.
–nopluginIgnore o carregamento de plug-ins. Implícito por -u NENHUM .
–controlo remotoConecte-se a um servidor vim e faça-o editar os arquivos fornecidos no restante dos argumentos. Se nenhum servidor for encontrado, um aviso será dado e os arquivos serão editados no vim atual .
–remote-expr { expr }Conecte-se a um servidor vim , avalie { expr } nele e imprima o resultado no stdout.
–remote-send { keys }Conecte-se a um servidor vim e envie { keys } para ele.
–remote-silentComo –remote , mas sem o aviso quando nenhum servidor for encontrado.
–remote-waitComo –remote , mas o vim não sai até que os arquivos tenham sido editados.
–remote-wait-silentComo –remote-wait , mas sem o aviso quando nenhum servidor for encontrado.
–Lista de servidoresListe os nomes de todos os servidores vim que podem ser encontrados.
–servername { name }Use { name } como o nome do servidor. Usado para o vim atual , a menos que seja usado com um argumento –remote , é o nome do servidor ao qual se conectar.
–socketid { id }Apenas GTK GUI: Use o mecanismo GtkPlug para executar o gvim em outra janela.
–versãoImprima as informações da versão e saia.

Acessando a Ajuda no vim

Digite ” : help ” no vim para começar. Digite ” : assunto da ajuda ” para obter ajuda sobre um assunto específico. Por exemplo: ” : help ZZ ” para obter ajuda para o comando ” ZZ “. Use <Tab> e Ctrl-D para concluir os assuntos (” : ajude a cmdline-complete “). As tags estão presentes para pular de um lugar para outro (tipo de links de hipertexto, consulte ” : ajuda “). Todos os arquivos de documentação podem ser visualizados dessa maneira, por exemplo ” : help syntax.txt “.

Aprendendo o vim com o tutor do vim

vim installs with a built-in tutorial system called the vimtutor to help you learn vim commands. It is a 30 minute tutorial that teaches the most basic vim functionality hands-on. On Unix and Linux, if vim has been properly installed, you can start it from the command line by running the command:

vimtutor

On Microsoft Windows you can find it in the Programs/vim menu, or you can run vimtutor.bat in the directory where vim was installed.

Editing In vim: Inserting Text

The vim editor is a “modal” editor. That means that the editor behaves differently depending on which mode you are in. The two basic modes are called Normal mode and Insert mode. In Normal mode the characters you type are commands. In Insert mode the characters are inserted as text.

Since you have just started vim it will be in Normal mode. To start Insert mode you type the “i” command (i for Insert). Then you can enter the text. It will be inserted into the file. For example, you can start a new vim session by running the following command from the command line:

vim

This will place you in a new vim editing window, which will show a tilde (“~“) on any line that is empty of text. The window will resemble the following:

┌────────────────────────────────────────────────────────┐
│~                                                       │
│~                                                       │
│~                                                       │
│~                                                       │
│                                                        │
└────────────────────────────────────────────────────────┘

You will start in normal mode, so to insert text you will need to enter insert mode. You will type i, and then the text you want to insert into the document. So, you can type the following:

iThis is a line of text.
This is line number two!

(Pressione <Enter> após a palavra texto para iniciar a nova linha). Por fim, pressione a tecla <Esc> para interromper o modo Inserir e voltar ao modo Normal. Agora você tem duas linhas de texto na janela do vim :

┌────────────────────────────────────────────────── ───────┐
│Esta é uma linha de texto. │
│Esta é a linha número dois! │
│ ~ │
│ ~ │
│ │
└────────────────────────────────────────────────── ───────┘

Sair do problema

Quando você aprende o vim pela primeira vez , é comum ficar confuso digitando o comando errado ou digitando-o no modo errado. Sempre, para voltar ao modo Normal (não importa em que modo você esteja), pressione a tecla <Esc> . Às vezes você tem que pressionar duas vezes. Se o vim emitir um bipe, você já está no modo Normal.

Movendo-se em torno de

Depois de retornar ao modo Normal, você pode se mover usando estas teclas:

hesquerda
jbaixa
kacima
eudireito

Essas teclas podem parecer escolhas estranhas para mover o cursor, mas há uma boa razão para isso: mover o cursor é a coisa mais comum que você faz em um editor e essas teclas estão na linha inicial da sua mão direita. Em outras palavras, esses comandos são colocados onde você pode digitá-los mais rapidamente (especialmente quando você digita com dez dedos).

Você também pode mover o cursor usando as teclas de seta. No entanto, se você fizer isso, você diminui bastante a velocidade da edição porque, para pressionar as teclas de seta, deve mover a mão das teclas de texto para as teclas de seta. Considerando que você pode fazer isso centenas de vezes por hora, isso pode levar uma quantidade significativa de tempo. Além disso, existem teclados que não possuem teclas de seta ou que os localizam em locais incomuns; portanto, conhecer o uso das teclas hjkl ajuda nessas situações.

A melhor maneira de aprender esses comandos é usando-os. Use o comando ” i ” para inserir mais algumas linhas de texto. Em seguida, use as teclas hjkl para se mover e inserir uma palavra em algum lugar. Não se esqueça de pressionar <Esc> para voltar ao modo Normal. O vimtutor também é uma maneira fácil de aprender fazendo.

Exclusão de caracteres

Para excluir um caractere, mova o cursor sobre ele e digite ” x “. Mova o cursor para o início da primeira linha, por exemplo, e digite xxxxxxx (sete x ‘s) para excluir ” Isto é “. O resultado deve ficar assim:

┌────────────────────────────────────────────────── ───────┐
│ uma linha de texto. │
│Esta é a linha número dois! │
│ ~ │
│ ~ │
│ │
└────────────────────────────────────────────────── ───────┘

Agora você pode inserir um novo texto, por exemplo, digitando:

Aqui está

<Esc>

Isso inicia uma inserção ( i ), insere as palavras ” Aqui está ” e sai do modo de inserção ( <Esc> ). O resultado:

┌────────────────────────────────────────────────── ───────┐
IsAqui está uma linha de texto. │
│Esta é a linha número dois! │
│ ~ │
│ ~ │
│ │
└────────────────────────────────────────────────── ───────┘

Excluindo uma linha

Para excluir uma linha inteira, mova o cursor para essa linha e digite ” dd “. A próxima linha será movida para cima para preencher a lacuna. Por exemplo, se você mover o cursor para a primeira linha e digitar ” dd “, nosso exemplo será semelhante a:

┌────────────────────────────────────────────────── ───────┐
│Esta é a linha número dois! │
│ ~ │
│ ~ │
│ ~ │
│ │
└────────────────────────────────────────────────── ───────┘

Excluindo uma quebra de linha

No vim, você pode unir duas linhas, o que significa que a quebra de linha entre elas é excluída. O comando ” J ” faz isso. Pegue estas duas linhas:

Esta é a linha 
   número dois

Move the cursor to the first line and press “J“:

This is line number two

Undo

The “u” command undoes the last edit. In vim, pressing u multiple times continues to undo previous edits. This is one of the ways vim is different than vi; in vi, pressing u twice undoes the undo itself.

Redo

Pressing Ctrl-R (redo) reverses the preceding command. So, after performing an undo with “u“, pressing Ctrl-R will undo the undo.

Appending

The “i” command inserts a character before the character under the cursor. That works fine; but what happens if you want to add stuff to the end of the line? For that you need to insert text after the cursor. This is done with the “a” (append) command. For example, to change the line

I am very excited about using vim.

to

I am very excited about using vim!!!

move the cursor over to the dot (period) at the end of the line. Then type “x” to delete the period. The cursor is now positioned at the end of the line. Now type

a!!!

And then press <Esc> to return to normal mode.

So, any time you want to insert text right where the cursor is, press “i“. But if you want to start adding text after the cursor position, press “a“.

Opening Up A New Line

The “o” command creates a new, empty line below the cursor and puts vim in Insert mode. Then you can type the text for the new line. Suppose the cursor is somewhere in the first of these two lines:

I am very excited about using vim. 
Amaze.

If you now use the “o” command and type new text:

oWow. Such useful. Many time saving.

Then type <Esc> to return to normal mode. The result is:

I am very excited about using vim.
Wow. Such useful. Many time saving.
Amaze.

The “O” command (uppercase) is similar, but opens a line above the cursor instead of below it.

Repeating A Command Using A Count

Suppose you want to move up nine lines. You can type “kkkkkkkkk” or you can enter the command “9k“. In fact, you can precede many commands with a number. For instance, earlier we added three exclamation points to the end of a line by typing “a!!!<Esc>“. Another way to do this is to use the command “3a!<Esc>“. The count of 3 tells the command that follows to triple its effect. Similarly, to delete three characters, use the command “3x“. The count always comes before the command it applies to.

Quitting vim

To exit, use the “ZZ” command. This command writes the file and exits.

Unlike other editors, vim does not automatically make a backup file. If you type “ZZ“, your changes are committed and there’s no turning back. You can configure vim to produce backup files (see “Backup Files“, below), but it will not do so by default.

Discarding Changes

To exit without saving your changes, use the command

:q!

For those of you interested in the details, the three parts of this command are the colon (:), which enters Command-line mode; the q command, which tells the editor to quit; and the override command modifier (!). The override command modifier is needed because vim is reluctant to throw away changes. If you were to just type “:q“, vim would display an error message and refuse to exit:

E37: No write since last change (use ! to override)

If you merely want to revert to the saved version of the file, the “:e!” command reloads the original version of the file.

Moving From Word To Word

To move the cursor forward one word, use the “w” command. Like most vim commands, you can use a numeric prefix to move past multiple words. For example, “3w” moves three words. This example shows how it works: For the following line of text, if you press “w” where indicated, the cursor moves to the place indicated by the arrow.

This is a line with example text 
w--->_
 w->_
 w>_

Or, using the numeric prefix:

This is a line with example text
3w------->_

The “b” command moves backward to the start of the previous word:

This is a line with example text 
                    _<---b
          _<--------2b 
        _<b 
     _<-b 
_<---b 

There is also the “e” command that moves to the next end of a word and “ge“, which moves to the previous end of a word:

This is a line with example text 
   <-   <--- ----->   ---->
   ge    ge     e       e

If you are at the last word of a line, the “w” command will take you to the first word in the next line. Thus you can use this to move through a paragraph, much faster than using “l“. “b” does the same in the other direction.

A word ends at a non-word character, such as a “.“, “” or “)“. To change what vim considers to be a word, use the “:help iskeyword” command. It is also possible to move by white space-separated words. This is not a “word” in the normal sense, that’s why the uppercase is used. The commands for moving this way are in uppercase, as shown here:

 ge b w e
       _<     _<         -->_                          -->_
This is-a line, with special/separated/words (and some more). 
   _<---- _<--- 	 ------------------->_         ---->_
 gE B W E

With this mix of lowercase and uppercase commands, you can quickly move forward and backward through a paragraph.

Moving To The Start Or End Of A Line

The “$” command moves the cursor to the end of a line. If your keyboard has an <End> key it will do the same thing.

The “^” command moves to the first non-blank character of the line. The “0” command (zero) moves to the very first character of the line (the <Home> key does the same thing):

 ^
     _<----------
     This is a line with example text 
_<---------------     -------------->_
 0 $

The “$” command takes a count, like most movement commands. But moving to the end of the line several times doesn’t make sense. Therefore it causes the editor to move to the end of another line. For example, “1$” moves you to the end of the first line (the one you’re on), “2$” to the end of the next line, and so on.

The “0” command doesn’t take a count argument, because the “0” would be part of the count.

Using a count with “^” doesn’t have any effect.

Moving To A Character

One of the most useful movement commands is the single-character search command. The command “fx” searches forward in the line for the single character “x”. The “f” stands for “Find”.

For example, you are at the beginning of the following line. Suppose you want to go to the “h” in “human”. Just execute the command “fh” and the cursor will be positioned over the h:

To err is human. To forgive is divine. 
--------->_
fh	

You can specify a count; so, from the beginning of the line, you can go to the “o” of “forgive” with “3fo“:

To err is human. To forgive is divine. 
-------------------->_
3fo

The “F” (uppercase F) command searches to the left:

To err is human. To forgive is divine.
          _<----------
 Fh

The “tx” command works like the “fx” command, except it stops one character before the searched character. The “t” stands for “To”. The backward version of this command is “Tx“.

To err is human. To forgive is divine. 
          _<---------    --------->_
 Th tn

These four commands can be repeated with “;“.

,” repeats in the other direction.

The cursor is never moved to another line. Not even when the sentence continues.

Sometimes you will start a search, only to realize that you have typed the wrong command. You type “f” to search backward, for example, only to realize that you really meant “F“. To abort a search, press <Esc>. So “f<Esc>” is an aborted forward search and doesn’t do anything. <Esc> cancels most operations, not just searches.

Matching A Parenthesis

When writing a program you often end up with nested <v>()</v> constructs. Then the “%” command is very handy: It moves to the matching parenthesis. If the cursor is on a “(” it moves to the matching “)“. If it’s on a “)” it moves to the matching “(“.

 %
         _<----
if (a == (b * c) / d) 
    --------------->_
 %

This also works for [] and {} pairs. This can be defined with the ‘matchpairs‘ option. Use the “:help matchpairs” command within vim for more information.

Quando o cursor não estiver em um caractere útil, ” % ” procurará adiante para encontrar um. Portanto, se o cursor estiver no início da linha do exemplo anterior, ” % ” procurará para a frente e encontrará o primeiro ” ( “. Em seguida, ele se moverá para sua correspondência:

se (a == (b * c) / d) 
 - + ---------------> _
%

Movendo para uma linha específica

Para ir para uma linha específica, use o comando ” G “.

Sem argumento, ” G ” o posiciona no final do arquivo. Uma maneira rápida de ir para o início de um arquivo, use ” gg “. ” 1G ” fará o mesmo.

	    | primeira linha de um arquivo ^
	    | texto texto texto texto |
	    | texto texto texto texto | gg 
	7G | texto texto texto texto |
	    | texto texto texto texto
	    | texto texto texto texto
	    V text text text text |
		texto texto texto texto | G
		texto texto texto texto |
		última linha de um arquivo V

Outra maneira de mover para uma linha é usar o comando ” % ” com uma contagem, que move essa porcentagem pelo arquivo. Por exemplo, ” 50% ” move você para a metade do arquivo. ” 90% ” chega perto do fim.

O anterior pressupõe que você deseja mover para uma linha no arquivo, não importa se está visível no momento ou não. E se você quiser passar para uma das linhas que pode ver? Esta figura mostra os três comandos que podem ser usados, H , M , e L .

H move você para o topo da tela visível, M para o meio e L para o final. Por exemplo:

        ┌──────────────────────────	
 H -> | texto de exemplo |
        | texto de exemplo |
        | texto de exemplo |
        | texto de exemplo |
M -> | texto de exemplo |
        | texto de exemplo |
        | texto de exemplo |
        | texto de exemplo |
L -> | texto de exemplo |
        └──────────────────────────┘

“H” significa Casa, “M” significa Médio e “L” significa Último.

Determinando onde você está

Para ver onde você está em um arquivo, há três maneiras:

Use o comando Ctrl-G . Você recebe uma mensagem como esta:

linha "usr_03.txt" 233 de 650 --35% - col 45-52

Isso mostra o nome do arquivo que você está editando, o número da linha onde está o cursor, o número total de linhas, a porcentagem do caminho através do arquivo e a coluna do cursor.

Às vezes, você verá um número de coluna dividido. Por exemplo, ” col 2-9 “. Isso indica que o cursor está posicionado no segundo caractere, mas como o caractere um é uma guia, ocupando oito espaços no valor de colunas, a coluna da tela é 9 .

Defina a opção ‘ número ‘. Isso exibe um número de linha na frente de cada linha:

: definir número

Para desligar novamente:

: definir nonumber

Como ‘number’ é uma opção booleana , acrescentar ” não ” ao nome tem o efeito de desativá-lo. Uma opção booleana possui apenas esses dois valores, está ativada ou desativada.

O vim tem muitas opções. Além dos booleanos, existem opções com um valor numérico e opções de string. Você verá exemplos disso onde eles são usados.

Defina a opção ‘ régua ‘. Isso exibe a posição do cursor no canto inferior direito da janela do vim :

: definir régua

Rolagem

The Ctrl-U command scrolls down half a screen of text. Think of looking through a viewing window at the text and moving this window up by half the height of the window. Thus the window moves up over the text, which is backward in the file. Don’t worry if you have a little trouble remembering which end is up. Most users have the same problem.

The Ctrl-D command moves the viewing window down half a screen in the file, thus scrolls the text up half a screen.

                               ┌────────────────┐
                               | some text      |
                               | some text      |
                               | some text      |
┌───────────────┐              | some text      |
| some text     | Ctrl-U -->  |                |
|               |              | 123456         |
| 123456        |              └────────────────┘
| 7890          |
|               |              ┌────────────────┐
| example       | Ctrl-D -->  | 7890           |
└───────────────┘              |                |
                               | example        |
                               | example        |
                               | example        |
                               | example        |
                               └────────────────┘

To scroll one line at a time use Ctrl-E (scroll up) and Ctrl-Y (scroll down). Think of Ctrl-E to give you one line Extra. If you use MSWindows-compatible key mappings Ctrl-Y will redo a change instead of scroll.

To scroll forward by a whole screen (except for two lines) use Ctrl-F. The other way is backward, Ctrl-B is the command to use. Fortunately Ctrl-F is Forward and Ctrl-B is Backward, that’s easy to remember.

A common issue is that after moving down many lines with “j” your cursor is at the bottom of the screen. You would like to see the context of the line with the cursor. That’s done with the “zz” command.

┌──────────────────┐             ┌──────────────────┐
| some text        |             | some text        |
| some text        |             | some text        |
| some text        |             | some text        |
| some text        | zz -->   | line with cursor |
| some text        |             | some text        |
| some text        |             | some text        |
| line with cursor |             | some text        |
└──────────────────┘             └──────────────────┘

The “zt” command puts the cursor line at the top, “zb” at the bottom. To always keep a few lines of context around the cursor, use the ‘scrolloff‘ option (“:set scrolloff“).

Simple Searches

To search for a string, use the “/string” command. To find the word “include”, for example, use the command:

/include

You will notice that when you type the “/” the cursor jumps to the last line of the vim window, like with colon commands. That is where you type the word. You can press the backspace key (backarrow or <BS>) to make corrections. Use the <Left> and <Right> cursor keys when necessary. Pressing <Enter> executes the command.

Note: The characters .*[]^%/\?~$ have special meanings. If you want to use them in a search you must put a \ in front of them; see below.

To find the next occurrence of the same string use the “n” command. Use this to find the first “#include” after the cursor:

/#include

And then type “n” several times. You move to each “#include” in the text. You can also use a count if you know which match you want. Thus “3n” finds the third match. Using a count with “/” doesn’t work.

The “?” command works like “/” but searches backwards:

?word

The “N” command repeats the last search the opposite direction. Thus using “N” after a “/” command search backwards, using “N” after “?” searches forward.

Ignoring Case

Normally you have to type exactly what you want to find. If you don’t care about upper or lowercase in a word, set the ‘ignorecase‘ option:

:set ignorecase

If you now search for “word”, it will also match “Word” and “WORD”. To match case again:

:set noignorecase

History

Suppose you do three searches:

/one
/two
/three

Now let’s start searching by typing a simple “/” without pressing <Enter>. If you press <Up> (the cursor key), vim puts “/three” on the command line. Pressing <Enter> at this point searches for three. If you do not press <Enter>, but press <Up> instead, vim changes the prompt to “/two“. Another press of <Up> moves you to “/one“.

You can also use the <Down> cursor key to move through the history of search commands in the other direction.

If you know what a previously used pattern starts with, and you want to use it again, type that character before pressing <Up>. With the previous example, you can type “/o<Up>” and vim will put “/one” on the command line.

The commands starting with “:” also have a history. That allows you to recall a previous command and execute it again. These two histories are separate.

Searching For A Word In The Text

Suppose you see the word “TheLongFunctionName” in the text and you want to find the next occurrence of it. You could type “/TheLongFunctionName“, but that’s a lot of typing.

There is an easier way: Position the cursor on the word and use the “*” command. vim will grab the word under the cursor and use it as the search string.

The “#” command does the same in the other direction. You can prepend a count: “3*” searches for the third occurrence of the word under the cursor.

Searching For Whole Words

If you type “/the” it will also match “there”. To only find words that end in “the” use:

/the\>

The “\>” notation is a special marker that only matches at the end of a word. Similarly “\<” only matches at the begin of a word. Thus to search for the word “the” only:

/\<the\>

This does not match “there” or “soothe”. Notice that the “*” and “#” commands use these start-of-word and end-of-word markers to only find whole words (you can use “g*” and “g#” to match partial words).

Highlighting Matches

While editing a program you see a variable called “nr”. You want to check where it’s used. You could move the cursor to “nr” and use the “*” command and press “n” to go along all the matches.

There is another way. Type this command:

:set hlsearch

If you now search for “nr“, vim will highlight all matches. That is a very good way to see where the variable is used, without the need to type commands.

To switch this off:

:set nohlsearch

Then you need to switch it on again if you want to use it for the next search command. If you only want to remove the highlighting, use this command:

:nohlsearch

This doesn’t reset the option. Instead, it disables the highlighting. As soon as you execute a search command, the highlighting will be used again. Also, for the “n” and “N” commands.

Tuning Searches

There are a few options that change how searching works. These are the essential ones:

:set incsearch

This makes vim display the match for the string while you are still typing it. Use this to check if the right match will be found. Then press <Enter> to really jump to that location. Or type more to change the search string.

:set nowrapscan

This stops the search at the end of the file. Or, when you are searching backwards, at the start of the file. The ‘wrapscan‘ option is on by default, thus searching wraps around the end of the file.

Intermezzo

If you like one of the options mentioned before, and set it each time you use vim, you can put the command in your vim startup file.

If you need to learn where the startup file is, use this command:

:scriptnames

Edit the file, for example with:

:edit ~/.vimrc

Then add a line with the command to set the option, just like you typed it in vim. Example:

Go:set hlsearch<Esc>

G” moves to the end of the file. “o” starts a new line, where you type the “:set” command. You end insert mode with <Esc>. Then write the file:

ZZ

If you now start vim again, the ‘hlsearch‘ option will already be set.

Simple Search Patterns

The vim editor uses regular expressions (“Regex”) to specify what to search for. Regular expressions are an extremely powerful and compact way to specify a search pattern.

Regex: Beginning And End Of A Line

The ^ (caret) character matches the beginning of a line. The pattern “include” matches the word include anywhere on the line. But the pattern “^include” matches the word include only if it is at the beginning of a line.

The $ character matches the end of a line. Therefore, “was$” matches the word “was” only if it is at the end of a line.

You can try searching with “/^the$“, it will only match a single line consisting soley of the word “the”. White space matters; therefore if a line contains a space after the word, like “the “, the pattern will not match.

Regex: Matching Any Single Character

The . (dot, or period) character matches any existing character. For example, the pattern “c.m” matches a string whose first character is a “c”, whose second character is anything, and whose the third character is “m”.

Regex: Matching Special Characters

If you really want to match a dot, you must avoid its special meaning by putting a backslash before it, like this: “\.“. The same goes for any special character (.*[]^%/\?~$).

Using Marks

When you make a jump to a position with the “G” command, vim remembers the position from before this jump. This position is called a mark. To go back where you came from, use this command:

``

This ` is a backtick or open single-quote character.

If you use the same command a second time you will jump back again. That’s because the ` command is a jump itself, and the position from before this jump is remembered.

Generally, every time you do a command that can move the cursor further than within the same line, this is called a jump. This includes the search commands “/” and “n” (it doesn’t matter how far away the match is). But not the character searches with “fx” and “tx” or the word movements “w” and “e“.

Also, “j” and “k” are not considered to be a jump. Even when you use a count to make them move the cursor quite a long way away.

The  command jumps back and forth, between two points. The Ctrl-O command jumps to older positions (“O” stands for “older”). Ctrl-I then jumps back to newer positions. Consider this sequence of commands:

33G
/^The
Ctrl-O

You first jump to line 33, then search for a line that starts with “The”. Then with Ctrl-O you jump back to line 33. Another Ctrl-O takes you back to where you started. If you now use Ctrl-I you jump to line 33 again. And to the match for “The” with another Ctrl-I.

Note: Ctrl-I is the same as <Tab>.

The “:jumps” command gives a list of positions you jumped to. The entry which you used last is marked with a “>“.

Named Marks

vim enables you to place marks in the text. The command “ma” marks the place under the cursor as mark “a”. You can place 26 marks (a through z) in your text. You can’t see them, it’s just a position that vim remembers.

To go to a mark, use the command `{mark}, where {mark} is the mark letter. Thus to move to the a mark:

`a

The command mark (single quotation mark, or apostrophe) moves you to the beginning of the line containing the mark. This differs from the `mark command, which moves you to marked column.

The marks can be very useful when working on two related parts in a file. Suppose you have some text near the start of the file you need to look at, while working on some text near the end of the file.

Move to the text at the start and place the “s” (start) mark there:

ms

Then move to the text you want to work on and put the “e” (end) mark there:

me

Now you can move around, and when you want to look at the start of the file, you use this to jump there:

's

Then you can use  to jump back to where you were, or ‘e to jump to the text you were working on at the end.

There is nothing special about using “s” for start and “e” for end, they are just easy to remember.

You can use this command to get a list of marks:

:marks

You will notice a few special marks. These include:

The cursor position before doing a jump.
The cursor position when last editing the file.
[Start of the last change.
]End of the last change.

Operators And Motions

The “dw” command deletes a word. You may recognize the “w” command as the move word command. In fact, the “d” command may be followed by any motion command, and it deletes from the current location to the place where the cursor winds up. The “4w” command, for example, moves the cursor over four words. The d4w command deletes four words.

To err is human. To forgive is divine. To breathe is required. 
                 --------------------->
 d4w
To err is human. To breathe is required.

vim only deletes up to the position where the motion takes the cursor. That’s because vim knows that you probably don’t want to delete the first character of a word. If you use the “e” command to move to the end of a word, vim guesses that you do want to include that last character:

To err is human. To breathe is required. 
                ---------->
 d2e
To err is human. is required.

Whether the character under the cursor is included depends on the command you used to move to that character. This is called “exclusive” when the character isn’t included and “inclusive” when it is.

The “$” command moves to the end of a line. The “d$” command deletes from the cursor to the end of the line, which is an inclusive motion, thus the last character of the line is included in the delete operation:

To err is human. is divine. 
                 -------->
 d$
To err is human.

There is a pattern here: operator-motion. You first type an operator command. For example, “d” is the delete operator. Then you type a motion command like “4l” or “w“. This way you can operate on any text you can move over.

Changing Text

Another operator is “c“, change. It acts just like the “d” operator, except it leaves you in Insert mode. For example, “cw” changes a word. Or more specifically, it deletes a word and then puts you in Insert mode.

To err is human 
   ------->
 c2wbe<Esc>
To be human

This “c2wbe<Esc>” contains these bits:

cThe change operator.
2wMove two words (they are deleted and Insert mode started).
beInsert this text.
<Esc>Back to Normal mode.

The space before “human” isn’t deleted. The c operator works just like the d operator, with one exception: “cw“. It actually works like “ce“, change to end of word. Thus the space after the word isn’t included, which is an exception that dates back to the old Vi. Since many people are used to it now, the inconsistency has remained in vim.

More Changes

Like “dd” deletes a whole line, “cc” changes a whole line. It keeps the existing indent (leading white space) though.

Assim como ” d $ ” é excluído até o final da linha, ” c $ ” é alterado até o final da linha. É como fazer ” d $ ” para excluir o texto e, em seguida, ” a ” para iniciar o modo Inserir e anexar novo texto.

Atalhos

Alguns comandos de movimento do operador são usados ​​com tanta frequência que recebem um comando de letra única:

Comando:Apoia:Descrição:
xd1Exclua o caractere sob o cursor.
XdhExclua o caractere à esquerda do cursor.
Dd $Excluir para final da linha.
Cc $Mude para o final da linha.
sc1Mude um caractere.
SccMude uma linha inteira.

Onde colocar a contagem

Os comandos ” 3dw ” e ” d3w ” excluem três palavras. Se você quer ser realmente exigente, o primeiro comando, ” 3dw “, exclui uma palavra três vezes; o comando ” d3w ” exclui três palavras uma vez, o que é uma diferença sem distinção. Você pode realmente colocar em duas contagens, no entanto. Por exemplo, ” 3d2w ” exclui duas palavras, repetidas três vezes, para um total de seis palavras.

Substituindo por um caractere

O comando ” r ” não é um operador. Ele espera que você digite um caractere e substitui o caractere sob o cursor por ele. Você pode fazer o mesmo com ” cl ” ou com o comando ” s “, mas com ” r ” você não precisa pressionar <Esc>

há algo sombrio aqui 
 rT
Há algo sombrio aqui 
 rt
Há algo grong aqui
 rw 
Há algo errado aqui

O uso de uma contagem com ” r ” faz com que muitos caracteres sejam substituídos pelo mesmo caractere. Exemplo:

Há algo errado aqui 
  5rx 
Há algo xxxxx aqui

Para substituir um caractere por uma quebra de linha, use ” r <Enter> “. Isso exclui um caractere e insere uma quebra de linha. O uso de uma contagem aqui se aplica apenas ao número de caracteres excluídos: ” 4r <Enter> ” substitui quatro caracteres por uma quebra de linha.

Repetindo uma alteração

O comando ” . ” É um dos comandos mais simples e poderosos do vim . Repete a última alteração. Por exemplo, suponha que você esteja editando um arquivo HTML e deseje excluir todas as tags <B> . Você posiciona o cursor no primeiro < e exclui o <B> com o comando ” df> “. Você então vai para o < do próximo </B> e o mata usando o comando ” . “. O comando ” . ” Executa o último comando de alteração (nesse caso, ” df> “). Para excluir outra marca, posicione o cursor no <“comando.

		      Para <B> gerar </B> uma tabela com <B> conteúdo 
 f < encontre primeiro <--->
 df> apague para> ->
 f < encontre próximo <--------->
 . repita df> --->
 f < encontre o próximo <------------->
 . repita df> ->

“As . ” Comando funciona para todas as mudanças que você faz, exceto para o ” u ” (desfazer), Ctrl-R (refazer) e comandos que começam com dois pontos ( : ).

Outro exemplo: você deseja alterar a palavra “quatro” para “cinco”. Aparece várias vezes no seu texto. Você pode fazer isso rapidamente com esta sequência de comandos:

/ quatro <Enter>Encontre a primeira string “quatro”.
cwfive <Esc>Mude a palavra para “cinco”.
nEncontre os próximos “quatro”.
.Repita a alteração para “cinco”.
nEncontre os próximos “quatro”.
.Repita a alteração.

etc.

Modo Visual

Para excluir itens simples, as alterações de movimento do operador funcionam muito bem. Mas muitas vezes não é tão fácil decidir qual comando se move sobre o texto que você deseja alterar. Então você pode usar o modo Visual.

Você inicia o modo Visual pressionando ” v “. Você move o cursor sobre o texto em que deseja trabalhar. Enquanto você faz isso, o texto é destacado. Por fim, digite o comando do operador.

Por exemplo, para excluir da metade de uma palavra para a outra palavra:

Esta é uma amostra de exame do modo visual 
                --------->
	 velllld 
Este é um exemplo de modo visual

Ao fazer isso, você realmente não precisa contar quantas vezes precisa pressionar ” l ” para terminar na posição correta. Você pode ver imediatamente o texto que será excluído quando pressionar ” d “.

Se a qualquer momento você decidir que não quer fazer nada com o texto realçado, basta pressionar <Esc> e o modo Visual será interrompido sem fazer nada.

Selecionando linhas

If you want to work on whole lines, use “V” to start Visual mode. You will see right away that the whole line is highlighted, without moving around. When you move left or right nothing changes. When you move up or down the selection is extended whole lines at a time.

For example, select three lines with “Vjj“:

                  ┌────────────────────────┐
                  | text more text         |
               >> | more text more text    | 
selected lines >> | text text text         | 
               >> | text more              | 
                  | more text more         |
                  └────────────────────────┘

Selecting Blocks

If you want to work on a rectangular block of characters, use Ctrl-V to start Visual mode. This is very useful when working on tables.

name		Q1	Q2	Q3
pierre		123	455	234
john		0	90	39
steve		392	63	334

To delete the middle “Q2” column, move the cursor to the “Q” of “Q2“. Press Ctrl-V to start blockwise Visual mode. Now move the cursor three lines down with “3j” and to the next word with “w“. You can see the first character of the last column is included. To exclude it, use “h“. Now press “d” and the middle column is gone.

Going To The Other Side

If you have selected some text in Visual mode, and discover that you need to change the other end of the selection, use the “o” command. The cursor will go to the other end, and you can move the cursor to change where the selection starts. Pressing “o” again brings you back to the other end. When using blockwise selection, you have four corners. “o” only takes you to one of the other corners, diagonally. Use “O” to move to the other corner in the same line. Note that “o” and “O” in Visual mode work very differently from Normal mode, where they open a new line below or above the cursor.

Moving Text

When you delete something with the “d“, “x“, or another command, the text is saved. You can paste it back by using the p command. (The vim name for this is “put”).

Take a look at how this works. First you will delete an entire line, by putting the cursor on the line you want to delete and typing “dd“. Now you move the cursor to where you want to put the line and use the “p” (put) command. The line is inserted on the line below the cursor.

	a line		a line	      a line
	line 2	 dd	line 3	 p line 3
	line 3			      line 2

Because you deleted an entire line, the “p” command placed the text line below the cursor. If you delete part of a line (a word, for instance), the “p” command puts it just after the cursor.

Some more boring try text to out commands. 
		 ---->
		 dw
Some more boring text to out commands. 
		 ------->
		 welp
Some more boring text to try out commands.

Putting

The “P” command puts text like “p“, but before the cursor. When you deleted a whole line with “dd“, “P” will put it back above the cursor. When you deleted a word with “dw“, “P” will put it back just before the cursor.

You can repeat putting as many times as you like. The same text will be used.

You can use a count with “p” and “P“. The text will be repeated as many times as specified with the count. Thus “dd” and then “3p” puts three copies of the same deleted line.

Swapping Characters

vim makes it easy to correct such problems such as accidentally typing “teh” for “the”. Just put the cursor on the e of “teh” and execute the command “xp”. This works as follows: “x” deletes the character e and places it in a register. “p” puts the text after the cursor, which is after the “h”.

teh
x 
th
p
the

Copying Text

To copy text from one place to another, you could delete it, use “u” to undo the deletion and then “p” to put it somewhere else. There is an easier way: yanking. The “y” operator copies text into a register. Then a “p” command can be used to put it.

Yanking is just a vim name for copying. The “c” letter was already used for the change operator, and “y” was still available. Calling this operator “yank” made it easier to remember to use the “y” key.

Since “y” is an operator, you use “yw” to yank a word. A count is possible as usual. To yank two words use “y2w“. Example:

	let sqr = LongVariable * 
		 -------------->
		 y2w
	let sqr = LongVariable * 
			 p
	let sqr = LongVariable * LongVariable

Notice that “yw” includes the white space after a word. If you don’t want this, use “ye“.

The “yy” command yanks a whole line, just like “dd” deletes a whole line. Unexpectedly, while “D” deletes from the cursor to the end of the line, “Y” works like “yy“, it yanks the whole line. Watch out for this inconsistency! Use “y$” to yank to the end of the line.

	a text line yy
        line 2
        last line
	a text line
	line 2 p
 	last line
	a text line
	line 2
	a text line
	last line

Using The Clipboard

If you are using the GUI version of vim (gvim), you can find the “Copy” item in the “Edit” menu. First select some text with Visual mode, then use the Edit/Copy menu. The selected text is now copied to the clipboard. You can paste the text in other programs. In vim itself too.

If you have copied text to the clipboard in another application, you can paste it in vim with the Edit/Paste menu. This works in Normal mode and Insert mode. In Visual mode the selected text is replaced with the pasted text.

The “Cut” menu item deletes the text before it’s put on the clipboard. The “Copy”, “Cut” and “Paste” items are also available in the popup menu (only when there is a popup menu, of course). If your vim has a toolbar, you can also find these items there.

If you are not using the GUI, or if you don’t like using a menu, you have to use another way. You use the normal “y” (yank) and “p” (put) commands, but prepend “* (double-quote star) before it. To copy a line to the clipboard:

"*yy

To put text from the clipboard back into the text:

"*p

This only works on versions of vim that include clipboard support.

Text Objects

If the cursor is in the middle of a word and you want to delete that word, you need to move back to its start before you can do “dw”. There is a simpler way to do this: “daw“.

	this is some example text. 
		 daw
	this is some text.

The “d” of “daw” is the delete operator. “aw” is a text object. Hint: “aw” stands for “A Word”. Thus “daw” is “Delete A Word”. To be precise, the white space after the word is also deleted (the white space before the word at the end of the line).

Using text objects is the third way to make changes in vim. We already had operator-motion and Visual mode. Now we add operator-text object.

It is very similar to operator-motion, but instead of operating on the text between the cursor position before and after a movement command, the text object is used as a whole. It doesn’t matter where in the object the cursor was.

To change a whole sentence use “cis“. Take this text:

	Hello there.  This 
	is an example.  Just 
	some text.

Move to the start of the second line, on “is an”. Now use “cis”:

	Hello there.    Just 
	some text.

The cursor is in between the blanks in the first line. Now you type the new sentence “Another line.”:

	Hello there.  Another line.  Just 
	some text.

cis” consists of the “c” (change) operator and the “is” text object. This stands for “Inner Sentence”. There is also the “as” (a sentence) object. The difference is that “as” includes the white space after the sentence and “is” doesn’t. If you would delete a sentence, you want to delete the white space at the same time, thus use “das“. If you want to type new text the white space can remain, thus you use “cis“.

You can also use text objects in Visual mode. It will include the text object in the Visual selection. Visual mode continues, thus you can do this several times. For example, start Visual mode with “v” and select a sentence with “as“. Now you can repeat “as” to include more sentences. Finally you use an operator to do something with the selected sentences.

Replace Mode

The “R” command causes vim to enter replace mode. In this mode, each character you type replaces the one under the cursor. This continues until you type <Esc>.

In this example, you start Replace mode on the first “t” of “text“:

This is text. 
Rinteresting.<Esc>
This is interesting.

You may have noticed that this command replaced 5 characters in the line with twelve others. The “R” command automatically extends the line if it runs out of characters to replace. It will not continue on the next line.

You can switch between Insert mode and Replace mode with the <Insert> key.

When you use <BS> (backspace) to make correction, you will notice that the old text is put back. Thus it works like an undo command for the last typed character.

Conclusion

The operators, movement commands and text objects give you the possibility to make lots of combinations. Now that you know how it works, you can use N operators with M movement commands to make N * M commands!

For example, there are other ways to delete pieces of text. Here are a few often used ones:

xDelete character under the cursor (short for “dl“).
XDelete character before the cursor (short for “dh“).
DDelete from cursor to end of line (short for “d$“).
dwDelete from cursor to next start of word.
dbDelete from cursor to previous start of word.
diwDelete word under the cursor (excluding white space).
dawDelete word under the cursor (including white space).
dGDelete until the end of the file.
dggDelete until the start of the file.

If you use “c” instead of “d” they become change commands. And with “y” you yank the text. And so forth.

There are a few often used commands to make changes that didn’t fit somewhere else:

~Change case of the character under the cursor, and move the cursor to the next character. This is not an operator (unless ‘tildeop‘ is set), thus you can’t use it with a motion command. It does work in Visual mode and changes case for all the selected text then.
IStart Insert mode after moving the cursor to the first non-blank in the line.
AStart Insert mode after moving the cursor to the end of the line.

The vimrc File

You probably got tired of typing commands that you use very often. To start vim with all your favorite option settings and mappings, you write them in what is called the vimrc file. vim executes the commands in this file when it starts up.

If you already have a vimrc file (e.g., when your sysadmin has one setup for you), you can edit it this way:

:edit $MYVIMRC

If you don’t have a vimrc file yet, you can create one. The “:version” command mentions the name of the “user vimrc file” vim looks for.

For Unix and Macintosh this file is always used and is recommended: ~/.vimrc

For MS-DOS and MS-Windows you can use one of these: $HOME/_vimrc$VIM/_vimrc

The vimrc file can contain all the commands that you type after a colon. The most simple ones are for setting options. For example, if you want vim to always start with the ‘incsearch‘ option on, add this line you your vimrc file:

set incsearch

For this new line to take effect you need to exit vim and start it again. Later you will learn how to do this without exiting vim.

Simple Mappings

A mapping enables you to bind a set of vim commands to a single key. Suppose, for example, that you need to surround certain words with curly braces. In other words, you need to change a word such as “amount” into “{amount}”. With the :map command, you can tell vim that the F5 key does this job. The command is as follows:

:map <F5> i{<Esc>ea}<Esc>

When entering this command, you must enter <F5> by typing four characters. Similarly, <Esc> is not entered by pressing the <Esc> key, but by typing five characters. Watch out for this difference.

Let’s break this down:

<F5>The F5 function key. This is the trigger key that causes the command to be executed as the key is pressed.
i{<Esc>Insert the { character. The <Esc> key ends Insert mode.
eMove to the end of the word.
a}<Esc>Append the } to the word.

After you execute the “:map” command, all you have to do to put {} around a word is to put the cursor on the first character and press F5.

In this example, the trigger is a single key; it can be any string. But when you use an existing vim command, that command will no longer be available. You better avoid that.

One key that can be used with mappings is the backslash. Since you probably want to define more than one mapping, add another character. You could map “\p” to add parentheses around a word, and “\c” to add curly braces, for example:

:map \p i(<Esc>ea)<Esc>
:map \c i{<Esc>ea}<Esc>

You need to type the \ and the p quickly after another, so that vim knows they belong together.

The “:map” command (with no arguments) lists your current mappings. At least the ones for Normal mode.

Adding A Global Plugin

vim‘s functionality can be extended by adding plugins. A plugin is nothing more than a vim script file that is loaded automatically when vim starts. You can add a plugin very easily by dropping it in your plugin directory. {not available when vim was compiled without the |+eval| feature}

There are two types of plugins: global plugins, which are used for all kinds of files; and filetype plugins, which are only used for a specific type of file.

When you start vim, it will automatically load a number of global plugins. You don’t have to do anything for this. They add functionality that most people will want to use, but which was implemented as a vim script instead of being compiled in.

You can add a global plugin to add functionality that will always be present when you use vim. There are only two steps for adding a global plugin: get a copy of the plugin, and drop it in the right directory.

Getting A Global Plugin

Some global plugins come with vim. You can find them in the directory $VIMRUNTIME/macros and its sub-directories.

Others can be downloaded from the official vim website, https://vim.sourceforge.io/.

Using A Global Plugin

First read the text in the plugin itself to check for any special conditions. Then copy the file to your plugin directory:

systemplugin directory
Unix~/.vim/plugin/
PC and OS/2$HOME/vimfiles/plugin or $VIM/vimfiles/plugin
Amigas:vimfiles/plugin
Macintosh$VIM:vimfiles:plugin
macOS X~/.vim/plugin/
RISC-OSChoices:vimfiles.plugin

Example for Unix (assuming you didn’t have a plugin directory yet):

mkdir ~/.vim
mkdir ~/.vim/plugin
cp /usr/local/share/vim/vim60/macros/justify.vim ~/.vim/plugin

That’s all! Now you can use the commands defined in this plugin to justify text.

Instead of putting plugins directly into the plugin/ directory, you may better organize them by putting them into subdirectories under plugin/. As an example, consider using “~/.vim/plugin/perl/*.vim” for all your Perl plugins.

Filetype Plugins

The vim distribution comes with a set of plugins for different filetypes that you can start using with this command:

:filetype plugin on

If you are missing a plugin for a filetype you are using, or you found a better one, you can add it. There are two steps for adding a filetype plugin: get a copy of the plugin, and drop it in the right directory.

Often Used Options

vim is an extensive program, and so it has a lot of options! Most of them you will hardly ever use. Some of the more useful ones will be mentioned here. Don’t forget you can find more help on these options with the “:help” command, with single quotes before and after the option name. For example:

:help 'wrap'

In case you have messed up an option value, you can set it back to the default by putting an ampersand (&) after the option name. Example:

:set iskeyword&

vim normally wraps long lines, so that you can see all of the text. Sometimes it’s better to let the text continue right of the window. Then you need to scroll the text left-right to see all of a long line. Switch wrapping off with this command:

:set nowrap

vim will automatically scroll the text when you move to text that is not displayed. To see a context of ten characters, do this:

:set sidescroll=10

This doesn’t change the text in the file, only the way it is displayed.

Most commands for moving around will stop moving at the start and end of a line. You can change that with the ‘whichwrap‘ option. This sets it to the default value:

:set whichwrap=b,s

This allows the <BS> key, when used in the first position of a line, to move the cursor to the end of the previous line. And the <Space> key moves from the end of a line to the start of the next one.

To allow the cursor keys <Left> and <Right> to also wrap, use this command:

:set whichwrap=b,s,<,>

This is still only for Normal mode. To let <Left> and <Right> do this in Insert mode as well:

:set whichwrap=b,s,<,>,[,]

There are a few other flags that can be added, see ‘whichwrap‘.

Using Syntax Highlighting

It all starts with one simple command:

:syntax enable

That should work in most situations to get color in your files. The vim command will automagically detect the type of file and load the right syntax highlighting. Suddenly comments are blue, keywords brown and strings red. This makes it easy to overview the file. After a while you will find that black&white text slows you down!

If you always want to use syntax highlighting, put the “:syntax enable” command in your vimrc file.

If you want syntax highlighting only when the terminal supports colors, you can put this in your vimrc file:

if &t_Co > 1
   syntax enable
endif

If you want syntax highlighting only in the GUI version, put the “:syntax enable” command in your gvimrc file.

Choosing Colors

vim guesses the background color that you are using. If it is black (or another dark color) it will use light colors for text. If it is white (or another light color) it will use dark colors for text. If vim guessed wrong the text will be hard to read. To solve this, set the ‘background‘ option. For a dark background:

:set background=dark

And for a light background:

:set background=light

Make sure you put this before the “:syntax enable” command, otherwise the colors will already have been set. You could do “:syntax reset” after setting ‘background‘ to make vim set the default colors again.

If you don’t like the default colors, you can select another color scheme. In the GUI use the Edit/Color Scheme menu. You can also type the command:

:colorscheme evening

evening” is the name of the color scheme. There are several others you might want to try out. Look in the directory $VIMRUNTIME/colors.

When you found the color scheme that you like, add the “:colorscheme” command to your vimrc file.

You could also write a color scheme. This is how you do it:

(1) Select a color scheme that comes close. Copy this file to your vim directory. For Unix, this should work. These commands are done from within vim:

:!mkdir ~/.vim/colors
:!cp $VIMRUNTIME/colors/morning.vim ~/.vim/colors/mine.vim

(2) Edit the color scheme file. These entries are useful:

termattributes in a B&W terminal
ctermattributes in a color terminal
ctermfgforeground color in a color terminal
ctermbgbackground color in a color terminal
guiattributes in the GUI
guifgforeground color in the GUI
guibgbackground color in the GUI

For example, to make comments green:

:highlight Comment ctermfg=green guifg=green

Attributes you can use for “cterm” and “gui” are “bold” and “underline“. If you want both, use “bold,underline“.

(3) Tell vim to always use your color scheme. Put this line in your vimrc:

colorscheme mine

If you want to see what the most often used color combinations look like, use this command:

:runtime syntax/colortest.vim

You will see text in various color combinations. You can check which ones are readable and look nice.

Editing Another File

Once you are in vim, you can start editing another file using this command:

:edit foo.txt

You can use any file name instead of “foo.txt”. vim will close the current file and open the new one. If the current file has unsaved changes, however, vim displays an error message and does not open the new file:

E37: No write since last change (use ! to override)

vim puts an error ID at the start of each error message. If you do not understand the message or what caused it, look in the help system for this ID. In this case:

:help E37

At this point, you have a number of alternatives. You can write the file using this command:

:write

Or you can force vim to discard your changes and edit the new file, using the force (!) character:

:edit! foo.txt

If you want to edit another file, but not write the changes in the current file yet, you can make it hidden:

:hide edit foo.txt

The text with changes is still there, but you can’t see it.

Editing A List Of Files

You can start vim to edit a sequence of files. For example:

vim one.c two.c three.c

This command starts vim and tells it that you will be editing three files. vim displays just the first file. After you have done your thing in this file, to edit the next file you use this command:

:next

If you have unsaved changes in the current file, you will get an error message and the “:next” will not work. This is the same problem as with “:edit” mentioned in the previous section. To abandon the changes:

:next!

But mostly you want to save the changes and move on to the next file. There is a special command for this:

:wnext

This does the same as using two separate commands:

:write
:next

To see which file in the argument list you are editing, look in the window title. It should show something like “(2 of 3)“. This means you are editing the second file out of three files.

If you want to see the list of files, use this command:

:args

This is short for “arguments”. The output might look like this:

one.c [two.c] three.c

These are the files you started vim with. The one you are currently editing, “two.c”, is in square brackets.

Moving From File To File

To go back one file:

:previous

This is just like the “:next” command, except that it moves in the other direction. Again, there is a shortcut command for when you want to write the file first:

:wprevious

To move to the very last file in the list:

:last

And to move back to the first one again:

:first

There is no “:wlast” or “:wfirst” command however.

You can use a count for “:next” and “:previous“. To skip two files forward:

:2next

Backup Files

Usually vim does not produce a backup file. If you want to have one, all you need to do is execute the following command:

:set backup

The name of the backup file is the original file with a tilde (“~“) added to the end. If your file is named data.txt, for example, the backup file name is data.txt~. If you do not like the fact that the backup files end with ~, you can change the extension:

:set backupext=.bak

This will use data.txt.bak instead of data.txt~.

Another option that matters here is ‘backupdir‘. It specifies where the backup file is written. The default, to write the backup in the same directory as the original file, will mostly be the right thing.

When the ‘backup‘ option isn’t set but the ‘writebackup‘ is, vim will still create a backup file. However, it is deleted as soon as writing the file was completed successfully. This functions as a safety against losing your original file when writing fails in some way (disk full is the most common cause).

If you are editing source files, you might want to keep the file before you make any changes. But the backup file will be overwritten each time you write the file. Thus it only contains the previous version, not the first one.

To make vim keep the original file, set the ‘patchmode’ option. This specifies the extension used for the first backup of a changed file. Usually you would do this:

:set patchmode=.orig

When you now edit the file data.txt for the first time, make changes and write the file, vim will keep a copy of the unchanged file under the name “data.txt.orig“.

If you make further changes to the file, vim will notice that “data.txt.orig” already exists and leave it alone. Further backup files will then be called “data.txt~” (or whatever you specified with ‘backupext‘).

If you leave ‘patchmode‘ empty (that is the default), the original file will not be kept.

Using Registers

When you want to copy several pieces of text from one file to another, having to switch between the files and writing the target file takes a lot of time. To avoid this, copy each piece of text to its own register.

A register is a place where vim stores text. Here we will use the registers named a to z (later you will find out there are others). Let’s copy a sentence to the f register (f for First):

"fyas

The “yas” command yanks a sentence like before. It’s the “f that tells vim the text should be place in the f register. This must come just before the yank command.

Now yank three whole lines to the l register (l for line):

"l3Y

The count could be before the “l just as well. To yank a block of text to the b (for block) register:

Ctrl-Vjjww"by

Notice that the register specification “b is just before the “y” command. This is required. If you would have put it before the “w” command, it would not have worked.

Now you have three pieces of text in the fl and b registers. Edit another file, move around and place the text where you want it:

"fp

Again, the register specification “f comes before the “p” command.

You can put the registers in any order. And the text stays in the register until you yank something else into it. Thus you can put it as many times as you like.

When you delete text, you can also specify a register. Use this to move several pieces of text around. For example, to delete-a-word and write it in the w register:

"wdaw

Again, the register specification comes before the delete command “d“.

Viewing A File Read-Only

Sometimes you only want to see what a file contains, without the intention to ever write it back. There is the risk that you type “:w” without thinking and overwrite the original file anyway. To avoid this, edit the file read-only.

To start vim in readonly mode, use this command:

vim -R file

On Unix this command should do the same thing:

view file

You are now editing “file” in read-only mode. When you try using “:w” you will get an error message and the file won’t be written.

When you try to make a change to the file vim gives you a warning:

W10: Warning: Changing a readonly file

The change will be done though. This allows for formatting the file, for example, to be able to read it easily.

If you make changes to a file and forgot that it was read-only, you can still write it. Add the ! to the write command to force writing.

If you really want to forbid making changes in a file, do this:

vim -M file

Now every attempt to change the text will fail. The help files are like this, for example. If you try to make a change you get this error message:

E21: Cannot make changes, 'modifiable' is off

You could use the -M argument to set up vim to work in a viewer mode. This is only voluntary though, since these commands will remove the protection:

:set modifiable
:set write

Saving As A New File Name

A clever way to start editing a new file is by using an existing file that contains most of what you need. For example, you start writing a new program to move a file. You know that you already have a program that copies a file, thus you start with:

:edit copy.c

You can delete the stuff you don’t need. Now you need to save the file under a new name. The “:saveas” command can be used for this:

:saveas move.c

vim will write the file under the given name, and edit that file. Thus the next time you do “:write“, it will write “move.c“. “copy.c” remains unmodified.

When you want to change the name of the file you are editing, but don’t want to write the file, you can use this command:

:file move.c

vim will mark the file as “not edited”. This means that vim knows this is not the file you started editing. When you try to write the file, you might get this message:

E13: File exists (use ! to override)

This protects you from accidentally overwriting another file.

Splitting Windows

The easiest way to open a new window is to use the following command:

:split

This command splits the screen into two windows and leaves the cursor in the top one:

        ┌──────────────────────────────────┐
	|/* file one.c */		   |
	|~				   |
	|~				   |
	|one.c=============================|
	|/* file one.c */		   |
	|~				   |
	|one.c=============================|
	|				   |
        └──────────────────────────────────┘

What you see here is two windows on the same file. The line with “====” is that status line. It displays information about the window above it. In practice the status line will be in reverse video.

The two windows allow you to view two parts of the same file. For example, you could make the top window show the variable declarations of a program, and the bottom one the code that uses these variables.

The Ctrl-W w command can be used to jump between the windows. If you are in the top window, Ctrl-W w jumps to the window below it. If you are in the bottom window it will jump to the first window. Ctrl-W Ctrl-W does the same thing, in case you let go of the Ctrl key a bit later.

To close a window, use the command:

:close

Actually, any command that quits editing a file works, like “:quit” and “ZZ“. But “:close” prevents you from accidentally exiting vim when you close the last window.

If you have opened a whole bunch of windows, but now want to concentrate on one of them, this command will be useful:

:only

This closes all windows, except for the current one. If any of the other windows has changes, you will get an error message and that window won’t be closed.

Splitting A Window On Another File

The following command opens a second window and starts editing the given file:

:split two.c

If you were editing one.c, then the result looks like this:

        ┌──────────────────────────────────┐
	|/* file two.c */		   |
	|~				   |
	|~				   |
	|two.c=============================|
	|/* file one.c */		   |
	|~				   |
	|one.c=============================|
	|				   |
        └──────────────────────────────────┘

To open a window on a new, empty file, use this:

:new

You can repeat the “:split” and “:new” commands to create as many windows as you like.

Window Size

The “:split” command can take a number argument. If specified, this will be the height of the new window. For example, the following opens a new window three lines high and starts editing the file alpha.c:

:3split alpha.c

For existing windows you can change the size in several ways. When you have a working mouse, it is easy: Move the mouse pointer to the status line that separates two windows, and drag it up or down.

To increase the size of a window: Ctrl-W +

To decrease it: Ctrl-W –

Both of these commands take a count and increase or decrease the window size by that many lines. Thus “4 Ctrl-W +” make the window four lines higher.

To set the window height to a specified number of lines: {height}Ctrl-W _

That’s: a number {height}Ctrl-W and then an underscore. To make a window as high as it can be, use the Ctrl-W _ command without a count.

Using The Mouse

In vim you can do many things very quickly from the keyboard. Unfortunately, the window resizing commands require quite a bit of typing. In this case, using the mouse is faster. Position the mouse pointer on a status line. Now press the left mouse button and drag. The status line moves, thus making the window on one side higher and the other smaller.

Options

The ‘winheight‘ option can be set to a minimal desired height of a window and ‘winminheight‘ to a hard minimum height. Likewise, there is ‘winwidth‘ for the minimal desired width and ‘winminwidth‘ for the hard minimum width. The ‘equalalways‘ option, when set, makes vim equalize the windows sizes when a window is closed or opened.

Vertical splits

The “:split” command creates the new window above the current one. To make the window appear at the left side, use:

:vsplit

or:

:vsplit two.c

The result looks something like this:

        ┌──────────────────────────────────────┐
	|/* file two.c */  ||/* file one.c */  ||||
	|~		   ||~		       ||||
	|~		   ||~		       ||||
	|~		   ||~		       ||||
	|two.c===============one.c=============|
	|				       |
        └──────────────────────────────────────┘

Actually, the || lines in the middle will be in reverse video. This is called the vertical separator. It separates the two windows left and right of it.

There is also the “:vnew” command, to open a vertically split window on a new, empty file. Another way to do this:

:vertical new

The “:vertical” command can be inserted before another command that splits a window. This will cause that command to split the window vertically instead of horizontally. If the command doesn’t split a window, it works unmodified.

Moving Between Windows

Since you can split windows horizontally and vertically as much as you like, you can create almost any layout of windows. Then you can use these commands to move between them:

Ctrl-W hmove to the window on the left
Ctrl-W jmove to the window below
Ctrl-W kmove to the window above
Ctrl-W lmove to the window on the right
Ctrl-W tmove to the TOP window
Ctrl-W bmove to the BOTTOM window

You will notice the same letters as used for moving the cursor. And the cursor keys can also be used, if you like.

Moving windows

You have split a few windows, but now they are in the wrong place. Then you need a command to move the window somewhere else. For example, you have three windows like this:

        ┌──────────────────────────────────┐
	|/* file two.c */		   |
	|~				   |
	|~				   |
	|two.c=============================|
	|/* file three.c */		   |
	|~				   |
	|~				   |
	|three.c===========================|
	|/* file one.c */		   |
	|~				   |
	|one.c=============================|
	|				   |
        └──────────────────────────────────┘

Clearly the last one should be at the top. Go to that window (using Ctrl-W w) and the type this command: Ctrl-W K

This uses the uppercase letter K. What happens is that the window is moved to the very top. You will notice that K is again used for moving upwards. When you have vertical splits, Ctrl-W K moves the current window to the top and make it occupy the full width of the vim window. If this is your layout:

        ┌───────────────────────────────────────────┐
	|/* two.c */ ||/* three.c */ ||/* one.c */  |
	|~	     ||~	     ||~	    |
	|~	     ||~	     ||~	    |
	|~	     ||~	     ||~	    |
	|~	     ||~	     ||~	    |
	|~	     ||~	     ||~	    |
	|two.c=========three.c=========one.c========|
	|					    |
        └───────────────────────────────────────────┘

Then using Ctrl-W K in the middle window (three.c) will result in:

	┌───────────────────────────────────────────┐
	|/* three.c */				    |
	|~					    |
	|~					    |
	|three.c====================================|
	|/* two.c */	      ||/* one.c */	    ||||
	|~		      ||~		    ||||
	|two.c==================one.c===============|
	|					    |
	└───────────────────────────────────────────┘

The other three similar commands (you can probably guess these now):

Ctrl-W Hmove window to the far left
Ctrl-W Jmove window to the bottom
Ctrl-W Lmove window to the far right

Commands For All Windows

When you have several windows open and you want to quit vim, you can close each window separately. A quicker way is using this command:

:qall

This stands for “quit all”. If any of the windows contain changes, vim will not exit. The cursor will automatically be positioned in a window with changes. You can then either use “:write” to save the changes, or “:quit!” to throw them away.

If you know there are windows with changes, and you want to save all these changes, use this command:

:wall

This stands for “write all”. But actually, it only writes files with changes. The vim command knows it doesn’t make sense to write files that were not changed.

And then there is the combination of “:qall” and “:wall“: the “write and quit all” command:

:wqall

This writes all modified files and quits vim.

Finally, there is a command that quits vim and throws away all changes:

:qall!

Be careful, there is no way to undo this command!

Opening A Window For All Files

To make vim open a window for each file, start it with the “-o” argument:

vim -o one.txt two.txt three.txt

This results in:

	┌───────────────────────────────┐
	|file one.txt			|
	|~				|
	|one.txt========================|
	|file two.txt			|
	|~				|
	|two.txt========================|
	|file three.txt			|
	|~				|
	|three.txt======================|
	|				|
	└───────────────────────────────┘

The “-O” argument is used to get vertically split windows.

When vim is already running, the “:all” command opens a window for each file in the argument list. “:vertical all” does it with vertical splits.

Viewing Differences With vimdiff

There is a special way to start vim, which shows the differences between two files. Let’s take a file “main.c” and insert a few characters in one line. Write this file with the ‘backup’ option set, so that the backup file “main.c~” will contain the previous version of the file.

Type this command in a shell (not in vim):

vimdiff main.c~ main.c

vim will start, with two windows side by side. You will only see the line in which you added characters, and a few lines above and below it.

	 VV		      VV
	┌─────────────────────────────────────────┐
	|+ +--123 lines: /* a|+ +--123 lines: /* a|  <- fold|||
	|  text		     |	text		  |
	|  text		     |	text		  |
	|  text		     |	text		  |
	|  text		     |	changed text	  |  <- changed line
	|  text		     |	text		  |
	|  text		     |	------------------|  <- deleted line
	|  text		     |	text		  |
	|  text		     |	text		  |
	|  text		     |	text		  |
	|+ +--432 lines: text|+ +--432 lines: text|  <- fold|||
	|  ~		     |	~		  |
	|  ~		     |	~		  |
	|main.c~==============main.c==============|
	|					  |
	└─────────────────────────────────────────┘

This picture doesn’t show the highlighting, use the vimdiff command for a better look.

The lines that were not modified have been collapsed into one line. This is called a closed fold. They are indicated in the picture with “<- fold“. Thus the single fold line at the top stands for 123 text lines. These lines are equal in both files.

The line marked with “<- changed line” is highlighted, and the inserted text is displayed with another color. This clearly shows what the difference is between the two files.

The line that was deleted is displayed with “” in the main.c window. See the “<- deleted line” marker in the picture. These characters are not really there. They just fill up main.c, so that it displays the same number of lines as the other window.

The Fold Column

Each window has a column on the left with a slightly different background. In the picture above these are indicated with “VV”. You notice there is a plus character there, in front of each closed fold. Move the mouse pointer to that plus and click the left button. The fold will open, and you can see the text that it contains.

The fold column contains a minus sign for an open fold. If you click on this -, the fold will close. Obviously, this only works when you have a working mouse. You can also use “zo” to open a fold and “zc” to close it.

Diffing In vim

Another way to start in diff mode can be done from inside vim. Edit the “main.c” file, then make a split and show the differences:

:edit main.c
:vertical diffsplit main.c~

The “:vertical” command is used to make the window split vertically. If you omit this, you will get a horizontal split.

If you have a patch or diff file, you can use the third way to start diff mode. First edit the file to which the patch applies. Then tell vim the name of the patch file:

:edit main.c
:vertical diffpatch main.c.diff

WARNING: The patch file must contain only one patch, for the file you are editing. Otherwise, you will get a lot of error messages, and some files might be patched unexpectedly.

The patching will only be done to the copy of the file in vim. The file on your harddisk will remain unmodified (until you decide to write the file).

Scroll Binding

When the files have more changes, you can scroll in the usual way. vim will try to keep both the windows start at the same position, so you can easily see the differences side by side.

When you don’t want this for a moment, use this command:

:set noscrollbind

Jumping To Changes

When you have disabled folding in some way, it may be difficult to find the changes. Use this command to jump forward to the next change:

]c

To go the other way use:

[c

Prepended a count to jump further away.

Removing Changes

You can move text from one window to the other. This either removes differences or adds new ones. The vim command doesn’t keep the highlighting updated in all situations. To update it use this command:

:diffupdate

To remove a difference, you can move the text in a highlighted block from one window to another. Take the “main.c” and “main.c~” example above. Move the cursor to the left window, on the line that was deleted in the other window. Now type this command:

dp

The change will be removed by putting the text of the current window in the other window. “dp” stands for “diff put”.

You can also do it the other way around. Move the cursor to the right window, to the line where “changed” was inserted. Now type this command:

do

A alteração será removida agora, obtendo o texto da outra janela. Como não há mais alterações agora, o vim coloca todo o texto em uma dobra fechada. “do” significa “diff obtenção”. “dg” teria sido melhor, mas isso já tem um significado diferente (“dgg” exclui do cursor até a primeira linha).

Opções diversas

A opção ‘ laststatus ‘ pode ser usada para especificar quando a última janela tem uma linha de status:

0 0Nunca
1somente quando houver janelas divididas (o padrão)
2sempre

Muitos comandos que editam outro arquivo têm uma variante que divide a janela. Para comandos da linha de comando, isso é feito acrescentando um “s”. Por exemplo: ” : tag ” pula para uma tag, ” : stag ” divide a janela e pula para uma tag.

Para comandos no modo Normal, um Ctrl-W é anexado. Ctrl- ^ pula para o arquivo alternativo, Ctrl-W Ctrl- ^ divide a janela e edita o arquivo alternativo.

A opção ‘ splitbelow ‘ pode ser definida para fazer uma nova janela aparecer abaixo da janela atual. A opção ‘ splitright ‘ pode ser definida para fazer uma janela dividida verticalmente aparecer à direita da janela atual.

Ao dividir uma janela, você pode acrescentar um comando modificador para saber onde a janela deve aparecer:

: leftabove {cmd}esquerda ou acima da janela atual
: aboveleft {cmd}idem
: rightbelow {cmd}direita ou abaixo da janela atual
: abaixo da direita {cmd}idem
: topleft {cmd}na parte superior ou esquerda da janela do vim
: botright {cmd}na parte inferior ou direita da janela do vim

Páginas da guia

Você deve ter notado que as janelas nunca se sobrepõem. Isso significa que você fica rapidamente sem espaço na tela. A solução para isso é chamada de páginas da guia.

Suponha que você esteja editando “este arquivo”. Para criar uma nova página de guia, use este comando:

: tabedit thatfile

Isso editará o arquivo “thatfile” em uma janela que ocupa toda a janela do vim. E você notará uma barra na parte superior com os dois nomes de arquivo:

	┌─────────────────────────────────┐
	| thisfile | / thatfile / __________ X | (esse arquivo está em negrito)
	| / * esse arquivo * / |
	| isso |
	| isso |
	| ~ |
	| ~ |
	| ~ |
	| |
	└─────────────────────────────────┘

Agora você tem duas páginas de guia. O primeiro tem uma janela para “thisfile” e o segundo uma janela para “thatfile”. É como duas páginas uma em cima da outra, com uma guia destacada em cada página mostrando o nome do arquivo.

Agora use o mouse para clicar em “thisfile” na linha superior. O resultado é

	┌─────────────────────────────────┐
	| / thisfile / | esse arquivo __________ X | (este arquivo está em negrito)
	| / * thisfile * / |
	| isso |
	| isso |
	| ~ |
	| ~ |
	| ~ |
	| |
	└─────────────────────────────────┘

Assim, você pode alternar entre as guias clicando no rótulo na linha superior. Se você não possui um mouse ou não deseja usá-lo, pode usar o comando “gt”. Você pode lembrá-lo como uma abreviação de “Goto Tab”.

Agora vamos criar outra aba com o comando:

: separador de separadores

Isso cria uma nova página de guia com uma janela que está editando o mesmo buffer que a janela em que estávamos:

	┌────────────────────────────────────┐
	| thisfile | / thisfile / | thatfile __X | (este arquivo está em negrito)
	| / * thisfile * / |
	| isso |
	| isso |
	| ~ |
	| ~ |
	| ~ |
	| |
	└────────────────────────────────────┘

Você pode colocar ” : tab ” antes de qualquer comando Ex que abrir uma janela. A janela será aberta em uma nova página de guia. Outro exemplo:

: tab help gt

Irá mostrar o texto de ajuda para ” gt ” em uma nova página de guia.

Mais algumas coisas que você pode fazer com as guias:

  • clique com o mouse no espaço após o último rótulo. A próxima aba será selecionada, como com ” gt “.
  • click with the mouse on the “X” in the top right corner. The current tab page will be closed. Unless there are unsaved changes in the current tab page.
  • double click with the mouse in the top line. A new tab page will be created.
  • the “tabonly” command. Closes all tab pages except the current one. Unless there are unsaved changes in other tab pages.

Macros

The “.” command repeats the preceding change. But what if you want to do something more complex than a single change? That’s where command recording comes in, better known as a macro. There are three steps:

(1) The “q{register}” command starts recording keystrokes into the register named {register}. The register name must be between a and z.

(2) Type your commands.

(3) To finish recording, press q (without any extra character).

You can now execute the macro by typing the command “@{register}”. Take a look at how to use these commands in practice. You have a list of file names that look like this:

	stdio.h 
	fcntl.h 
	unistd.h 
	stdlib.h

And what you want is the following:

	#include "stdio.h" 
	#include "fcntl.h" 
	#include "unistd.h" 
	#include "stdlib.h" 

You start by moving to the first character of the first line. Next you execute the following commands:

qaStart recording a macro in register a.
^Move to the beginning of the line.
i#include “<Esc>Insert the string #include “ at the beginning of the line.
$Move to the end of the line.
a”<Esc>Append the character double quotation mark () to the end of the line.
jGo to the next line.
qStop recording the macro.

Now that you have done the work once, you can repeat the change by typing the command “@a” three times.

The “@a” command can be preceded by a count, which will cause the macro to be executed that number of times. In this case you would type:

3@a

Move And Execute

You might have the lines you want to change in various places. Just move the cursor to each location and use the “@a” command. If you have done that once, you can do it again with “@@“. That’s a bit easier to type. If you now execute register b with “@b“, the next “@@” will use register b.

If you compare the playback method with using “.“, there are several differences. First of all, “.” can only repeat one change. As seen in the example above, “@a” can do several changes, and move around as well. Secondly, “.” can only remember the last change. Executing a register allows you to make any changes and then still use “@a” to replay the recorded commands. Finally, you can use 26 different registers. Thus you can remember 26 different command sequences to execute.

Using Registers

The registers used for recording are the same ones you used for yank and delete commands. This allows you to mix recording with other commands to manipulate the registers.

Suppose you have recorded a few commands in register n. When you execute this with “@n” you notice you did something wrong. You could try recording again, but perhaps you will make another mistake. Instead, use this trick:

GGo to the end of the file.
o<Esc>Create an empty line.
“npPut the text from the n register. You now see the commands you typed as text in the file.
{edits}Change the commands that were wrong. This is just like editing text.
0Go to the start of the line.
“ny$Yank the corrected commands into the n register.
ddDelete the scratch line.

Now you can execute the corrected commands with “@n“. If your recorded commands include line breaks, adjust the last two items in the example to include all the lines.

Appending To A Register

So far we have used a lowercase letter for the register name. To append to a register, use an uppercase letter.

Suppose you have recorded a command to change a word to register c. It works properly, but you would like to add a search for the next word to change. This can be done with:

qC/word\<Enter\>q

You start with “qC“, which records to the c register and appends. Thus writing to an uppercase register name means to append to the register with the same letter, but lowercase.

This works both with recording and with yank and delete commands. For example, you want to collect a sequence of lines into the a register. Yank the first line with:

"aY

Now move to the second line, and type:

"AY

Repeat this command for all lines. The a register now contains all those lines, in the order you yanked them.

Substitution

The “:substitute” command enables you to perform string replacements on a whole range of lines. The general form of this command is as follows:

:[range]substitute/from/to/[flags]

This command changes the “from” string to the “to” string in the lines specified with [range]. For example, you can change “Professor” to “Teacher” in all lines with the following command:

:%substitute/Professor/Teacher/

Note: The “:substitute” command is almost never spelled out completely. Most of the time, people use the abbreviated version “:s“. From here on the abbreviation will be used.

The “%” before the command specifies the command works on all lines. Without a range, “:s” only works on the current line.

By default, the “:substitute” command changes only the first occurrence on each line. For example, the preceding command changes the line:

Professor Smith criticized Professor Johnson today.

to:

Teacher Smith criticized Professor Johnson today.

To change every occurrence on the line, you need to add the g (global) flag. The command:

:%s/Professor/Teacher/g

results in (starting with the original line):

Teacher Smith criticized Teacher Johnson today.

Other flags include p (print), which causes the “:substitute” command to print out the last line it changes. The c (confirm) flag tells “:substitute” to ask you for confirmation before it performs each substitution. Enter the following:

:%s/Professor/Teacher/c

vim finds the first occurrence of “Professor” and displays the text it is about to change. You get the following prompt:

replace with Teacher (y/n/a/q/l/^E/^Y)?

At this point, you must enter one of the following answers:

yYes; make this change.
nNo; skip this match.
aAll; make this change and all remaining ones without further confirmation.
qQuit; don’t make any more changes.
lLast; make this change and then quit.
Ctrl-EScroll the text one line up.
Ctrl-YScroll the text one line down.

The “from” part of the substitute command is actually a pattern. The same kind as used for the search command. For example, this command only substitutes “the” when it appears at the start of a line:

:s/^the/these/

If you are substituting with a “from” or “to” part that includes a slash, you need to put a backslash before it. A simpler way is to use another character instead of the slash. A plus, for example:

:s+one/two+one or two+

Command ranges

The “:substitute” command, and other : commands, can be applied to a selection of lines. This is called a range.

The simple form of a range is {number},{number}. For example:

:1,5s/this/that/g

Executes the substitute command on the lines 1 to 5. Line 5 is included. The range is always placed before the command.

A single number can be used to address one specific line:

:54s/President/Fool/

Some commands work on the whole file when you do not specify a range. To make them work on the current line the “.” address is used. The “:write” command works like that. Without a range, it writes the whole file. To make it write only the current line into a file:

:.write otherfile

The first line always has number one. How about the last line? The “$” character is used for this. For example, to substitute in the lines from the cursor to the end:

:.,$s/yes/no/

The “%” range that we used before, is actually a short way to say “1,$“, from the first to the last line.

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

You can see a search pattern is used twice. The first “?^Chapter?” finds the line above the current position that matches this pattern. Thus the ?pattern? range is used to search backwards. Similarly, “/^Chapter/” is used to search forward for the start of the next chapter.

To avoid confusion with the slashes, the “=” character was used in the substitute command here. A slash or another character would have worked as well.

Add And Subtract

There is a slight error in the above command: If the title of the next chapter had included “grey” it would be replaced as well. Maybe that’s what you wanted, but what if you didn’t? Then you can specify an offset.

To search for a pattern and then use the line above it:

/Chapter/-1

You can use any number instead of the 1. To address the second line below the match:

/Chapter/+2

The offsets can also be used with the other items in a range. Look at this one:

:.+3,$-5

This specifies the range that starts three lines below the cursor and ends five lines before the last line in the file.

Using Marks

Instead of figuring out the line numbers of certain positions, remembering them and typing them in a range, you can use marks.

Place the marks as mentioned in chapter 3. For example, use “mt” to mark the top of an area and “mb” to mark the bottom. Then you can use this range to specify the lines between the marks (including the lines with the marks):

:'t,'b

Visual Mode And Ranges

You can select text with Visual mode. If you then press “:” to start a colon command, you will see this:

:'<,'>

Now you can type the command and it will be applied to the range of lines that was visually selected.

Note: When using Visual mode to select part of a line, or using Ctrl-V to select a block of text, the colon commands will still apply to whole lines.

The ‘< and ‘> are actually marks, placed at the start and end of the Visual selection. The marks remain at their position until another Visual selection is made. Thus you can use the “‘<” command to jump to position where the Visual area started. And you can mix the marks with other items:

:'>,$

This addresses the lines from the end of the Visual area to the end of the file.

A Number Of Lines

When you know how many lines you want to change, you can type the number and then “:“. For example, when you type “5:“, you will get:

:.,.+4

Now you can type the command you want to use. It will use the range “.” (current line) until “.+4” (four lines down). Thus it spans five lines.

The Global Command

The “:global” command is one of the more powerful features of vim. It allows you to find a match for a pattern and execute a command there. The general form is:

:[range]global/{pattern}/{command}

This is similar to the “:substitute” command. But, instead of replacing the matched text with other text, the command {command} is executed.

Note: The command executed for “:global” must be one that starts with a colon. Normal mode commands can not be used directly. The :normal command can do this for you.

Suppose you want to change “foobar” to “barfoo”, but only in C++ style comments. These comments start with “//“. Use this command:

:g+//+s/foobar/barfoo/g

This starts with “:g“. That is short for “:global“, just like “:s” is short for “:substitute“. Then the pattern, enclosed in plus characters. Since the pattern we are looking for contains a slash, this uses the plus character to separate the pattern. Next comes the substitute command that changes “foobar” into “barfoo”.

The default range for the global command is the whole file. Thus no range was specified in this example. This is different from “:substitute“, which works on one line without a range.

The command isn’t perfect, since it also matches lines where “//” appears halfway a line, and the substitution will also take place before the “//”.

Just like with “:substitute“, any pattern can be used. When you learn more complicated patterns later, you can use them here.

Visual Block Mode

With Ctrl-V you can start selection of a rectangular area of text. There are a few commands that do something special with the text block.

There is something special about using the “$” command in Visual block mode. When the last motion command used was “$“, all lines in the Visual selection will extend until the end of the line, also when the line with the cursor is shorter. This remains effective until you use a motion command that moves the cursor horizontally. Thus using “j” keeps it, “h” stops it.

Inserting Text

The command “I{string}<Esc>” inserts the text {string} in each line, just left of the visual block. You start by pressing Ctrl-V to enter visual block mode. Now you move the cursor to define your block. Next you type I to enter Insert mode, followed by the text to insert. As you type, the text appears on the first line only.

After you press <Esc> to end the insert, the text will magically be inserted in the rest of the lines contained in the visual selection. Example:

	include one 
	include two 
	include three 
	include four

Move the cursor to the “o” of “one” and press Ctrl-V. Move it down with “3j” to “four”. You now have a block selection that spans four lines. Now type:

Imain.<Esc>

The result:

	include main.one 
	include main.two 
	include main.three 
	include main.four

If the block spans short lines that do not extend into the block, the text is not inserted in that line. For example, make a Visual block selection that includes the word “long” in the first and last line of this text, and thus has no text selected in the second line:

	This is a long line 
	short 
	Any other long line 
		  ^^^^ selected block

Now use the command “Ivery <Esc>“. The result is:

	This is a very long line 
	short 
	Any other very long line

In the short line no text was inserted.

If the string you insert contains a newline, the “I” acts just like a Normal insert command and affects only the first line of the block.

The “A” command works the same way, except that it appends after the right side of the block. And it does insert text in a short line. Thus you can make a choice whether you do or don’t want to append text to a short line.

There is one special case for “A“: Select a Visual block and then use “$” to make the block extend to the end of each line. Using “A” now will append the text to the end of each line.

Using the same example from above, and then typing “$A XXX<Esc>, you get this result:

	This is a long line XXX 
	short XXX 
	Any other long line XXX 

This really requires using the “$” command. vim remembers that it was used. Making the same selection by moving the cursor to the end of the longest line with other movement commands will not have the same result.

Changing Text

The Visual block “c” command deletes the block and then throws you into Insert mode to enable you to type in a string. The string will be inserted in each line in the block.

Starting with the same selection of the “long” words as above, then typing “c_LONG_<Esc>“, you get this:

	This is a _LONG_ line 
	short 
	Any other _LONG_ line

Just like with “I” the short line is not changed. Also, you can’t enter a newline in the new text.

The “C” command deletes text from the left edge of the block to the end of line. It then puts you in Insert mode so that you can type in a string, which is added to the end of each line.

Starting with the same text again, and typing “Cnew text<Esc>” you get:

	This is a new text 
	short 
	Any other new text

Notice that, even though only the “long” word was selected, the text after it is deleted as well. Thus only the location of the left edge of the visual block really matters.

Again, short lines that do not reach into the block are excluded.

Other commands that change the characters in the block:

~swap case (a -> A and A -> a)
Umake uppercase (a -> A and A -> A)
umake lowercase (a -> a and A -> a)

Filling With A Character

To fill the whole block with one character, use the “r” command. Again, starting with the same example text from above, and then typing “rx“:

	This is a xxxx line 
	short 
	Any other xxxx line

Note: If you want to include characters beyond the end of the line in the block, check out the ‘virtualedit‘ feature (you can type “:help virtualedit” in vim to learn more).

Shifting

The command “>” shifts the selected text to the right one shift amount, inserting whitespace. The starting point for this shift is the left edge of the visual block.

With the same example again, “>” gives this result:

	This is a	  long line 
	short 
	Any other	  long line

The shift amount is specified with the ‘shiftwidth‘ option. To change it to use 4 spaces:

:set shiftwidth=4

The “<” command removes one shift amount of whitespace at the left edge of the block. This command is limited by the amount of text that is there; so if there is less than a shift amount of whitespace available, it removes what it can.

Joining Lines

The “J” command joins all selected lines together into one line. Thus it removes the line breaks. Actually, the line break, leading white space and trailing white space is replaced by one space. Two spaces are used after a line ending (that can be changed with the ‘joinspaces‘ option).

Let’s use the example that we got so familiar with now. The result of using the “J” command:

	This is a long line short Any other long line 

The “J” command doesn’t require a blockwise selection. It works with “v” and “V” selection in exactly the same way.

If you don’t want the white space to be changed, use the “gJ” command.

Reading and writing part of a file

When you are writing an e-mail message, you may want to include another file. This can be done with the “:read {filename}” command. The text of the file is put below the cursor line.

Starting with this text:

	Hi John, 
	Here is the diff that fixes the bug: 
	Bye, Pierre.

Move the cursor to the second line and type:

	:read patch

The file named “patch” will be inserted, with this result:

	Hi John, 
	Here is the diff that fixes the bug: 
	2c2 
	<	for (i = 0; i <= length; ++i) 
	--- 
	>	for (i = 0; i < length; ++i) 
	Bye, Pierre.

The “:read” command accepts a range. The file will be put below the last line number of this range. Thus “:$r patch” appends the file “patch” at the end of the file.

What if you want to read the file above the first line? This can be done with the line number zero. This line doesn’t really exist, you will get an error message when using it with most commands. But this command is allowed:

:0read patch

The file “patch” will be put above the first line of the file.

Writing A Range Of Lines

To write a range of lines to a file, the “:write” command can be used. Without a range it writes the whole file. With a range only the specified lines are written:

:.,$write tempo

This writes the lines from the cursor until the end of the file into the file “tempo”. If this file already exists you will get an error message. vim protects you from accidentally overwriting an existing file. If you know what you are doing and want to overwrite the file, append !:

:.,$write! tempo

CAREFUL: The ! must follow the “:write” command immediately, without white space. Otherwise, it becomes a filter command, which is explained later on this page.

Appending To A File

In the first section of this page was explained how to collect a number of lines into a register. The same can be done to collect lines in a file. Write the first line with this command:

:.write collection

Now move the cursor to the second line you want to collect, and type this:

:.write >>collection

The “>>” tells vim the “collection” file is not to be written as a new file, but the line must be appended at the end. You can repeat this as many times as you like.

Formatting text

When you are typing plain text, it’s nice if the length of each line is automatically trimmed to fit in the window. To make this happen while inserting text, set the ‘textwidth‘ option:

:set textwidth=72

To check the current value of ‘textwidth‘:

:set textwidth

Now lines will be broken to take only up to 72 characters. But when you insert text halfway a line, or when you delete a few words, the lines will get too long or too short. vim doesn’t automatically reformat the text.

To tell vim to format the current paragraph:

gqap

This starts with the “gq” command, which is an operator. Following is “ap“, the text object that stands for “a paragraph”. A paragraph is separated from the next paragraph by an empty line.

Note: A blank line, which contains white space, does NOT separate paragraphs. This is hard to notice!

Instead of “ap” you could use any motion or text object. If your paragraphs are properly separated, you can use this command to format the whole file:

gggqG

gg” takes you to the first line, “gq” is the format operator and “G” the motion that jumps to the last line. In case your paragraphs aren’t clearly defined, you can format just the lines you manually select. Move the cursor to the first line you want to format. Start with the command “gqj“. This formats the current line and the one below it. If the first line was short, words from the next line will be appended. If it was too long, words will be moved to the next line. The cursor moves to the second line. Now you can use “.” to repeat the command. Keep doing this until you are at the end of the text you want to format.

Changing Case

You have text with section headers in lowercase. You want to make the word “section” all uppercase. Do this with the “gU” operator. Start with the cursor in the first column:

			     gUw
 	section header	    ---->      SECTION header

The “gu” operator does exactly the opposite:

			     guw
 	SECTION header	    ---->      section header

You can also use “g~” to swap case. All these are operators, thus they work with any motion command, with text objects and in Visual mode.

To make an operator work on lines you double it. The delete operator is “d“, thus to delete a line you use “dd“. Similarly, “gugu” makes a whole line lowercase. This can be shortened to “guu“. “gUgU” is shortened to “gUU” and “g~g~” to “g~~“. Example:

				g~~ 
 	Some GIRLS have Fun    ---->   sOME girls HAVE fUN 

Using An External Program

vim has a very powerful set of commands, it can do anything. But there may still be something that an external command can do better or faster.

The command “!{motion}{program}” takes a block of text and filters it through an external program. In other words, it runs the system command represented by {program}, giving it the block of text represented by {motion} as input. The output of this command then replaces the selected block.

Because this summarizes badly if you are unfamiliar with UNIX filters, take a look at an example. The sort command sorts a file. If you execute the following command, the unsorted file input.txt will be sorted and written to output.txt. This works on both UNIX and Microsoft Windows.

sort <input.txt >output.txt

Now do the same thing in vim. You want to sort lines 1 through 5 of a file. You start by putting the cursor on line 1. Next you execute the following command:

!5G

The “!” tells vim that you are performing a filter operation. The vim editor expects a motion command to follow, indicating which part of the file to filter. The “5G” command tells vim to go to line 5, so it now knows that it is to filter lines 1 (the current line) through 5.

In anticipation of the filtering, the cursor drops to the bottom of the screen and a ! prompt displays. You can now type in the name of the filter program, in this case sort. Therefore, your full command is as follows:

!5Gsort<Enter>

The result is that the sort program is run on the first 5 lines. The output of the program replaces these lines.

	line 55     -->    line 11
	line 33	    -->    line 22
	line 11	    -->    line 33
	line 22	    -->    line 44
	line 44	    -->    line 55
	last line   -->    last line

The “!!” command filters the current line through a filter. In Unix the date command prints the current time and date. “!!date<Enter>” replaces the current line with the output of date. This is useful to add a timestamp to a file.

When It Doesn’t Work

Starting a shell, sending it text and capturing the output requires that vim knows how the shell works exactly. When you have problems with filtering, check the values of these options:

shellspecifies the program that vim uses to execute external programs.
shellcmdflagargument to pass a command to the shell
shellquotequote to be used around the command
shellxquotequote to be used around the command and redirection
shelltypekind of shell (only for the Amiga)
shellslashuse forward slashes in the command (only for MS-Windows and alikes)
shellredirstring used to write the command output into a file

On Unix this is hardly ever a problem, because there are two kinds of shells: “sh“-like and “csh“-like. vim checks the ‘shell‘ option and sets related options automatically, depending on whether it sees “csh” somewhere in ‘shell‘.

On MS-Windows, however, there are many different shells and you might have to tune the options to make filtering work. Check the help for the options for more information.

Reading Command Output

To read the contents of the current directory into the file, use the following commands.

on Unix:

:read !ls

on MS Windows:

:read !dir

The output of the “ls” or “dir” command is captured and inserted in the text, below the cursor. This is similar to reading a file, except that the “!” is used to tell vim that a command follows.

The command may have arguments. And a range can be used to tell where vim should put the lines:

:0read !date -u

This inserts the current time and date in UTC format at the top of the file. If you have a date command that accepts the “-u” argument. Note the difference with using “!!date“: that replaced a line, while “:read !date” will insert a line.

Writing Text To A Command

The Unix command wc counts words. To count the words in the current file:

:write !wc

This is the same write command as before, but instead of a file name the “!” character is used and the name of an external command. The written text will be passed to the specified command as its standard input. The output could look like this:

       4      47     249 

The wc command isn’t verbose. This output means you have 4 lines, 47 words and 249 characters.

Watch out for this mistake:

:write! wc

This will write the file “wc” in the current directory, with force. (White space is important here!)

Redrawing The Screen

If the external command produced an error message, the display may have been messed up. vim is very efficient and only redraws those parts of the screen that it knows need redrawing. But it can’t know about what another program has written. To tell vim to redraw the screen, press Ctrl-L.

Useful Tricks

The substitute command can be used to replace all occurrences of a word with another word:

:%s/four/4/g

The “%” range means to replace in all lines. The “g” flag at the end causes all words in a line to be replaced.

This will not do the right thing if your file also contains “thirtyfour”. It would be replaced with “thirty4”. To avoid this, use the “\<” item to match the start of a word:

:%s/\\<four/4/g

Obviously, this still goes wrong on “fourteen”. Use “\>” to match the end of a word:

:%s/\<four\>/4/g

If you are programming, you might want to replace “four” in comments, but not in the code. Since this is difficult to specify, add the “c” flag to have the substitute command prompt you for each replacement:

:%s/\<four\>/4/gc

Replacing In Several Files

Suppose you want to replace a word in more than one file. You could edit each file and type the command manually. It’s a lot faster to use record and playback.

Let’s assume you have a directory with C++ files, all ending in “.cpp”. There is a function called “GetResp” that you want to rename to “GetAnswer”.

vim *.cppStart vim, defining the argument list to contain all the C++ files. You are now in the first file.
qqStart recording into the q register
:%s/\<GetResp\>/GetAnswer/gDo the replacements in the first file.
:wnextWrite this file and move to the next one.
qStop recording.
@qExecute the q register. This will replay the substitution and “:wnext“. You can verify that this doesn’t produce an error message.
999@qExecute the q register on the remaining files. At the last file you will get an error message, because “:wnext” cannot move to the next file. This stops the execution, and everything is done.

Note: When playing back a recorded sequence, an error stops the execution. Therefore, make sure you don’t get an error message when recording.

There is one catch: If one of the .cpp files does not contain the word “GetResp”, you will get an error and replacing will stop. To avoid this, add the “e” flag to the substitute command:

:%s/\<GetResp\>/GetAnswer/ge

The “e” flag tells “:substitute” that not finding a match is not an error.

In this next example, let’s change the text “Last, First” to “First Last”. Let’s say you have a list of names in this form:

	Doe, John 
	Smith, Peter

You want to change that to:

	John Doe 
	Peter Smith

This can be done with just one command:

:%s/\([^,]*\), \(.*\)/\2 \1/

Let’s break this down in parts. Obviously it starts with a substitute command. The “%” is the line range, which stands for the whole file. Thus the substitution is done in every line in the file. The arguments for the substitute command are “/from/to/“. The slashes separate the “from” pattern and the “to” string. This is what the “from” pattern contains:

The whole search string is:
\([^,]*\), \(.*\)
The first part between \( \) matches “Last”:
\(     \)
by matching anything but a comma:
[^,]
…any number of times:
*
…then matching a “, ” literally:
,
The second part between \( \) matches “First”:
\(  \)
by matching any character:
.
…any number of times:
*

In the “to” part we have “\2” and “\1“. These are called backreferences. They refer to the text matched by the “\( \)” parts in the pattern. “\2” refers to the text matched by the second “\( \)“, which is the “First” name. “\1” refers to the first “\( \)“, which is the “Last” name.

You can use up to nine backreferences in the “to” part of a substitute command. “\0” stands for the whole matched pattern.

Sort a list

In a Makefile you often have a list of files. For example:

	OBJS = \ 
		version.o \ 
		pch.o \ 
		getopt.o \ 
		util.o \ 
		getopt1.o \ 
		inp.o \ 
		patch.o \ 
		backup.o

To sort this list, filter the text through the external sort command:

/^OBJS
j
:.,/^$/-1!sort

This goes to the first line, where “OBJS” is the first thing in the line. Then it goes one line down and filters the lines until the next empty line. You could also select the lines in Visual mode and then use “!sort“. That’s easier to type, but more work when there are many lines.

The result is this:

	OBJS = \ 
		backup.o 
		getopt.o \ 
		getopt1.o \ 
		inp.o \ 
		patch.o \ 
		pch.o \ 
		util.o \ 
		version.o \

Notice that a backslash at the end of each line is used to indicate the line continues. After sorting, this is wrong! The “backup.o” line that was at the end didn’t have a backslash. Now that it sorts to another place, it must have a backslash.

The simplest solution is to add the backslash with “A \<Esc>“. You can keep the backslash in the last line, if you make sure an empty line comes after it. That way you don’t have this problem again.

Reverse line order

The :global command can be combined with the :move command to move all the lines before the first line, resulting in a reversed file. The command is:

:global/^/m 0

Abbreviated:

:g/^/m 0

A expressão regular ” ^ ” corresponde ao início da linha (mesmo que a linha esteja em branco). O comando : move move a linha correspondente para depois da mítica linha zeroth, para que a linha correspondente atual se torne a primeira linha do arquivo. Como o comando : global não se confunde com a alteração na numeração das linhas,: global prossegue para corresponder a todas as linhas restantes do arquivo e coloca cada uma como a primeira.

Isso também funciona em várias linhas. Primeiro mova para acima da primeira linha e marque-a com “mt”. Em seguida, mova o cursor para a última linha no intervalo e digite:

: 't + 1, .g / ^ / m' t

Contar palavras

Às vezes você precisa escrever um texto com um número máximo de palavras. O vim pode contar as palavras para você.

Quando o arquivo inteiro for o que você deseja contar, use este comando:

g Ctrl-G

Não digite um espaço após o g , isso é usado apenas aqui para facilitar a leitura do comando.

A saída é assim:

Col 1 de 0; Linha 141 de 157; Palavra 748 de 774; Byte 4489 de 4976 

Você pode ver em qual palavra você é (748) e o número total de palavras no arquivo (774).

Quando o texto é apenas parte de um arquivo, você pode ir para o início do texto, digitar ” g Ctrl-G “, mover para o final do texto, digitar ” g Ctrl-G ” novamente e usar seu cérebro para calcular a diferença na posição da palavra. Esse é um bom exercício, mas existe uma maneira mais fácil. No modo Visual, selecione o texto em que você deseja contar as palavras. Em seguida, digite g Ctrl-G . O resultado:

Selecionado 5 de 293 linhas; 70 de 1884 Palavras; 359 de 10928 bytes 

Encontre uma página de homem

Ao editar um script de shell ou programa C, você está usando um comando ou função para a qual deseja encontrar a página de manual (isso se aplica ao Linux, não ao MS Windows). Vamos usar primeiro uma maneira simples: Mova o cursor para a palavra que você deseja encontrar ajuda on e pressione K .

O vim executará o programa man externo na palavra. Se a página do manual for encontrada, ela será exibida. Isso usa o pager normal para rolar o texto (principalmente o programa mais ). Quando você chegar ao fim, pressionar <Enter> o levará de volta ao vim .

Uma desvantagem é que você não pode ver a página de manual e o texto em que está trabalhando ao mesmo tempo. Existe um truque para fazer a página de manual aparecer em uma janela do vim. Primeiro, carregue o plug-in man filetype:

:tempo de execução! ftplugin / man.vim

Coloque este comando no seu arquivo vimrc se você pretende fazer isso com freqüência. Agora você pode usar o comando ” : Man ” para abrir uma janela em uma página de manual:

: Man csh

Você pode rolar e o texto é destacado, o que permite encontrar a ajuda que estava procurando. Use Ctrl-W w para pular para a janela com o texto em que você estava trabalhando.

Para encontrar uma página de manual em uma seção específica, coloque o número da seção primeiro. Por exemplo, para procurar na seção 3 ” echo “:

: Man 3 eco

Para pular para outra página do manual, que está no texto com a forma típica “word (1)”, pressione Ctrl-] nela. Outros comandos ” : Man ” usarão a mesma janela.

Para exibir uma página de manual para a palavra sob o cursor, use o comando \ K .

(Se você redefiniu o <Leader> , use-o em vez da barra invertida). Por exemplo, você deseja saber o valor de retorno de ” strstr () ” ao editar esta linha:

if (strstr (entrada, "aap") ==) 

Mova o cursor para algum lugar em ” strstr ” e digite ” \ K “. Uma janela será aberta para exibir a página de manual para strstr () .

Aparar espaços em branco

Some people find spaces and tabs at the end of a line useless, wasteful, and ugly. To remove whitespace at the end of every line, execute the following command:

:%s/\s\+$//

The line range “%” is used, thus this works on the whole file. The pattern that the “:substitute” command matches with is “\s\+$“. This finds white space characters (\s), 1 or more of them (\+), before the end-of-line ($).

The “to” part of the substitute command is empty: “//”. Thus it replaces with nothing, effectively deleting the matched white space.

Another wasteful use of spaces is placing them before a tab. Often these can be deleted without changing the amount of white space. But not always! Therefore, you can best do this manually. Use this search command: “/“. You cannot see it, but there is a space before a tab in this command. Thus it’s “/<Space><Tab>“. Now use “x” to delete the space and check that the amount of white space doesn’t change. You might have to insert a tab if it does change. Type “n” to find the next match. Repeat this until no more matches can be found.

Find where a word is used

If you are a UNIX user, you can use a combination of vim and the grep command to edit all the files that contain a given word. This is extremely useful if you are working on a program and want to view or edit all the files that contain a specific variable.

For example, suppose you want to edit all the C program files that contain the word “frame_counter”. To do this you use the command:

vim `grep -l frame_counter *.c`

Let’s look at this command in detail. The grep command searches through a set of files for a given word. Because the -l argument is specified, the command will only list the files containing the word and not print the matching lines. The word it is searching for is “frame_counter”. Actually, this can be any regular expression. Note: What grep uses for regular expressions is not exactly the same as what vim uses.

The entire command is enclosed in backticks (“`“). This tells the UNIX shell to run this command and pretend that the results were typed on the command line. So what happens is that the grep command is run and produces a list of files, these files are put on the vim command line. This results in vim editing the file list that is the output of grep. You can then use commands like “:next” and “:first” to browse through the files.

Finding Each Line

The above command only finds the files in which the word is found. You still have to find the word within the files.

vim has a built-in command that you can use to search a set of files for a given string. If you want to find all occurrences of “error_string” in all C program files, for example, enter the following command:

:grep error_string *.c

This causes vim to search for the string “error_string” in all the specified files (*.c). The editor will now open the first file where a match is found and position the cursor on the first matching line. To go to the next matching line (no matter in what file it is), use the “:cnext” command. To go to the previous match, use the “:cprev” command. Use “:clist” to see all the matches and where they are.

The “:grep” command uses the external commands grep (on Unix) or findstr (on Windows). You can change this by setting the option ‘grepprg‘.

Command Line Abbreviations

Some of the “:” commands are really long. We already mentioned that “:substitute” can be abbreviated to “:s“. This is a generic mechanism, all “:” commands can be abbreviated.

How short can a command get? There are 26 letters, and many more commands. For example, “:set” also starts with “:s“, but “:s” doesn’t start a “:set” command. Instead “:set” can be abbreviated to “:se“.

When the shorter form of a command could be used for two commands, it stands for only one of them. There is no logic behind which one, you have to learn them. In the help files the shortest form that works is mentioned. For example:

:s[ubstitute]

This means that the shortest form of “:substitute” is “:s“. The following characters are optional. Thus “:su” and “:sub” also work.

In the user manual we will either use the full name of command, or a short version that is still readable. For example, “:function” can be abbreviated to “:fu“. But since most people don’t understand what that stands for, we will use “:fun“. vim doesn’t have a “:funny” command, otherwise “:fun” would be confusing too.

It is recommended that in vim scripts you write the full command name. That makes it easier to read back when you make later changes. Except for some often used commands like “:w” (“:write“) and “:r” (“:read“).

A particularly confusing one is “:end“, which could stand for “:endif“, “:endwhile” or “:endfunction“. Therefore, always use the full name.

Short Option Names

In the user manual the long version of the option names is used. Many options also have a short name. Unlike “:” commands, there is only one short name that works. For example, the short name of ‘autoindent’ is ‘ai’. Thus these two commands do the same thing:

:set autoindent
:set ai

Command Line Completion

This is one of those vim features that, by itself, is a reason to switch from Vi to vim. Once you have used this, you can’t do without.

Suppose you have a directory that contains these files:

	info.txt
	intro.txt
	bodyofthepaper.txt

To edit the last one, you use the command:

:edit bodyofthepaper.txt

It’s easy to type this wrong. A much quicker way is:

:edit b<Tab>

Which will result in the same command. What happened? The <Tab> key does completion of the word before the cursor. In this case “b“. vim looks in the directory and finds only one file that starts with a “b“. That must be the one you are looking for, thus vim completes the file name for you.

Now type:

:edit i<Tab>

vim will beep, and give you:

:edit info.txt

The beep means that vim has found more than one match. It then uses the first match it found (alphabetically). If you press <Tab> again, you get:

:edit intro.txt

Thus, if the first <Tab> doesn’t give you the file you were looking for, press it again. If there are more matches, you will see them all, one at a time.

If you press <Tab> on the last matching entry, you will go back to what you first typed:

:edit i

Then it starts all over again. Thus vim cycles through the list of matches. Use Ctrl-P to go through the list in the other direction:

	      <────────────────────<Tab>──────────────────────────┐
								  |
		 <Tab> ──>		 <Tab> ──>
	:edit i		      :edit info.txt		   :edit intro.txt
		  <── Ctrl-P		       <── Ctrl-P
	   |
	   └───────────────────────Ctrl-P─────────────────────>

Command Context

When you type “:set i” instead of “:edit i” and press <Tab> you get:

:set icon

Hey, why didn’t you get “:set info.txt“? That’s because vim has context sensitive completion. The kind of words vim will look for depends on the command before it. The vim command knows that you cannot use a file name just after a “:set” command, but you can use an option name.

Again, if you repeat typing the <Tab>vim will cycle through all matches. There are quite a few, it’s better to type more characters first:

:set isk<Tab>

Gives:

:set iskeyword

Now type “=” and press <Tab>:

:set iskeyword=@,48-57,_,192-255

What happens here is that vim inserts the old value of the option. Now you can edit it.

What is completed with <Tab> is what vim expects in that place. Just try it out to see how it works. In some situations, you will not get what you want. That’s either because vim doesn’t know what you want, or because completion was not implemented for that situation. In that case you will get a <Tab> inserted (displayed as ^I).

Viewing List Matches

When there are many matches, you would like to see an overview. Do this by pressing Ctrl-D. For example, pressing Ctrl-D after:

:set is

results in:

:set is
incsearch  isfname    isident    iskeyword  isprint
:set is

vim lists the matches and then comes back with the text you typed. You can now check the list for the item you wanted. If it isn’t there, you can use <BS> to correct the word. If there are many matches, type a few more characters before pressing <Tab> to complete the rest.

If you have watched carefully, you will have noticed that “incsearch” doesn’t start with “is”. In this case “is” stands for the short name of “incsearch”. Many options have a short and a long name. vim is clever enough to know that you might have wanted to expand the short name of the option into the long name.

There Is More

O comando Ctrl-L completa a palavra com a string mais longa e inequívoca. Se você digitar ” : edit i ” e houver os arquivos “info.txt” e “info_backup.txt”, você receberá ” : edit info “.

A opção ‘ wildmode ‘ pode ser usada para alterar o funcionamento da conclusão. A opção ‘ wildmenu ‘ pode ser usada para obter uma lista de correspondências no menu. Use a opção ‘ sufixos ‘ para especificar arquivos menos importantes e que aparecem no final da lista de arquivos. A opção ‘ wildignore ‘ especifica os arquivos que não estão listados.

Exemplos

vim

Inicia o vim , colocando você no modo normal com um novo documento.

vim document.txt

Inicia o vim e abre o arquivo document.txt , ou um documento em branco, se document.txt ainda não existir.

ed – Um simples editor de texto.
emacs – Um editor de texto altamente extensível.
ex – modo editor de linha do editor de texto vi.
pico – Um simples editor de texto.

23 de outubro de 2019

Sobre nós

A Linux Force Brasil é uma empresa que ama a arte de ensinar. Nossa missão é criar talentos para a área de tecnologia e atender com excelência nossos clientes.

CNPJ: 13.299.207/0001-50
SAC:         0800 721 7901

Comercial  Comercial: (11) 3796-5900

Suporte:    (11) 3796-5900

Copyright © Linux Force Security  - Desde 2011.