6

For new files, Sublime Text changes the file name to the first line of the file:

enter image description here

Is it possible to achieve this on Vim?

5

I don't use Sublime Text, but something like this sounds like what you want:

function! SetFileNameToFirstLine()
  silent execute "file ".fnameescape(getline(1))
endfunction

let s:set_file_name_to_first_line_active = 0
function! ToggleSetFileNameToFirstLine()
  if s:set_file_name_to_first_line_active
    silent! augroup! SFNTFL
    echo "SetFileNameToFirstLine disabled"
  else
    augroup SFNTFL
      au! InsertLeave,CursorHold,CursorHoldI <buffer> call SetFileNameToFirstLine()
    augroup END
    call SetFileNameToFirstLine()
    echo "SetFileNameToFirstLine enabled"
  endif
  let s:set_file_name_to_first_line_active = !s:set_file_name_to_first_line_active
endfunction

nnoremap <F9> :call ToggleSetFileNameToFirstLine()<CR>

F9 will start changing your filename to the first line. Hit it again to disable this behaviour. (You can make it default, but I really recommend against it...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.