Archive

Archive for July, 2020

vim, less, head, tail and grep

July 14th, 2020 No comments

The vim method is a command line approach to edit file, without the need to open Microsoft office or editors that require the X-server. Hence, it is very handy if you login to remote server e.g. iridis and want to edit some control parameter files for simulation. You can also use vim to edit your programming codes at remote server.

  1. Open or create a file called control.par at a terminal=> vim control.par
  2. Once the file is opened using vim, to insert text, just type ‘i’ to go to the insert mode. Then you can add text, or to delete text using the delete/back_space key.
  3. To get out of the insert mode just press the Escape key [Esc].
  4. You can delete, while not in the insert mode, by pressing the ‘d’ key and right(left) arrow to delete the character to the right(left) of the cursor. You can delete the whole line by typing (not in the insert mode) ‘dd’.
  5. To copy, you can just highlight the text (using mouse) and go the place that you want to past the text and click the middle button of the mouse to paste.
  6. At vim, to save the file type=> [Esc] :w [Enter]
  7. At vim: to save and quit vim=> [Esc] :wq [Enter]
  8. At vim: to quit => [Esc] :q [Enter]
  9. At vim: to quit without saving the changes=> [Esc] :q! [Enter]
  10. At vim, you can go to the top of the file using command=> gg
  11. At vim, you can go to the bottom of the file using command=> G (this is given by the command [shift g])
  12. To go to the 16th line=> :16
  13. At vim, you can search for a word (e.g. mike) using command=> /mike[enter]. You can continue searching for the same phrase by => /[enter].

The less, tail and head are useful command line techniques for reading text files.

  1. At the terminal, if you want to read a text file e.g. control.par => less control.par
  2. Once the file is open using less: you can use page_up, page_down or space bar to navigate.
  3. Searching in less is the same as vim, to search for the phrase mike => /mike[Enter]
  4. Go to the top=> gg
  5. Go to the bottom=> G
  6. To quit less => q
  7. If you just want to display the first few lines of a file (e.g. control.par), at the terminal type=> head control.par
  8. If you just want to display the first 15 lines of a file (e.g. control.par), at the terminal type=> head -15 control.par
  9. If you just want to display the last few lines of a file (e.g. control.par), at the terminal type=> tail control.par
    If you just want to display the last 5 lines of a file (e.g. control.par), at the terminal type=> tail -5 control.par

If you want to find the phrase (e.g. mike) from all the .cpp file, grep is a useful command for that.

  1. To display on your terminal all the .cpp files that contain the phrase mike => grep mike *.cpp
  2. To display on your terminal all the .cpp files that contain the phrase mike together with the line number => grep -n mike *.cpp
Categories: Uncategorised Tags: