New answers tagged

1

In vim, or with vi on a BSD system: Use the vi command :0r !hostname Or, in its longer form, :0read !hostname You would have to press Esc first, of course. The read command usually takes a filename and inserts the contents of that file beneath the current line, but if you specify a shell command with ! in front of it, it will take the output from ...


1

You tagged vi however it sounds like you're looking for a CLI option. While in vi you can use shift + O to insert above and automatically add a new line and just paste your line right in. If you don't want to use an editor you can use sed. sudo sed -i '1iabcd555.india.com' /etc/hosts


1

Would this work using sed? sed -i '1 i\'$HOSTNAME'' file Using this with a file named test1 produces: $ cat test1 one two three four five Then: $ sed -i '1 i\'$HOSTNAME'' test1 leads to: $ cat test1 chris-dell one two three four five


0

The easiest way to do this programmatically is to write to a temporary file and then overwrite the existing one: { printf '%s\n' "$(hostname)"; cat somefile } > somefile.tmp mv somefile.tmp somefile


3

If you just want to delete all occurrences of "involve", that is done with a standard vi command, e.g., :%s/\<involve\>//g On the other hand, if you want to delete exactly 10000 occurrences, you need something more involved, one of vim's extensions. The markers \< and \> are used to ensure that you do not change words such as "involved", "...


0

I don't know anything about vim so my response may be a non starter, but many years ago vi was considered a good skill because you could do everything with keystrokes so you were still ok if you didn't have a functional mouse.


0

It seems that you might have used numeric keypad. For me also, pressing / in numeric pad shows <F2> in command line. It is mapped by default. You can type :set termcap and look for t_K8. That will show <kDivide>. Solutions: 1) pressing / near to shift key instead of the other one. 2) setting termcap of t_K8 to /. 3) Mapping <kDivide> ...


2

You can use awk: awk '{ print > (NR % 2 ? "odd.txt" : "even.txt") }' a.txt This reads from a.txt and appends lines to either odd.txt or even.txt depending on the current line number.


0

Specifically for use with vi, there is no need to create the file before you edit it: vi can be used to create and save a new file. However, there are calling contexts where the file needs to exist. For example, on my system (OS X) I can launch an appropriate GUI editor (determined by file type) like this: open foo.txt This would open foo.txt in TextEdit, ...


8

readline's vi-mode is a subset of vi (essentially features that affect a single line, with some allowances for usability). The ci command is not part of vi; it is a vim feature. Further reading: A powerful VIM command I never knew about until now. Vim 101: Efficient HTML Editing with Text Objects How to replace text between quotes in vi Change inside ...


0

By man page of touch his primary job is to change file timestamps. Ofc is also creating the file with current timestamp and then you can edit the file. VI is text editor that do what it says edit text open,save,edit file etc. All is flavor of of user and habbit: touch then vi or vi file.txt same thing different colour.


3

There is no benefit to touching first; vi will create the file if it does not exist. The accepted answer says it checks whether you can write there before wasting time in an editor. True, but now you'll be wasting time typing touch every time. Not being able to write somewhere is fairly exceptional compared to how often it will just work (as long as you ...


6

Without touch, a new file won't exist until you tell vi to write it. Consider a multi-user system (perhaps you're on an network-mounted filesystem shared by many systems each with many users). Running touch will ensure you have the file (and that you can write to it) and even updates the timestamp. Another user wanting to create such a file will see that ...


1

vi is a visual text editor (vi = visual) It's visual compared to "ed" anyway, which just lets you see and change one line of text at a time. The touch command updates the timestamp on an existing file, or creates a new file if the file didn't already exist. It's good for testing things that are highly dependent on timestamps. Now if your file is a text ...


22

Apart from the given answers, one advantage of touch is that any other user/terminal editing the same file while you touched it , will receive a warning when they try to save any changes. WARNING: The file has been changed since reading it!!! Do you really want to write to it (y/n)? This would alert them even though you have not made any changes per se ...


0

vi is used to edit a file as user, while touch can set the timestamp on it and is mostly used in scripts and such. Another way to create a file is: >newfile.txt


18

Apart from the accepted answer: It is worth noting that touch is used to update file timestamps. If you use touch on a file that exists, it will then update the files timestamp to the current date and time. If the file does not exist, it creates an empty file with the current date and time as the timestamp. vi, on the other hand, does not create a new file ...


107

touching the file first confirms that you actually have the ability to create the file, rather than wasting time in an editor only to find out that the filesystem is read-only or some other problem.


3

There's no benefit. vi will create a file if doesn't exist.


0

The question is specifically about CentOS7, and mentions vim. That can only be one of a few packages: vim-minimal, vim-enhanced, vim-x11. CentOS does not provide packages for other implementations of vi, such as nvi. The package description for vim-minimal says The vim-minimal package includes a minimal version of VIM, which is installed into /bin/...


0

The cd should not be required. The following line should do the same. /WebSphere/was85/mycel/mynode/AppServer/java_1.7_64/bin/java -cp \ /usr/my.jar com/com.my_comapny_witt_entire_name/myMain I broke the command line into two lines by using backslash continuation. There must be no characters between the backslash and the newline for continuation ...


1

To start with, it's better to always check the exit status of cd, so cd /WebSphere/was85/mycel/mynode/AppServer/java_1.7_64/bin && ./java -cp /usr/my.jar com/com.my_comapny_witt_entire_name/myMain cmd1 && cmd2 means execute cmd2 if cmd1 succeeds. Now I suspect your problem here is that that script is not being interpreted by that OMVS ...


0

Just a backslash cd /WebSphere/was85/mycel/mynode/AppServer/java_1.7_64/bin ./java -cp \ /usr/my.jar com/com.my_comapny_witt_entire_name/myMain



Top 50 recent answers are included