Useful tips and tricks for the UNIX VIM editor.

Posted: November 30, 2015. At: 10:57 PM. This was 2 years ago. Post ID: 8529
Page permalink.
WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters. These cookies expire two weeks after they are set.

When in command mode, press the u key to undo the last change to a file.

Press U to undo last change(s) on a line.

To open a file at a certain line, start it like this: vim +567 file.txt. This will open the file file.txt on line 567.

Using the VIM editor for Linux is a good way to edit configuration files and write programming code. if the VIM editor is not installed on your system then type sudo apt-get install vim. To type text once you have entered the editor, press the i key to enter editing mode. To delete a line of text, move to that line and then press the d key in command mode. This is reached by pressing Esc to exit editing mode. in the command mode, you may type :w to save the current file, :q! to exit without saving and :wq to exit and save your current file. If you type :w myfile2.txt then the file currently being edited will be saved as a copy of the current buffer with that file name.

To open the file with the cursor on the line of paragraph that contains a certain string of text then type this at the bash prompt: vim myfile.txt +/foo and the file will be opened at the first occurrence of the text string “foo”. For a C source file, you can type vim myfile.cpp +/main and the file will be opened at the first occurrence of main().

If you wish to insert a file into a file you are editing, exit to command mode and type :r and the path to the filename. Below is an example.

:r /etc/bash.bashrc

This will insert the contents of that file at the cursor position.

No comments have been made. Use this form to start the conversation :)

Leave a Reply